Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Feb 2006
    Posts
    138
    Thanks
    11
    Thanked 0 Times in 0 Posts

    UPDATE statement not working correctly

    I have the following UPDATE statement:

    Code:
    $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

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    PHP Code:
    $query "UPDATE WRGleads SET date2 = '".$date2."', time2 = '".$time2."' WHERE phone = '".$phonecheck."'";
    $result=MYSQL_QUERY($query) or .... 
    regards

  • Users who have thanked oesxyl for this post:

    chornbeck (07-22-2008)

  • #3
    Regular Coder
    Join Date
    Feb 2006
    Posts
    138
    Thanks
    11
    Thanked 0 Times in 0 Posts
    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!

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by chornbeck View Post
    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:

    PHP Code:
    $myvar "this var";
    echo 
    '$myvar'// don't work
    echo "$myvar"// this work 
    regards

  • #5
    Regular Coder
    Join Date
    Feb 2006
    Posts
    138
    Thanks
    11
    Thanked 0 Times in 0 Posts
    Thanks for the explanation!

  • #6
    Regular Coder
    Join Date
    Apr 2008
    Location
    New Jersey
    Posts
    116
    Thanks
    0
    Thanked 29 Times in 29 Posts
    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:

    PHP Code:
    <?
    $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:

    PHP Code:
    <?
    $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.
    Chaos
    Lost Souls: text based RPG | MUDseek: MUD gaming search | MUDfind: MUD resource sites | Discordian Quotes


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •