我有以下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();
但是如果插入成功,我想更新另一个表。
我以为会这样做:
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);
}
答案 0 :(得分:1)
if ($stmt->execute()) {
$sql = "UPDATE users SET prestige = prestige + 5";
$query = $dbh->query($sql);
}
只需使用查询,因为您不需要预备语句。
答案 1 :(得分:0)
execute成功时返回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();
}