-6

I have a table named ORDER with the title order_date where I need to put the date.

function NewOrderID()
    {    
        $date = date('Y-m-d H:i:s');
        $t = "INSERT INTO order (order_date) 
              VALUES (' ".$date." ')";
          $query = sprintf($t);    
          $result = mysql_query($query);                        
          if (!$result)
          die(mysql_error());
          return  mysql_insert_id();
    }

and I got the following 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 'order ('order_date')
 VALUES (' 2014-04-29 15:02:40 ')' at line 1

Any ideas where I messed up with the query?

metamorph_online
  • 204
  • 2
  • 11

2 Answers2

4

Order is reserved word in MySQL, you can use back tick(`) around table name

Also you can use MySQL NOW() function for date instead of PHP variable $date

$t = "INSERT INTO `order` (order_date) VALUES (NOW())";
PravinS
  • 2,640
  • 3
  • 21
  • 25
0

Firstly, You are using mysql_* functions which are now deprecated and will be removed from PHP in the future. So you need to start using MySQLi or PDO instead.

Your getting MYSQL error because :

order is reserved keyword of MYSQL so you have to use back ticks ` around your tablename :

$t = "INSERT INTO `order` (order_date) VALUES ('".$date."')";
Rakesh Shetty
  • 4,548
  • 7
  • 40
  • 79