Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 插入单个记录,获取上次插入的ID,然后执行多次插入_Php_Pdo - Fatal编程技术网

Php 插入单个记录,获取上次插入的ID,然后执行多次插入

Php 插入单个记录,获取上次插入的ID,然后执行多次插入,php,pdo,Php,Pdo,我有一个表单,在提交时,我希望插入一条记录,然后获取最后插入的ID,然后将多行插入另一个表中 我能把第一部分弄对,但第二部分不行 public function save($data) { $this->customer_id = $data['customer_id']; $this->description = $data['description']; $this->line_description = $data['line_descriptio

我有一个表单,在提交时,我希望插入一条记录,然后获取最后插入的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_转储,则公共函数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">

如果我正确理解您的问题,您可以尝试下一种方法。行信息包含在
$this->line\u description
$this->line\u 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();
        }   
    }
}
public函数保存($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(空($this->errors)){
$db=static::getDB();
$stmt=$db->prepare(“插入'invoices'('customer\u id')值(:customer\u id)”);
$stmt->bindValue(“:customer_id”,$this->customer_id,PDO::PARAM_INT);
$stmt->execute();
$last_id=$db->lastInsertId();
$db=static::getDB();
$stmt=$db->prepare(“插入到`line`(`description`、`amount`、`invoice\u id`)值(:description、:amount、:invoice\u id)”);
对于($i=0;$iline_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();
}   
}
}

您是否总是收到有关
$data
参数中某个项目的信息?谢谢您的回复,但我不理解您的问题?如果有帮助,在本例中,
$data['description']
是一个单值,而
$data['line\u description']
是一个数组。我更新了我的问题以显示htmlIn您的示例数据
$data['line\u description']
有2个元素,但
$data['line\u amount']
有1个元素。您还有
$data['line\u amount']
,并且金额不相等。您如何接收有关发票行的信息?再次感谢。@Zhorov,对不起,那是我的错。我正在用正确的转储文件更新我的问题。这很有效,谢谢!我只是想和你确认一下,这是否是每次循环bindValue,这是否有效?我见过一些示例(但无法让它们工作),其中它只在
$stmt->execute()
位上运行foreach循环