0
我有一个表格,我发布数据到两个不同的数据库表。您可以在下面看到我的交易。PDO事务最后插入的ID在表1中用于表2
$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”,我想要绑定一个值。这里的值应该与我的客户端表上自动增加的id值相同。
我为此需要以某种方式从表一中得到最后一个插入ID,并在表二中使用该值。我已经评论了我的失败尝试,但没有奏效,并且返回NULL
任何人都可以给我一些关于如何管理的指示吗?
嗯,是的,不知道为什么,我错过了。谢谢。 – Adnaves 2014-11-21 18:43:52