PDO::lastInsertID 返回 0
PDO::lastInsertID returning 0
在阅读并尝试了关于该主题的几乎所有答案后,lastInsertId 仍然 returns 0。
我想我可能忽略了一些非常明显的东西,但似乎找不到什么。
这是我的代码:
public function create($ordertype, $userid, $travel, $distance, $time, $description, $lineids, $amounts, $descriptions, $prices, $orderid = null) {
try {
$stmt = $this->db->prepare("INSERT INTO workorder(orderid, userid, ordertype, travel, description, distance, totaltime)
VALUES(:orderid, :userid, :ordertype, :travel, :description, :distance, :totaltime)
ON DUPLICATE KEY UPDATE userid=:userid, ordertype=:ordertype, travel=:travel, description=:description,
distance=:distance, totaltime=:totaltime");
$stmt->bindparam(":orderid", $orderid);
$stmt->bindparam(":userid", $userid);
$stmt->bindparam(":ordertype", $ordertype);
$stmt->bindparam(":travel", $travel);
$stmt->bindparam(":description", $description);
$stmt->bindparam(":distance", $distance);
$stmt->bindparam(":totaltime", $time);
$stmt->execute();
$workorderid = $this->db->lastInsertId();
echo $workorderid;
exit();
return $stmt;
save_lines($workorderid, $lineids, $amounts, $descriptions, $prices);
} catch(PDOException $e) {
echo $e->getMessage();
}
}
这很明显...
我忘了检查记录是否已经存在。如果我不更改记录(工作单)中的任何内容,SQL 查询将不会执行任何操作,并且 lastInsertId
返回 0
。
我更新的代码(首先检查 orderid
的 id):
public function create($ordertype, $userid, $travel, $distance, $time, $description, $lineids, $amounts, $descriptions, $prices, $orderid = null) {
try {
$stmt = $this->db->prepare("INSERT INTO workorder(orderid, userid, ordertype, travel, description, distance, totaltime)
VALUES(:orderid, :userid, :ordertype, :travel, :description, :distance, :totaltime)
ON DUPLICATE KEY UPDATE userid=:userid, ordertype=:ordertype, travel=:travel, description=:description,
distance=:distance, totaltime=:totaltime");
$stmt->bindparam(":orderid", $orderid);
$stmt->bindparam(":userid", $userid);
$stmt->bindparam(":ordertype", $ordertype);
$stmt->bindparam(":travel", $travel);
$stmt->bindparam(":description", $description);
$stmt->bindparam(":distance", $distance);
$stmt->bindparam(":totaltime", $time);
$stmt->execute();
if($orderid == null) {
$workorderid = $this->db->lastInsertId();
} else {
$workorderid = $orderid;
}
if($amounts !== '') {
$this->save_lines($workorderid, $lineids, $amounts, $descriptions, $prices);
}
} catch(PDOException $e) {
echo $e->getMessage();
}
}
在阅读并尝试了关于该主题的几乎所有答案后,lastInsertId 仍然 returns 0。 我想我可能忽略了一些非常明显的东西,但似乎找不到什么。
这是我的代码:
public function create($ordertype, $userid, $travel, $distance, $time, $description, $lineids, $amounts, $descriptions, $prices, $orderid = null) {
try {
$stmt = $this->db->prepare("INSERT INTO workorder(orderid, userid, ordertype, travel, description, distance, totaltime)
VALUES(:orderid, :userid, :ordertype, :travel, :description, :distance, :totaltime)
ON DUPLICATE KEY UPDATE userid=:userid, ordertype=:ordertype, travel=:travel, description=:description,
distance=:distance, totaltime=:totaltime");
$stmt->bindparam(":orderid", $orderid);
$stmt->bindparam(":userid", $userid);
$stmt->bindparam(":ordertype", $ordertype);
$stmt->bindparam(":travel", $travel);
$stmt->bindparam(":description", $description);
$stmt->bindparam(":distance", $distance);
$stmt->bindparam(":totaltime", $time);
$stmt->execute();
$workorderid = $this->db->lastInsertId();
echo $workorderid;
exit();
return $stmt;
save_lines($workorderid, $lineids, $amounts, $descriptions, $prices);
} catch(PDOException $e) {
echo $e->getMessage();
}
}
这很明显...
我忘了检查记录是否已经存在。如果我不更改记录(工作单)中的任何内容,SQL 查询将不会执行任何操作,并且 lastInsertId
返回 0
。
我更新的代码(首先检查 orderid
的 id):
public function create($ordertype, $userid, $travel, $distance, $time, $description, $lineids, $amounts, $descriptions, $prices, $orderid = null) {
try {
$stmt = $this->db->prepare("INSERT INTO workorder(orderid, userid, ordertype, travel, description, distance, totaltime)
VALUES(:orderid, :userid, :ordertype, :travel, :description, :distance, :totaltime)
ON DUPLICATE KEY UPDATE userid=:userid, ordertype=:ordertype, travel=:travel, description=:description,
distance=:distance, totaltime=:totaltime");
$stmt->bindparam(":orderid", $orderid);
$stmt->bindparam(":userid", $userid);
$stmt->bindparam(":ordertype", $ordertype);
$stmt->bindparam(":travel", $travel);
$stmt->bindparam(":description", $description);
$stmt->bindparam(":distance", $distance);
$stmt->bindparam(":totaltime", $time);
$stmt->execute();
if($orderid == null) {
$workorderid = $this->db->lastInsertId();
} else {
$workorderid = $orderid;
}
if($amounts !== '') {
$this->save_lines($workorderid, $lineids, $amounts, $descriptions, $prices);
}
} catch(PDOException $e) {
echo $e->getMessage();
}
}