[英]MySQL UPDATE Query Syntax Error?
I am relatively new to MySQL and PHP and I have been trying to UPDATE a table for a very long time now, I've searched Google and SO and I still can't figure it out. 我是MySQL和PHP的新手,很长一段时间以来我一直在尝试更新表,我已经搜索过Google和SO,但仍然无法弄清楚。
Here is the php: 这是PHP:
$info = array('about_me' => NULL, 'profile_pic' => NULL, 'political_party' => NULL, 'econ_views' => NULL, 'religious_views' => NULL,
'abortion_view' =>NULL,'gay_marraige' => NULL, 'other' => NULL);
foreach ($_POST as $key => $value) {
$info[$key] = mysql_escape_string($value);
}
$about_me = $info['about_me'];
$profile_pic = $info['profile_pic'];
$econ_views = $info['econ_views'];
$religious_views = $info['religious_views'];
$abortion_view = $info['abortion_view'];
$gay_marraige = $info['gay_marraige'];
$other = $info['other'];
$political_party = $info['political_party'];
//Connect to database
require 'db.php';
$query = "UPDATE `users` SET `about_me`=$about_me, `profile_pic`=$profile_pic, `econ_views`=$econ_views,
`religious_views`=$religious_views,`abortion_view`=$abortion_view,`gay_marriage`=$gay_marraige,
`other`=$other, `political_party`=$political_party WHERE `username`=emoore24";
echo "$query"."<br /><br />";
$result = mysql_query($query) or die(mysql_error());
echo "success"
This is run on a form with many text areas and one select element. 它在具有许多文本区域和一个select元素的表单上运行。 I ran everything with simple strings as data and got this: 我使用简单的字符串作为数据来运行所有内容,并得到了以下信息:
UPDATE
users
SETabout_me
=test about,profile_pic
=,econ_views
=test econ,religious_views
=test rel,abortion_view
=test abortion,gay_marriage
=test gay marraige,other
=test other,political_party
=democrat WHEREusername
=emoore24 UPDATEusers
设置about_me
= test about,profile_pic
=,econ_views
= testecon_views
,religious_views
= test rel,abortion_view
= test堕胎,gay_marriage
= test gaygay_marriage
,other
= test other,political_party
= democrat WHEREusername
= emoore24You 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 '
econ_views
=test econ, >religious_views
=test rel,abortion_view
=test abor' at line 1 在第1行上,检查与您的MySQL>服务器版本相对应的手册,以在'econ_views
= testecon_views
,>religious_views
econ_views
= test rel,abortion_view
= test abor'附近使用正确的语法
I'm assuming that it's something small, but I can't see it. 我假设它很小,但我看不到。 Could anyone help? 有人可以帮忙吗?
You haven't put quotes around any of your string literals. 您没有在任何字符串文字周围加上引号。
UPDATE `users` SET
`about_me`=about_me,
`profile_pic`=,
`econ_views`=test econ,
`religious_views`=test rel,
`abortion_view`=test abortion,
`gay_marriage`=test gay marraige,
`other`=test other,
`political_party`=democrat
WHERE `username`=emoore24
Should be: 应该:
UPDATE `users` SET
`about_me`='about_me',
`profile_pic`=NULL,
`econ_views`='test econ',
`religious_views`='test rel',
`abortion_view`='test abortion',
`gay_marriage`='test gay marraige',
`other`='test other',
`political_party`='democrat'
WHERE `username`='emoore24'
If you use PDO with prepared statements, it would be a lot simpler and safer, and you won't have to worry about quoting or escaping literals. 如果您将PDO与预准备语句一起使用,它将更加简单和安全,并且您将不必担心引用或转义文字。 For example, here's how I might write that code: 例如,这是我编写该代码的方式:
$info = array(
'about_me' => NULL,
'profile_pic' => NULL,
'political_party' => NULL,
'econ_views' => NULL,
'religious_views' => NULL,
'abortion_view' => NULL,
'gay_marriage' => NULL,
'other' => NULL
);
$query = "UPDATE `users` SET
`about_me`=:about_me,
`profile_pic`=:profile_pic,
`econ_views`=:econ_views,
`religious_views`=:religious_views,
`abortion_view`=:abortion_view,
`gay_marriage`=:gay_marriage,
`other`=:other,
`political_party`=:political_party
WHERE `username`=:username";
if (($stmt = $pdo->prepare($query)) == FALSE) {
$err = $pdo->errorInfo(); die($err[2]);
}
$values = array_intersect_key($_POST, $info);
$values['username'] = 'emoore24';
if ($stmt->execute( $values ) == FALSE) {
$err = $stmt->errorInfo(); die($err[2]);
}
You need to quote the text in your query 您需要在查询中引用文本
UPDATE `users` SET `about_me`='about_me'
And do the same for the other fields. 并对其他字段执行相同操作。
Your query is wrong. 您的查询是错误的。 You need to put quotes around all values : 您需要在所有值周围加上引号:
Change your query like this: 像这样更改查询:
$query = "UPDATE `users` SET `about_me`='about_me', `profile_pic`='$profile_pic', `econ_views`='$econ_views',`religious_views`='$religious_views',`abortion_view`='$abortion_view',`gay_marriage`='$gay_marraige', `other`='$other', `political_party`='$political_party' WHERE `username`='emoore24'";
Hope this works :) 希望这有效:)
profile_pic
=, also looks wrong. profile_pic
=,看起来也不对。 I run my queries by hand in a mysql IDE or mysql command line editor to see what the issues are. 我在mysql IDE或mysql命令行编辑器中手动运行查询,以查看问题所在。
I also start with a small select statement and build it up. 我还从一个小的select语句开始并进行构建。 After I have a select statement that works I switch it to a update statement. 在我有一条有效的选择语句后,将其切换到更新语句。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.