...

View Full Version : UPDATE statement not working correctly



chornbeck
07-22-2008, 05:54 PM
I have the following UPDATE statement:


$result=MYSQL_QUERY("UPDATE WRGleads SET date2 = '$date2', time2 = '$time2' WHERE phone = '$phonecheck'") or


$phonecheck is a valid variable that can be echoed later in the page. If I remove the WHERE clause, the statement works, updating all rows in the table with the new date and time.

I simply want to only update the rows that are validated by the WHERE clause - this is not working for some reason - can anyone shed any light on this?

Thanks!
Chris

oesxyl
07-22-2008, 06:00 PM
$query = "UPDATE WRGleads SET date2 = '".$date2."', time2 = '".$time2."' WHERE phone = '".$phonecheck."'";
$result=MYSQL_QUERY($query) or ....


regards

chornbeck
07-22-2008, 06:21 PM
Thanks for the help! Can anyone tell me WHY this solution works, while mine does not? Is it a matter of the variables being passed incorrectly?

Thanks again!

oesxyl
07-22-2008, 06:36 PM
Thanks for the help! Can anyone tell me WHY this solution works, while mine does not? Is it a matter of the variables being passed incorrectly?

Thanks again!
I apologize, is my fault. Is a matter of quating. The variables between ' are not expanded, only between ". for example:



$myvar = "this var";
echo '$myvar'; // don't work
echo "$myvar"; // this work


regards

chornbeck
07-22-2008, 07:57 PM
Thanks for the explanation!

chaosprime
07-23-2008, 02:30 PM
Well, except that's not what's going on. Since your outer quotes are double quotes, you'll get variable interpolation regardless of the fact that single quotes appear inside them. You can test that with this:



<?
$foo = 'hi';
echo "'$foo'\n";
?>


That will output 'hi' (with the quotes). There actually is no difference between your original query and the solution posted. This is easily tested as well:



<?
$date2 = 'second date';
$time2 = 'second time';
$phonecheck = '555-1212';
$query1 = "UPDATE WRGleads SET date2 = '".$date2."', time2 = '".$time2."' WHERE phone = '".$phonecheck."'";
$query2 = "UPDATE WRGleads SET date2 = '$date2', time2 = '$time2' WHERE phone = '$phonecheck'";
echo $query1 . "<br />\n";
echo $query2 . "<br />\n";
?>


You will get two identical lines of output. If one worked and the other didn't, it's because of some third factor not in evidence.

Sorry to, I'm sure, add to the confusion, but it's not going to help you any to go through life with mistaken ideas about string quoting because of this.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum