如何在一个查询中使用 PDO 进行 INSERT 和 UPDATE?
How to INSERT and UPDATE with PDO in one query?
我有以下 INSERT 片段可以正常工作:
$sql = "INSERT INTO `rating`(`business_id`, `user_id`, `quality`, `service`, `value`) VALUES (?,?,?,?,?)";
$query = $dbh->prepare($sql);
$query->bindParam(1, $business_id, PDO::PARAM_STR, 255);
$query->bindParam(2, $user_id, PDO::PARAM_STR, 255);
$query->bindParam(3, $quality, PDO::PARAM_STR, 255);
$query->bindParam(4, $service, PDO::PARAM_STR, 255);
$query->bindParam(5, $value, PDO::PARAM_STR, 255);
$query->execute();
不过,如果插入成功,我想更新另一个 table。
我认为这样就可以了:
if ($stmt->execute()) {
$sql = "UPDATE users SET prestige = prestige + 5";
$query = $dbh->prepare($sql);
$query->execute();
}
但是运气不好,谁能给我指出正确的方向?
有效的最终代码:
if ($query->execute()) {
$sql = "UPDATE user SET prestige = prestige + 250
WHERE id = {$user_id}";
$query = $dbh->query($sql);
}
execute returns TRUE 表示成功,FALSE 表示失败。所以这段代码必须有效。我认为你的问题是 'if ($stmt->execute())' 你应该检查 'if ($query->execute())'
$sql = "INSERT INTO `rating`(`business_id`, `user_id`, `quality`, `service`, `value`) VALUES (?,?,?,?,?)";
$query = $dbh->prepare($sql);
$query->bindParam(1, $business_id, PDO::PARAM_STR, 255);
$query->bindParam(2, $user_id, PDO::PARAM_STR, 255);
$query->bindParam(3, $quality, PDO::PARAM_STR, 255);
$query->bindParam(4, $service, PDO::PARAM_STR, 255);
$query->bindParam(5, $value, PDO::PARAM_STR, 255);
if ($query->execute()) {
$sql = "UPDATE users SET prestige = prestige + 5";
$query = $dbh->prepare($sql);
$query->execute();
}
if ($stmt->execute()) {
$sql = "UPDATE users SET prestige = prestige + 5";
$query = $dbh->query($sql);
}
只需使用查询,因为您不需要准备好的语句。
我有以下 INSERT 片段可以正常工作:
$sql = "INSERT INTO `rating`(`business_id`, `user_id`, `quality`, `service`, `value`) VALUES (?,?,?,?,?)";
$query = $dbh->prepare($sql);
$query->bindParam(1, $business_id, PDO::PARAM_STR, 255);
$query->bindParam(2, $user_id, PDO::PARAM_STR, 255);
$query->bindParam(3, $quality, PDO::PARAM_STR, 255);
$query->bindParam(4, $service, PDO::PARAM_STR, 255);
$query->bindParam(5, $value, PDO::PARAM_STR, 255);
$query->execute();
不过,如果插入成功,我想更新另一个 table。
我认为这样就可以了:
if ($stmt->execute()) {
$sql = "UPDATE users SET prestige = prestige + 5";
$query = $dbh->prepare($sql);
$query->execute();
}
但是运气不好,谁能给我指出正确的方向?
有效的最终代码:
if ($query->execute()) {
$sql = "UPDATE user SET prestige = prestige + 250
WHERE id = {$user_id}";
$query = $dbh->query($sql);
}
execute returns TRUE 表示成功,FALSE 表示失败。所以这段代码必须有效。我认为你的问题是 'if ($stmt->execute())' 你应该检查 'if ($query->execute())'
$sql = "INSERT INTO `rating`(`business_id`, `user_id`, `quality`, `service`, `value`) VALUES (?,?,?,?,?)";
$query = $dbh->prepare($sql);
$query->bindParam(1, $business_id, PDO::PARAM_STR, 255);
$query->bindParam(2, $user_id, PDO::PARAM_STR, 255);
$query->bindParam(3, $quality, PDO::PARAM_STR, 255);
$query->bindParam(4, $service, PDO::PARAM_STR, 255);
$query->bindParam(5, $value, PDO::PARAM_STR, 255);
if ($query->execute()) {
$sql = "UPDATE users SET prestige = prestige + 5";
$query = $dbh->prepare($sql);
$query->execute();
}
if ($stmt->execute()) {
$sql = "UPDATE users SET prestige = prestige + 5";
$query = $dbh->query($sql);
}
只需使用查询,因为您不需要准备好的语句。