如何将其重新放置后,如何在PHP PDO mysql中插入LastInsertID。 我已经从数据库中检索了它, 现在我想插入它尝试过连接它的不正常工作,但不确定错误的何时尝试,但如果我回应了最后一个塞的ID,它的显示 先感谢您 这是我的代码

public function addinvoice($data){

    $this->db->query('INSERT INTO tbl_invoce_total (sub_total,total_tax,total,last_activity)
                          VALUES (:sub_total,:total_tax,:total,:last_activity)');
    $this->db->bind(':sub_total',$data['sub_total']);
    $this->db->bind(':total_tax',$data['total_tax']);
    $this->db->bind(':total',$data['total']);
    $this->db->bind(':last_activity',$data['last_activity']);
    $this->db->execute();
    $lastserted_id = $this->db->lastInsertId();
    echo $lastserted_id;

    foreach ($data['productname'] as $key => $value) {
        $this->db->query("INSERT INTO tbl_invoice_items (users_id,customer_id,companies_id,invoice_fk,productname,quantity,price,last_activity)
           VALUES (:users_id,:customer_id,:companies_id,$lastserted_id,:productname,:quantity,:price,:last_activity)");
        $this->db->bind(':users_id',$data['users_id']);
        $this->db->bind(':customer_id',$data['customer_id']);
        $this->db->bind(':companies_id',$data['companies_id']);
        $this->db->bind("$lastserted_id", 'invoice_fk');
        $this->db->bind(':productname',$data['productname'][$key]);
        $this->db->bind(':quantity',$data['quantity'][$key]);
        $this->db->bind(':price',$data['price'][$key]);
        $this->db->bind(':last_activity',$data['last_activity']);
        $this->db->execute();
    }

    echo $lastserted_id;
    die( print_r($data));
分析解答

您必须将一个占位符放入SQL中,您希望将插入式替代,然后绑定该占位符。

您只需要创建一次准备好的语句即可。然后在循环中绑定值并执行它。

public function addinvoice($data){

    $this->db->query('INSERT INTO tbl_invoce_total (sub_total,total_tax,total,last_activity)
                          VALUES (:sub_total,:total_tax,:total,:last_activity)');
    $this->db->bind(':sub_total',$data['sub_total']);
    $this->db->bind(':total_tax',$data['total_tax']);
    $this->db->bind(':total',$data['total']);
    $this->db->bind(':last_activity',$data['last_activity']);
    $this->db->execute();
    $lastserted_id = $this->db->lastInsertId();
    echo $lastserted_id;

    $this->db->query("INSERT INTO tbl_invoice_items (users_id,customer_id,companies_id,invoice_fk,productname,quantity,price,last_activity)
           VALUES (:users_id,:customer_id,:companies_id,:invoice_fk,:productname,:quantity,:price,:last_activity)");
    foreach ($data['productname'] as $key => $value) {
        $this->db->bind(':users_id',$data['users_id']);
        $this->db->bind(':customer_id',$data['customer_id']);
        $this->db->bind(':companies_id',$data['companies_id']);
        $this->db->bind(':invoice_fk', $lastserted_id);
        $this->db->bind(':productname',$data['productname'][$key]);
        $this->db->bind(':quantity',$data['quantity'][$key]);
        $this->db->bind(':price',$data['price'][$key]);
        $this->db->bind(':last_activity',$data['last_activity']);
        $this->db->execute();
    }

    echo $lastserted_id;
    die( print_r($data));