开发者

Updating with multiple variables with MySQL + PHP

开发者 https://www.devze.com 2023-02-07 18:33 出处:网络
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 ju

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


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."'");


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


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 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号