使用 MySQL 更新多个变量 + PHP

发布于 2024-10-15 05:12:07 字数 1355 浏览 3 评论 0原文

我有两个包含文本字符串的变量。我需要更新表中的它们,但是在我尝试过的大约 5 个不同脚本的 20 多个不同变体中,它就是没有更新!

我可以使用此更新:

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

但是我现在正在不同的页面上工作,我需要稍微不同的更新查询。这是:

UPDATE cart SET quantity = $q WHERE sessionid = $somethin AND description = $desc

为此,我有:(对于

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

上述查询,我​​在不同地方尝试了许多带有不同引号的变体,但没有任何效果!)

我也尝试过:

$conn = mysql_connect("my01..com", "dbase", "2354ret345ert");
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
$sql = 'UPDATE cart
        SET quantity="'.$q.'"
        WHERE sessionid="$o" AND description = "$d"';

mysql_select_db('mysql_94569_dbase');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
mysql_close($conn);  

最后一个不显示任何错误,事实上,它甚至告诉我它已成功更新!但这是在撒谎。它没有更新任何东西。

有人可以帮我吗,我真的厌倦了在教程之后阅读教程,却从未学到任何东西,因为它们都有不同的语法,而且似乎都不起作用。

我想做的是:

UPDATE table SET columnname = $this WHERE thiscolumn = $this AND thiscolumn = $that

$this = $var

谢谢

I have 2 variables that contain a a string of text. I need to update them in the table, but out of the 20 + different variations of about 5 different scripts that I've tried out, it just doesn't update!

I can update using this:

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

but I am now working on a different page, where I need a slightly different update query. Which is:

UPDATE cart SET quantity = $q WHERE sessionid = $somethin AND description = $desc

And for that I have:

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

(I have tried many variations with different quotes in different places for the above query, but nothing works!)

I have also tried:

$conn = mysql_connect("my01..com", "dbase", "2354ret345ert");
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
$sql = 'UPDATE cart
        SET quantity="'.$q.'"
        WHERE sessionid="$o" AND description = "$d"';

mysql_select_db('mysql_94569_dbase');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
mysql_close($conn);  

That last one doesn't display any errors, in fact, it even tells me that it has successfully updated! But it's lying. It hasn't updated anything.

Can someone please help me out here, I am really getting sick of reading tutorial after turorial and never learning anything because they all have differnt syntax and none of it seems to work.

What I would like to do is:

UPDATE table SET columnname = $this WHERE thiscolumn = $this AND thiscolumn = $that

$this = $var

Thank you

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

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

发布评论

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

评论(3

肤浅与狂妄 2024-10-22 05:12:07

您缺少描述和 SessionID 中的引号,请执行以下操作:

 mysql_query("UPDATE cart
              SET quantity = '".$q."'
              WHERE sessionid = '".$o."' AND description = '".$d."'");

You are missing the quotes in description and SessionID, do it like this:

 mysql_query("UPDATE cart
              SET quantity = '".$q."'
              WHERE sessionid = '".$o."' AND description = '".$d."'");
〆一缕阳光ご 2024-10-22 05:12:07

为了避免您的困惑,我建议开始使用串联运算符(例如“UPDATE '.$table .' SET ...”),而不是直接将变量写入字符串(例如“UPDATE $table SET ...”) 。
在这种情况下,您的查询将如下所示:

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

这可能会帮助您更快地找到引号和括号的问题

In order to save you confusion, I would recommend start using concatenation operator (eg 'UPDATE '.$table .' SET ...')instead of writing variables directly to strings (eg. "UPDATE $table SET ...").
in this case your query would look like:

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

This might help you to find problems with quotes and parenthesis quicker

请叫√我孤独 2024-10-22 05:12:07

坏的:
我在 php 中有这样的查询:

$query = "UPDATE users SET username = ".$nume." WHERE id = ".$userID;

做了这个 SQL:

UPDATE users SET username = elev WHERE id = 2

好: 为了让它工作,我将其更改为这个 php:

$query = "UPDATE users SET username = ".'"'.$nume.'"'." WHERE id = ".$userID;

做了这个 SQL:

UPDATE users SET username = "elev" WHERE id = 2 

BAD:
I had this query in php:

$query = "UPDATE users SET username = ".$nume." WHERE id = ".$userID;

That did this SQL:

UPDATE users SET username = elev WHERE id = 2

GOOD: For it to work I changed it to this php:

$query = "UPDATE users SET username = ".'"'.$nume.'"'." WHERE id = ".$userID;

That did this SQL:

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