MySQL UPDATE 查询语法错误?

发布于 2024-12-29 18:48:53 字数 1782 浏览 0 评论 0原文

我对 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"

它在一个具有许多文本区域和一个选择元素的表单上运行。我用简单的字符串作为数据运行了所有内容并得到了这个:

更新用户 SET about_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 SET about_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 WHERE username=emoore24

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 ' 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

云裳 2025-01-05 18:48:53

您还没有在任何字符串文字周围加上引号。

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

应该是:

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'

如果你将 PDO 与准备好的语句一起使用,它会更简单、更安全,而且你不必担心引用或转义文字。例如,我可以这样编写该代码:

$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 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. 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]);
}
一抹微笑 2025-01-05 18:48:53

您需要在查询中引用文本

UPDATE `users` SET `about_me`='about_me'

并对其他字段执行相同的操作。

You need to quote the text in your query

UPDATE `users` SET `about_me`='about_me'

And do the same for the other fields.

身边 2025-01-05 18:48:53

你的查询是错误的。您需要在所有值周围加上引号:

像这样更改您的查询:

$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'";

希望这有效:)

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 :)

痴情 2025-01-05 18:48:53

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文