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

时间:2019-03-20 09:59:11

标签: php pdo

我有一个表单,提交时我想插入一条记录,然后获取最后插入的ID,然后在另一张表中插入多行。

我可以正确地理解第一部分,而第二部分则不能。

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
    }
}

如果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输入:

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

1 个答案:

答案 0 :(得分:1)

如果我正确理解了您的问题,则可以尝试使用下一种方法。行信息包含在$this->line_description$this->line_amount中,因此您需要确保两个数组的元素数相等。您的方法是正确的-准备语句并针对每一行执行该语句。

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();
        }   
    }
}