[英]MySQL UPDATE Query PHP doesn't work
I'm trying to update a column, I got no modification in the column value can you please help me with that? 我正在尝试更新列,但列值没有修改,您能帮我吗?
Code i am trying:- 我正在尝试的代码:-
global $wpdb;
$param1 = $_GET['projectID'];
$sql1 = "UPDATE wp_projects SET nbrDonation = nbrDonation+1 WHERE projectID = $param1";
$wpdb->query($sql1);
echo $param1;
echo $sql1;
this is what i got as error : 这就是我得到的错误:
Erreur de la base de données WordPress : [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1] UPDATE wp_projects SET nbrDonation=nbrDonation+1 WHERE projectID=
UPDATE wp_projects SET nbrDonation=nbrDonation+1 WHERE projectID=
It seems like your $param1
value may be empty, or otherwise invalid. 您的
$param1
值似乎为空,否则无效。
[You have an error in your SQL syntax;
[您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1]
检查与您的MySQL服务器版本相对应的手册,以在第1行的“附近”使用正确的语法]
The ''
implies that the value is empty; ''
表示该值为空; so the SQL is doing: 所以SQL正在做:
UPDATE wp_projects SET nbrDonation=(nbrDonation+1) WHERE projectID=''
Which is invalid as nothing ( ''
) is not an integer value as expected. 这是无效的,因为没有(
''
)不是预期的整数值。
You need to force the $param1
value to be interger. 您需要强制
$param1
值是整数。 You can do this by typecasting in PHP . 您可以通过在PHP中进行类型转换来实现 。
so: 所以:
$param1 = (int)$_GET['projectID']; // forces it to a numeric value, 1 or 0
This will then mean the SQL will work correctly: 然后,这将意味着SQL将正常工作:
$sql1 = "UPDATE wp_projects SET nbrDonation = nbrDonation+1 WHERE projectID = $param1";
You do not need the brackets around the nbrDonation+1
and you do not need quotes around the ID number, because it's numeric. 您不需要
nbrDonation+1
周围的括号, 也不需要 ID号周围的引号 ,因为它是数字。
Please also note: 另请注意:
Remove the single quote your projectID 删除您的projectID的单引号
$sql1="UPDATE wp_projects SET nbrDonation=(nbrDonation+1) WHERE projectID=$param1";
Try now. 现在试试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.