我有一个表单,我将数据发布到两个不同的数据库表。您可以在下面看到我的交易。
$db->beginTransaction();
$sql = "INSERT INTO clients (name, contact_person, phone, email, url)
VALUES (:name, :contact_person, :phone, :email, :url)";
$stm = $db->prepare ( $sql );
$stm->bindParam ( ":name", $name );
$stm->bindParam ( ":contact_person", $contact_person );
$stm->bindParam ( ":phone", $phone );
$stm->bindParam ( ":email", $email );
$stm->bindParam ( ":url", $url );
$client = $stm->execute ();
//$last_id = $db->lastInsertId;
$sql = "INSERT INTO task (title, description, user_id, status_id, client_id)
VALUES (:title, :description, :user_id, :status_id ,:client_id)";
$stm = $db->prepare ( $sql );
$stm->bindParam ( ":title", $title );
$stm->bindParam ( ":description", $description );
$stm->bindParam ( ":user_id", $user_id );
$stm->bindParam ( ":status_id", $status_id );
//$stm->bindParam ( ":client_id", $last_id );
$task = $stm->execute ();
$db->commit();
然而,在我的表格"任务"我有另一栏" client_id"我想绑定一个值的地方。这里的值应该与我的clients表上自动增量的id值相同。
因此我需要以某种方式从表1中获取最后一个insertet id,并在表2中使用该值。我已经对我失败的尝试进行了评估,该尝试没有用,并返回NULL
有人能给我一些关于如何管理它的指示吗?
答案 0 :(得分:1)
请改用此功能:
$last_id = $db->lastInsertId();