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 7 of 7
  1. #1
    Regular Coder
    Join Date
    Oct 2003
    Posts
    603
    Thanks
    2
    Thanked 1 Time in 1 Post

    update WHERE clause?

    can this be done? i can't get it to work... i want it to update the row with id that equals $id and update all fields... but it doesnt work.

    PHP Code:
    <?php
    $title 
    $_POST['title'];
    $body $_POST['body'];
    $date $_POST['date'];
    $id $_POST['id'];

    $con mysql_connect("localhost","user","pwd");
    $db mysql_select_db("database",$con);
    $sql "UPDATE `table` SET `topic` = '$title', `body` = '$body', `date` = '$date' WHERE `ids` = '$id'";
    mysql_query($sql) or die("Error: ".mysql_error());
    ?>

  • #2
    Regular Coder
    Join Date
    Sep 2002
    Location
    Scotland
    Posts
    407
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: update WHERE clause?

    dont like what you've done with all those ' marks in the $sql line.... try this:

    PHP Code:
    <?php
    $title 
    $_POST['title'];
    $body $_POST['body'];
    $date $_POST['date'];
    $id $_POST['id'];

    $con mysql_connect("localhost","user","pwd");
    $db mysql_select_db("database",$con);
    $sql "UPDATE table SET topic = ".$title.", body = ".$body.", date = ".$date." WHERE ids = ".$id."";
    mysql_query($sql) or die("Error: ".mysql_error());
    ?>
    My body's a temple... and like those ancient Greek ones it's a ruin
    sweenster.co.uk

  • #3
    Regular Coder
    Join Date
    Oct 2003
    Posts
    603
    Thanks
    2
    Thanked 1 Time in 1 Post
    nvm... i fixed it... it was a problem in my form... i didnt have a </form> echoed in my previous php page... sorry.

  • #4
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    actually the code you just posted sweenster will not work, and his code is more likley to.

    You need to put strings in single quotes, and there is nothing wrong with backquotes around fields as far as I know, I usually use them.

    PHP Code:
    <?php
    error_reporting
    (E_ALL);

    $title $_POST['title'];
    $body $_POST['body'];
    $date $_POST['date'];
    $id $_POST['id'];

    $con mysql_connect("localhost","user","pwd");
    $db mysql_select_db("database",$con);
    $sql "UPDATE `table` SET `topic` = '".$title."', `body` = '".$body."', `date` = '".$date."' WHERE `ids` = '".$id."'";

    echo 
    $sql// print your sql statement to make sure it looks ok

    mysql_query($sql);
    ?>
    Also ensure your input contains no single quotes, as this will cause it not to work if they are not automatically being escaped (\\')

  • #5
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    guess my last post was useless then , but for debugging, its always a good idea to print your SQL statements if your having problems, to make sure the statement is correct, then check DB connection, and then, like you did, html code.

  • #6
    Regular Coder
    Join Date
    Sep 2002
    Location
    Scotland
    Posts
    407
    Thanks
    0
    Thanked 0 Times in 0 Posts
    oops! forgot to put the quotes in around the variables
    (must remember to check it next time).

    Apart from that it should work OK without the quotes around the string names...

    example:
    (from the first but of code i had to hand)

    Code:
    $query = "UPDATE stats SET usernum=\"".$s_userid."\" WHERE id=\"".$statnum."\"";
    My body's a temple... and like those ancient Greek ones it's a ruin
    sweenster.co.uk

  • #7
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes it will work like that, although there is nothing wrong with backquotes

    Also, i usually use single quotes for string values inside of double quotes, as you dont have to escape them.


  •  

    Posting Permissions

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