【问题标题】:how to use lastinsertid, LAST_INSERT_ID to insert record into two table (PHP MYSQL) [duplicate]如何使用lastinsertid,LAST_INSERT_ID将记录插入两个表(PHP MYSQL)[重复]
【发布时间】:2020-03-03 21:45:17
【问题描述】:

我有两个表,在表 1 中,有一个自动增量 ID,我想在记录保存到表 1 后立即将最后一个 ID 插入表 2。是否可以这样做。我有一个用户填写的表格,然后在提交时将收集的数据提交到两个不同的表格。 这是我插入数据的代码

class.php

public function insertID(){
  $query = "INSERT INTO table_1 SET book_name=:book_name; INSERT INTO table_2 SET 
book_type=:book_type, book_type_id=:book_type_id";

 $stmt = $this->conn->prepare($query);

 $this->book_name=$this->name;  //this data came from a form
 $this->book_type=$this->book_type; //same as this one
 $this->book_type_id=(Im not sure what to put here)(I tried LAST_INSERT_ID and 
 mysqli_insert_id) 
 //data the will go here should come from the last inserted ID so I connect table 1 and 
 table 2 using this ID

  $stmt->bindParam(':book_name', $this->book_name);
  $stmt->bindParam(':book_type', $this->book_type);
  $stmt->bindParam(':book_type_id', $this->book_type_id);

  if($stmt->execute()){
        return  true;
    }else{
        return false;
    }
 }

这是我的表格

 include_once 'class.php';
 $class = new Class($db);

 if($_POST){
     $class->book_name=$_POST['book_name'];
     $class->book_type=$_POST['book_type'];

  if($class->insertId()){
    echo "saved";
   }else {
     not saved
    }
  }    

任何建议将不胜感激。

这是我在@GMB 的帮助下的解决方案

class.php

public function insertID(){

 $query_1 = "INSERT INTO table_1
     SET book_name=:book_name";

     $stmt_1 = $this->conn->prepare($query_1);         

     $this->book_name=$this->book_name; 

     $stmt_1->bindParam(':book_name', $this->book_name);

     if($stmt_1->execute()){

           $query_2 = "INSERT INTO table_2 
            SET book_type=:book_type, book_type_id=:book_type_id";

            $stmt_2 = $this->conn->prepare($query_2);

            $this->book_type=$this->book_type;
            $this->book_type_id=$this->conn->lastInsertId();

            $stmt_2->bindParam(':book_type', $this->book_type);
            $stmt_2->bindParam(':book_type_id', $this->book_type_id);

                  if($stmt_2->execute()){
                     return  true;
                         }else{
                           return false;
                                 }
    }else{
        return false;
    }
 }

【问题讨论】:

  • 你需要执行两个不同的语句。
  • 如何将它集成到我的函数中?
  • 感谢@FunkFortyNiner 的帮助,

标签: php mysql sql sql-insert


【解决方案1】:

您可以使用两个单独的查询来做到这一点:

$query1 = "INSERT INTO table_1(book_name) VALUES(:book_name)";
$query2 = "INSERT INTO table_2(book_type, book_type_id) VALUES(:book_type, LAST_INSERT_ID())";

第一个查询插入到父表中。秒使用MySQL函数LAST_INSERT_ID()恢复上一次插入时生成的id,插入到子表中。

您需要在单独的数据库调用中独立运行每个查询。比如:

$query1 = "INSERT INTO table_1(book_name) VALUES(:book_name)";
$stmt1 = $this->conn->prepare($query1);
$stmt1->bindParam(':book_name', ...);
if(!$stmt1->execute()){ # error handling}

$query2 = "INSERT INTO table_2(book_type, book_type_id) VALUES(:book_type, LAST_INSERT_ID())";
$stmt2 = $this->conn->prepare($query2);
$stmt1->bindParam(':book_type', ...);
if(!$stmt1->execute()){ # error handling }

【讨论】:

  • 如何将它集成到我的函数中?
  • 是否可以通过用户的一笔交易来做到这一点?我很困惑,但我真的很努力地得到你的建议。
  • @aaa28:我在答案中添加了一些(伪)代码。
  • 你太客气了,那我得做两张表格,不是我想要的,但我想现在就可以了,谢谢
  • @aaa28:你不必制作两个表格。当您提交一个表单时,您只需要执行两个 SQL 查询。
猜你喜欢
  • 2023-03-28
  • 1970-01-01
  • 2012-03-10
  • 2014-04-22
  • 2013-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-31
相关资源
最近更新 更多