简体   繁体   English

使用最后一个ID PDO PHP MySQL将数据插入两个表

[英]Insert data to two tables using last id PDO PHP MySQL

I am trying to insert data to two tables using the id for the first table. 我正在尝试使用第一个表的ID将数据插入两个表。 I have tried several possibilities but none seem to work. 我尝试了几种可能性,但似乎都没有用。 Please see below for the current method I am using. 请参阅以下有关我当前使用的方法。 It is inserting to the first table but not the second. 它正在插入第一个表,但不插入第二个表。 Plus there isn't any error telling me what I did wrong. 另外,没有错误可以告诉我我做错了什么。

If anyone can tell me where I am going wrong that would be great. 如果有人可以告诉我我要去哪里错,那将是很好的。

public function addContact($cnt_fname,$cnt_lname,$cnt_email,$cnt_phone,$cnt_type,$cnt_company,$cnt_web,$cnt_add1,$cnt_add2,$cnt_city,$cnt_state,$cnt_post,$cnt_country,$cnt_status) {
    try
    {

    $stmt = $this->conn->prepare("

    START TRANSACTION;

    INSERT INTO LeadContact(lead_fname,lead_lname,lead_email,lead_phone,lead_type,lead_company,lead_add1,lead_add2,lead_city,lead_state,lead_post,lead_country,lead_status) 
    VALUES(:cnt_fname,:cnt_lname,:cnt_email,:cnt_phone,:cnt_type,:cnt_company,:cnt_add1,:cnt_add2,:cnt_city,:cnt_state,:cnt_post,:cnt_country,:cnt_status);

    INSERT INTO LeadCompany(company_phone,company_type,company_name,company_website,company_add1,company_add2,company_city,company_state,company_post,company_country,company_status,company_contact) 
    VALUES(:cnt_phone,cnt_type,:cnt_company,:cnt_web,:cnt_add1,:cnt_add2,:cnt_city,:cnt_state,:cnt_post,:cnt_country,:cnt_status,last_insert_id());

    COMMIT;
    ");

    $stmt->bindparam(":cnt_fname", $cnt_fname);
    $stmt->bindparam(":cnt_lname", $cnt_lname);
    $stmt->bindparam(":cnt_email", $cnt_email);                                       
    $stmt->bindparam(":cnt_phone", $cnt_phone);                                       
    $stmt->bindparam(":cnt_type", $cnt_type);                                         
    $stmt->bindparam(":cnt_company", $cnt_company);                                       
    $stmt->bindparam(":cnt_add1", $cnt_add1);                                         
    $stmt->bindparam(":cnt_add2", $cnt_add2);                                         
    $stmt->bindparam(":cnt_city", $cnt_city);                                         
    $stmt->bindparam(":cnt_state", $cnt_state);                                       
    $stmt->bindparam(":cnt_post", $cnt_post);                                         
    $stmt->bindparam(":cnt_country", $cnt_country);                                       
    $stmt->bindparam(":cnt_status", $cnt_status);
    $stmt->bindparam(":cnt_web", $cnt_web);

    $stmt->execute();

    return $stmt;

}
catch(PDOException $e)
{
    echo $e->getMessage();
}               
}

The suggested duplicate is not the same question as I am trying to use the last_insert_id() function. 建议的重复项与我尝试使用last_insert_id()函数的问题不同。

Separating the queries might help, you need to first execute the first query then get the ID of that first insert then use the id to insert new table.Remember, if you use a transaction you should use lastInsertId BEFORE you commit otherwise it will return 0 分离查询可能会有所帮助,您需要先执行第一个查询,然后获取该第一次插入的ID,然后使用该ID插入新表。请记住,如果您使用事务处理,则应在提交之前使用lastInsertId,否则将返回0

<?php
public function addContact($cnt_fname, $cnt_lname, $cnt_email, $cnt_phone, $cnt_type, $cnt_company, $cnt_web, $cnt_add1, $cnt_add2, $cnt_city, $cnt_state, $cnt_post, $cnt_country, $cnt_status)
{
    try {
        $this->conn->BeginTransaction();

        $stmt = $this->conn->prepare("INSERT INTO LeadContact(lead_fname,lead_lname,lead_email,lead_phone,lead_type,lead_company,lead_add1,lead_add2,lead_city,lead_state,lead_post,lead_country,lead_status) 
    VALUES(:cnt_fname,:cnt_lname,:cnt_email,:cnt_phone,:cnt_type,:cnt_company,:cnt_add1,:cnt_add2,:cnt_city,:cnt_state,:cnt_post,:cnt_country,:cnt_status);");

        $stmt->bindparam(":cnt_fname", $cnt_fname);
        $stmt->bindparam(":cnt_lname", $cnt_lname);
        $stmt->bindparam(":cnt_email", $cnt_email);
        $stmt->bindparam(":cnt_phone", $cnt_phone);
        $stmt->bindparam(":cnt_type", $cnt_type);
        $stmt->bindparam(":cnt_company", $cnt_company);
        $stmt->bindparam(":cnt_add1", $cnt_add1);
        $stmt->bindparam(":cnt_add2", $cnt_add2);
        $stmt->bindparam(":cnt_city", $cnt_city);
        $stmt->bindparam(":cnt_state", $cnt_state);
        $stmt->bindparam(":cnt_post", $cnt_post);
        $stmt->bindparam(":cnt_country", $cnt_country);
        $stmt->bindparam(":cnt_status", $cnt_status);

        if ($stmt->execute()) {

            //insert to table 2

            $inserted_id = $this->conn->lastInsertId(); //get last id


            $sql = $this->conn->prepare("INSERT INTO LeadCompany(company_phone,company_type,company_name,company_website,company_add1,company_add2,company_city,company_state,company_post,company_country,company_status,company_contact) 
    VALUES(:cnt_phone,cnt_type,:cnt_company,:cnt_web,:cnt_add1,:cnt_add2,:cnt_city,:cnt_state,:cnt_post,:cnt_country,:cnt_status,:insertid)");
            $sql->bindparam(":cnt_fname", $cnt_fname);
            $sql->bindparam(":cnt_lname", $cnt_lname);
            $sql->bindparam(":cnt_email", $cnt_email);
            $sql->bindparam(":cnt_phone", $cnt_phone);
            $sql->bindparam(":cnt_type", $cnt_type);
            $sql->bindparam(":cnt_company", $cnt_company);
            $sql->bindparam(":cnt_add1", $cnt_add1);
            $sql->bindparam(":cnt_add2", $cnt_add2);
            $sql->bindparam(":cnt_city", $cnt_city);
            $sql->bindparam(":cnt_state", $cnt_state);
            $sql->bindparam(":cnt_post", $cnt_post);
            $sql->bindparam(":cnt_country", $cnt_country);
            $sql->bindparam(":cnt_status", $cnt_status);
            $sql->bindparam(":insertidr", $inserted_id);

            if ($sql->execute()) {
                return $sql;
            }

        } else {

            throw new Exception("Error inserting");

        }

        if ($this->conn->commit()) {
            $success = true;
        } else {
            throw new Exception('Transaction commit failed.');
        }

    }
    catch (Exception $e) {
        try {
            // something went wrong, we have to rollback
            $this->conn->rollback();
            // and display the error message
            echo $e->getMessage();
        }
        catch (Exception $f) {
            // and display the error message
            echo $f->getMessage();
        }
    }
}
?>

Use Transactions , first execute the first command and get Last Insert Id , use it on the next insert. 使用Transactions ,首先执行第一个命令并获取Last Insert ID ,然后在下一次插入时使用它。

public function addContact($cnt_fname,$cnt_lname,$cnt_email,$cnt_phone,$cnt_type,$cnt_company,$cnt_web,$cnt_add1,$cnt_add2,$cnt_city,$cnt_state,$cnt_post,$cnt_country,$cnt_status)
{

    try {
        $db->beginTransaction();

        $stmt = $db->prepare("INSERT INTO LeadContact(lead_fname,lead_lname,lead_email,lead_phone,lead_type,lead_company,lead_add1,lead_add2,lead_city,lead_state,lead_post,lead_country,lead_status) 
  VALUES(:cnt_fname,:cnt_lname,:cnt_email,:cnt_phone,:cnt_type,:cnt_company,:cnt_add1,:cnt_add2,:cnt_city,:cnt_state,:cnt_post,:cnt_country,:cnt_status)");
        $stmt->bindparam(":cnt_fname", $cnt_fname);
        $stmt->bindparam(":cnt_lname", $cnt_lname);
        $stmt->bindparam(":cnt_email", $cnt_email);
        $stmt->bindparam(":cnt_phone", $cnt_phone);
        $stmt->bindparam(":cnt_type", $cnt_type);
        $stmt->bindparam(":cnt_company", $cnt_company);
        $stmt->bindparam(":cnt_add1", $cnt_add1);
        $stmt->bindparam(":cnt_add2", $cnt_add2);
        $stmt->bindparam(":cnt_city", $cnt_city);
        $stmt->bindparam(":cnt_state", $cnt_state);
        $stmt->bindparam(":cnt_post", $cnt_post);
        $stmt->bindparam(":cnt_country", $cnt_country);
        $stmt->bindparam(":cnt_status", $cnt_status);

        $insertId = $db->lastInsertId();

        $stmt = $db->prepare("INSERT INTO LeadCompany(company_phone,company_type,company_name,company_website,company_add1,company_add2,company_city,company_state,company_post,company_country,company_status,company_contact) 
  VALUES(:cnt_phone,:cnt_type,:cnt_company,:cnt_web,:cnt_add1,:cnt_add2,:cnt_city,:cnt_state,:cnt_post,:cnt_country,:cnt_status,:id)");
        $stmt->bindparam(":cnt_phone", $cnt_phone);
        $stmt->bindparam(":cnt_type", $cnt_type);
        $stmt->bindparam(":cnt_company", $cnt_company);
        $stmt->bindparam(":cnt_web", $cnt_web);
        $stmt->bindparam(":cnt_add1", $cnt_add1);
        $stmt->bindparam(":cnt_add2", $cnt_add2);
        $stmt->bindparam(":cnt_city", $cnt_city);
        $stmt->bindparam(":cnt_state", $cnt_state);
        $stmt->bindparam(":cnt_post", $cnt_post);
        $stmt->bindparam(":cnt_country", $cnt_country);
        $stmt->bindparam(":cnt_status", $cnt_status);
        $stmt->bindparam(":id", $insertId);
        $stmt->execute();

        $db->commit();
    } catch (PDOException $ex) {
        //Something went wrong rollback!
        $db->rollBack();
        throw $ex;
    }
}

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

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