预处理语句MySQL UPDATE不更新数据库值

时间:2019-01-08 09:51:35

标签: php mysql pdo

我正在尝试编写一个脚本,以使用

形式键入的新值来更新数据库中存储的值。

从表单中获取值:

$reviewTitle = $_POST['reviewTitle'];
$storeScore = $_POST['storeScore'];
$reviewContent = $_POST['reviewContent'];

将值更新到数据库

$sql = "UPDATE reviews SET reviewTitle=?, storeScore=?, reviewContent=? WHERE reviewID=?";

$stmt = $db->prepare($sql);

$stmt->bind_param('sisi', $reviewTitle, $storeScore, $reviewContent, $_POST['edit']);

$stmt->execute();

if ($stmt->error) {
  echo "FAILURE!!! " . $stmt->error;
}
else echo "Updated {$stmt->affected_rows} rows";

header("Location: review?store=" . $store['storeName']);

我看不到为什么它不起作用的原因,我在语法上缺少什么吗?任何帮助表示赞赏

编辑:我添加了脚本输出的错误

Fatal error: Uncaught Error: Call to undefined method 
PDOStatement::bind_param() in . 
/home/o2q4e1ph6yl2/public_html/editreview.php:38 Stack trace: #0 
/home/o2q4e1ph6yl2/public_html/review.php(54): include() #1 {main} . 
thrown in /home/o2q4e1ph6yl2/public_html/editreview.php on line 38

1 个答案:

答案 0 :(得分:1)

该问题的答案是:

$query = $db->prepare("UPDATE reviews SET reviewTitle=:reviewTitle, storeScore=:storeScore, reviewContent=:reviewContent WHERE reviewID=:reviewID");

$query->execute(array(':reviewTitle' => $reviewTitle, ':storeScore' => 
$storeScore, ':reviewContent' => $reviewContent, ':reviewID' => $reviewID));

花了4个小时才弄清