php - 无法使用 php 更改 mysql 中单行的两件事

标签 php mysql

代码如下:

$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/

相关文章:

java - 连接对象返回空值

mysql - #2006 - MySQL 服务器消失了

php - 如何使用 TwigBridge 在 Laravel 中使用自定义 Twig 函数

php - 将任何语言转换为 PHP 源代码

php - 仅显示数据库中的最后一条记录

php - 哪个更高效 PHP : array_intersect() OR array_intersect_key()

mysql - 从名称为 MySQL 中另一个选择结果的列中选择

mysql - 每个 ID 的最新日期

mysql - 稍后在查询中使用 AS 值

php - 使用 jquery 加载测验项目。为什么加载后我会丢失选择下一个项目的按钮?