代码如下:
$id = intval($_POST['id']);
$score = "'" . $_POST['score'] . "'";
$shares = "'" . $_POST['shares'] . "'";
$conn = new PDO('mysql:host=localhost;dbname=news', 'root', '');
$stmt = $conn->prepare("UPDATE news SET 'shares' = :shares, 'score' = :score
WHERE id = :id");
$stmt -> execute(array(
'shares' => $shares,
'score' => $score,
'id' => $id
));
而且它不起作用。我不确定如何看到我认为 mysql 在某处给出的错误,并且我已经尝试了我能想到的一切。
使用双引号并立即将变量添加到语句中。
向份额和分数添加单引号。
我该怎么做?
最佳答案
我认为您需要删除列名称中的引号,例如如下:
$stmt = $conn->prepare("UPDATE news SET shares = :shares, score = :score
WHERE id = :id");
最好使用bindParam
方法来绑定(bind)参数。
$stmt ->bindParam(':shares', $shares, PDO::PARAM_INT);
$stmt ->bindParam(':score', $score, PDO::PARAM_INT);
$stmt ->bindParam(':id', $id, PDO::PARAM_INT);
$stmt -> execute();
我假设所有字段都是 INT 类型。如果没有,请更新为适当的类型。
关于php - 无法使用 php 更改 mysql 中单行的两件事,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13132862/