Cannot update record when inserting to datetime

Discussion in 'MySQL' started by Breakpoint, Jan 14, 2010.

  1. #1
    I have the sql created in a variable and I send it to the mysql database to update a field, but I get an error with the query. I was curious why I can't update the datetime field in my database.

    $query = "UPDATE site_games SET release = '2010-01-01 23:32:45' WHERE id = 1";
    $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());

    This returns

    Error in query: UPDATE site_games SET release = '2010-01-01 23:32:45' WHERE id = 1. 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 'release = '2010-01-01 23:32:45' WHERE id = 1' at line 1


    Anyone know how to fix this?
     
    Breakpoint, Jan 14, 2010 IP
  2. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #2
    $query = "UPDATE site_games SET release = '2010-01-01 23:32:45' WHERE id = '1'";
     
    crivion, Jan 15, 2010 IP
    Breakpoint likes this.
  3. Breakpoint

    Breakpoint Peon

    Messages:
    164
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    That doesn't work.

    Not usre what the problem is, id is an int(11) and release is a datetime set to be null.

    I wish the warnings were more descriptive ;/
     
    Last edited: Jan 16, 2010
    Breakpoint, Jan 16, 2010 IP
  4. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #4
    release is a MySQL reserve word. It is not a good practice to use reserve words for your db name, table and column names. Apply back ticks if you want to use that
    $query = "UPDATE site_games SET `release` = '2010-01-01 23:32:45' WHERE id = 1";
    Code (markup):
     
    mwasif, Jan 16, 2010 IP
    Breakpoint likes this.
  5. WebWorth

    WebWorth Greenhorn

    Messages:
    89
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    20
    #5
    UPDATE `db_name`.`site_games` SET `release` = '2010-01-01 23:32:45' WHERE `site_games`.`id` =1 LIMIT 1 ;

    try that release db_name with the db name
     
    WebWorth, Jan 17, 2010 IP
  6. WebWorth

    WebWorth Greenhorn

    Messages:
    89
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    20
    #6
    or just do the update in phpmyadmin and grab the sql from there (if you have it)
     
    WebWorth, Jan 17, 2010 IP
    Breakpoint likes this.
  7. Breakpoint

    Breakpoint Peon

    Messages:
    164
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    yep I figured out it was because of the name release, thanks for your help guys. That error sucked and took me like 12 hours to figure out :(

    rep for all :)
     
    Breakpoint, Jan 18, 2010 IP
  8. fourdesign

    fourdesign Member

    Messages:
    26
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #8
    Yeah it's good practice to surround table names and field names with ``. This will prevent lots of confusion when doing complex queries... But of course do what you feel comfortable with because it's only a suggestion.
     
    fourdesign, Jan 26, 2010 IP