MySQL UPDATE 查询语法错误?
我对 MySQL 和 PHP 比较陌生,我已经尝试更新一个表很长时间了,我已经搜索了 Google 和 SO,但我仍然无法弄清楚。
这是 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"
它在一个具有许多文本区域和一个选择元素的表单上运行。我用简单的字符串作为数据运行了所有内容并得到了这个:
更新
用户
SETabout_me
=测试关于,profile_pic
=,econ_views
=测试经济,宗教_views
=测试rel,abortion_view
=测试堕胎,gay_marriage
=测试同性恋婚姻,other
=测试其他,political_party
=democrat WHERE用户名
=emoore24您的 SQL 语法有错误;检查与您的 MySQL > 服务器版本相对应的手册,了解在 '
econ_views
=test econ, >religious_views
=test rel,abortion_view 附近使用的正确语法
=测试 abor' 在第 1 行
我假设它很小,但我看不到它。有人可以帮忙吗?
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.
Here is the 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. 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
=emoore24You have an error in your SQL syntax; 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
I'm assuming that it's something small, but I can't see it. Could anyone help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您还没有在任何字符串文字周围加上引号。
应该是:
如果你将 PDO 与准备好的语句一起使用,它会更简单、更安全,而且你不必担心引用或转义文字。例如,我可以这样编写该代码:
You haven't put quotes around any of your string literals.
Should be:
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. For example, here's how I might write that code:
您需要在查询中引用文本
并对其他字段执行相同的操作。
You need to quote the text in your query
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:
Hope this works :)
profile_pic
=,看起来也是错误的。我在 mysql IDE 或 mysql 命令行编辑器中手动运行查询,以查看问题所在。我还从一个小的选择语句开始并构建它。当我有了一个有效的 select 语句后,我将其切换为 update 语句。
profile_pic
=, also looks wrong. I run my queries by hand in a mysql IDE or mysql command line editor to see what the issues are.I also start with a small select statement and build it up. After I have a select statement that works I switch it to a update statement.