Inserting CURDATE() and CURTIME() in MySQL (PHP)

16,174

Solution 1

date and time are reserved words of mysql and hence should be quoted with backticks to avoid conflicting with reserved words.

"INSERT INTO `ordisp` (`date`, `time`, operator, ....

Solution 2

You have a syntax error - a comma on the end of the query:

'".$name."', '".$email."',)";
                         ^ here

Remove it, so the full query becomes:

$query = "INSERT INTO `ordisp` (date, time, operator, status, completed, name, email) VALUES (CURDATE(), CURTIME(), '".$operator."', '".$status."', '".$complete."', '".$name."', '".$email."')";

For future debugging you should look at the MySQL error, it will give you a description of the problem, for example unknown field/table name or a syntax error etc etc.

Share:
16,174

Related videos on Youtube

I wrestled a bear once.
Author by

I wrestled a bear once.

Yes I know the band sucks.

Updated on June 04, 2022

Comments

  • I wrestled a bear once.
    I wrestled a bear once. almost 2 years

    How do you insert CURDATE() and CURTIME() in MySQL?

    I am inserting them into date and time fields, respectively in the DB table.

    $query = "INSERT INTO `ordisp` (date, time, operator, status, completed, name, email) VALUES (CURDATE(), CURTIME(), '".$operator."', '".$status."', '".$complete."', '".$name."', '".$email."')";
    

    Why isn't this working?

    • Rudi Visser
      Rudi Visser over 11 years
      What isn't working? Do you have any errors? Is it perhaps because of your trailing ,? Or perhaps because of reserved word usage?
    • Bhavik Shah
      Bhavik Shah over 11 years
      Please put errors that you get while using above mentioned code
  • Bhavik Shah
    Bhavik Shah over 11 years
    I dont think so. Refer this from Official MySql documentation. Please update your answer before it gets down-voted.
  • MrCode
    MrCode over 11 years
    date and time are not reserved words. See dev.mysql.com/doc/refman/5.5/en/reserved-words.html
  • I wrestled a bear once.
    I wrestled a bear once. over 11 years
    I am using mysql 5.0, not 5.5. the backticks fixed it. Thanks user!
  • sunlight
    sunlight over 11 years
    @MrCode: True, These are no longer reserved words in mysql 5.5. I made a guess that he might be running on mysql 5.1
  • MrCode
    MrCode over 11 years
    The backticks alone couldn't have solved this, even on MySQL 5.0 because of the syntax error. Strange - even the 5.0 docs say date and time are not reserved: dev.mysql.com/doc/refman/5.0/en/reserved-words.html