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 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jan 2008
    Posts
    334
    Thanks
    9
    Thanked 0 Times in 0 Posts

    INSERT mysql query

    Whats wrong with this query?

    Code:
    mysql_query("INSERT INTO users (`last_ip`) VALUES ('$new_ip') WHERE user_id='".$active['user_id']."' ") or die('Server Error: ' . mysql_error());
    I get this syntax error:

    Server Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE user_id='1'' at line 1

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,121
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    You can't use a WHERE clause with an INSERT. Period.

    Doesn't make sense: You are creating a *NEW* record. Completely new. From scratch.

    A WHERE clause is always used to select some *existing* record or records.

  • #3
    Regular Coder
    Join Date
    Jan 2008
    Posts
    334
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Okay, so I would have to use the update, right?

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,121
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Dunno. Only you can tell us.

    If the record does not exist, you can't update it. Then you must insert it. But yes, if it exists you must update.


  •  

    Posting Permissions

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