需要有关多个 WHERE 子句的帮助

发布于 2024-10-15 10:41:49 字数 617 浏览 5 评论 0原文

我有一个想要执行的 UPDATE 查询。

但在我们更新数据库中的数量之前,需要满足两个条件。

首先,session_id() 必须与 sessionid 列中的一个 sessionid 匹配,其次,产品的描述必须与该 sessionid 对应的产品描述匹配。

这就是我所拥有的:

mysql_query(UPDATE cart 
               SET quantity = $q, 
             WHERE sessionid = "'.session_id().'" 
               AND description = $d') or die(mysql_error());

现在,它给了我以下错误:

您的 SQL 语法有错误;
检查与您的 MySQL 服务器版本相对应的手册,了解在第 1 行 'WHERE sessionid="bqlbh5rdogbhmq70skhtkbvmb0" AND description=$d' 附近使用的正确语法

但是,我直接从 W3Schools 复制了此更新查询,因此它必须是正确的, 正确的?任何帮助将不胜感激。

I have an UPDATE query I'd like to perform.

But there need to be 2 conditions met before we can update a quantity in the database.

First, the sessions_id() must match one of the sessionid's in the sessionid column, and second, the Description of the product must match the description of the product corresponding to the sessionid.

Here's what I have:

mysql_query(UPDATE cart 
               SET quantity = $q, 
             WHERE sessionid = "'.session_id().'" 
               AND description = $d') or die(mysql_error());

Now, it is giving me the following error:

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 'WHERE sessionid="bqlbh5rdogbhmq70skhtkbvmb0" AND description=$d' at line 1

However, I have copied this update query straight from W3Schools, so it must be correct, right? Any help at all would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

倒数 2024-10-22 10:41:50

另外,这里的单引号有问题 .'" AND description=$d') 并且 $d 未被解析。这将导致错误的 SQL 查询。
将单引号更改为双引号,或者仅使用字符串附加 . 来附加 $d 的值,而不是文字 $d

示例:

$a = 5;
$b = 'a is $a'; // a is $a;
$c = "a is $a"; // a is 5

Also you have problem with the single quotes here .'" AND description=$d') and $d is not parsed. This will result wrong SQL query.
Change the single quotes to double quotes or just use string append . to append the value of $d, not the literal $d

example :

$a = 5;
$b = 'a is $a'; // a is $a;
$c = "a is $a"; // a is 5
情丝乱 2024-10-22 10:41:49

发生错误的原因是 WHERE 语句之前的“,”符号。正确的查询是:

mysql_query( "UPDATE cart SET amount = " . $q . " WHERE sessionid = "' . session_id( ) . '" AND description = " . $d . "" ) 或 die( mysql_error ( ) );

The error occurs because of the ',' sign before the WHERE statement. The correct query would be:

mysql_query( "UPDATE cart SET quantity = " . $q . " WHERE sessionid = "' . session_id( ) . '" AND description = " . $d . "" ) or die( mysql_error( ) );

凹づ凸ル 2024-10-22 10:41:49

删除 $q 后面的 ,(逗号)

remove the , (comma) after $q

秋风の叶未落 2024-10-22 10:41:49

您粘贴的代码格式错误,我怀疑您的应用程序中使用的代码。

$sql = sprintf(
    "UPDATE `cart` SET `quantity` = '%d' WHERE `sessionid` = '%s' AND `description` = '%s'", 
    $q, 
    session_id(), 
    $d
);
mysql_query($sql);

the code you have pasted is badly formatted, and i doubt the code used in your app.

$sql = sprintf(
    "UPDATE `cart` SET `quantity` = '%d' WHERE `sessionid` = '%s' AND `description` = '%s'", 
    $q, 
    session_id(), 
    $d
);
mysql_query($sql);
黑白记忆 2024-10-22 10:41:49

它应该看起来像这样:

mysql_query("UPDATE cart SET quantity = $q WHERE sessionid='" .session_id(). "' AND description = '$d'") or die(mysql_error());

It should look that way:

mysql_query("UPDATE cart SET quantity = $q WHERE sessionid='" .session_id(). "' AND description = '$d'") or die(mysql_error());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文