简体   繁体   English

插入单个记录,获取最后插入的ID,然后进行多次插入

[英]Insert single record, get last inserted ID and then do multiple insert

I have a single form that when submitting I want to insert a single record, then get the last inserted ID and then insert multiple rows into another table. 我有一个表单,提交时想插入一个记录,然后获取最后插入的ID,然后将多行插入另一个表。

I can get the first part right but not the second. 我可以正确地理解第一部分,而第二部分则不能。

public function save($data)
{
    $this->customer_id = $data['customer_id'];
    $this->description = $data['description'];
    $this->line_description = $data['line_description'];
    $this->line_amount = $data['line_amount'];


    $this->validate();

    if (empty($this->errors)) {

        $db = static::getDB();
        $stmt = $db->prepare("INSERT INTO `invoices` (`customer_id`) VALUES (:customer_id)");
        $stmt->bindValue(":customer_id", $this->customer_id, PDO::PARAM_INT);
        $stmt->execute();

        $last_id = $db->lastInsertId();


        $db = static::getDB();
        $stmt = $db->prepare("INSERT INTO `lines` (`description`, `amount`, `invoice_id`) VALUES (:description, :amount, :invoice_id)");
        $stmt->bindValue(":description", $this->line_description, PDO::PARAM_STR);
        $stmt->bindValue(":amount", $this->line_amount, PDO::PARAM_STR);
        $stmt->bindValue(":invoice_id", $last_id, PDO::PARAM_INT);
        // foreach loop here ?
        $stmt->execute();
        // end foreach
    }
}

$data in public function save($data) would look like this if var_dumped 如果var_dumped,则公共函数save($ data)中的$ data看起来像这样

 array(5) {
      ["customer_id"]=> string(2) "16"
      ["description"]=> string(4) "test"
      ["amount"]=> string(0) ""
      ["line_description"]=> array(2) {
           [0]=> string(5) "desc1"
           [1]=> string(4) "desc"
      }
      ["line_amount"]=> array(2) {
           [0]=> string(3) "123"
           [1]=> string(3) "456"
      }
 }

Html inputs: HTML输入:

<input type="text" class="form-control" placeholder="Line Description" name="line_description[]">
<input type="text" class="form-control" placeholder="Description" name="description">

If I understand your question correctly, you may try with the next approach. 如果我正确理解了您的问题,则可以尝试使用下一种方法。 Lines information is included in $this->line_description and $this->line_amount , so you need to be sure, that both arrays have equal count of elements. 行信息包含在$this->line_description$this->line_amount ,因此您需要确保两个数组的元素计数相等。 Your approach is correct - prepare statement and execute this statement for every line. 您的方法是正确的-准备语句并针对每一行执行此语句。

public function save($data)
{
    $this->customer_id = $data['customer_id'];
    $this->description = $data['description'];
    $this->line_description = $data['line_description'];
    $this->line_amount = $data['line_amount'];


    $this->validate();

    if (empty($this->errors)) {

        $db = static::getDB();
        $stmt = $db->prepare("INSERT INTO `invoices` (`customer_id`) VALUES (:customer_id)");
        $stmt->bindValue(":customer_id", $this->customer_id, PDO::PARAM_INT);
        $stmt->execute();

        $last_id = $db->lastInsertId();


        $db = static::getDB();
        $stmt = $db->prepare("INSERT INTO `lines` (`description`, `amount`, `invoice_id`) VALUES (:description, :amount, :invoice_id)");
        for ($i = 0; $i < count($this->line_description); $i++) 
        {       
            $stmt->bindValue(":description", $this->line_description[$i], PDO::PARAM_STR);
            $stmt->bindValue(":amount", $this->line_amount[$i], PDO::PARAM_STR);
            $stmt->bindValue(":invoice_id", $last_id, PDO::PARAM_INT);
            $stmt->execute();
        }   
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM