简体   繁体   English

使用MySQL + PHP更新多个变量

[英]Updating with multiple variables with MySQL + PHP

I have 2 variables that contain aa string of text. 我有2个包含一个文本字符串的变量。 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! 我需要在表中更新它们,但是在我尝试的大约5种不同脚本的20种不同版本中,它只是没有更新!

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 谢谢

You are missing the quotes in description and SessionID, do it like this: 您缺少描述和SessionID中的引号,请执行以下操作:

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

BAD: I had this query in php: 不好:我在php中有这个查询:

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

That did this SQL: 这样做的SQL:

UPDATE users SET username = elev WHERE id = 2

GOOD: For it to work I changed it to this php: 良好:为了使其正常工作,我将其更改为以下php:

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

That did this SQL: 这样做的SQL:

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

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 ..."). 为了避免造成混乱,我建议开始使用串联运算符(例如'UPDATE'。$ table。'SET ...'),而不是直接将变量写入字符串(例如“ 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 这可以帮助您更快地找到引号和括号中的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM