使用 MySQL 更新多个变量 + PHP
我有两个包含文本字符串的变量。我需要更新表中的它们,但是在我尝试过的大约 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您缺少描述和 SessionID 中的引号,请执行以下操作:
You are missing the quotes in description and SessionID, do it like this:
为了避免您的困惑,我建议开始使用串联运算符(例如“UPDATE '.$table .' SET ...”),而不是直接将变量写入字符串(例如“UPDATE $table SET ...”) 。
在这种情况下,您的查询将如下所示:
这可能会帮助您更快地找到引号和括号的问题
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:
This might help you to find problems with quotes and parenthesis quicker
坏的:
我在 php 中有这样的查询:
做了这个 SQL:
好: 为了让它工作,我将其更改为这个 php:
做了这个 SQL:
BAD:
I had this query in php:
That did this SQL:
GOOD: For it to work I changed it to this php:
That did this SQL: