3

This is my Coding :

session_start();
include 'Connect.php';
$userid=$_SESSION['userid'];
$tanggal=date('Y-m-d h:i:s');
$status='Pending';  

echo $userid;
echo $status;
echo $tanggal;

mysql_query("INSERT INTO 'Order' (IdPelanggan,Tanggal,StatusOrder) VALUES ('$userid', '$tanggal', '$status')") or die (mysql_error());

After i run that coding, i got this error :

 `9Pending2013-07-27 11:25:54You 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 (IdPelanggan,Tanggal,StatusOrder) VALUES (9, 2013-07-27 11:25:54, Pending)' at line 1`

I wonder where my mistakes is..

1 Answers1

4

If you want to escape reserved words in MySQL like order then do it with backticks and not with quotes:

INSERT INTO `Order` ...

Quotes are string delimiters.

juergen d
  • 201,996
  • 37
  • 293
  • 362
  • I also suggest to not use reserved words for table names... it is bad practice and will cause confusion for others later. – amaster Jul 27 '13 at 21:36
  • Thanks, the error for Order is gone : and i got this error : `9Pending2013-07-27 11:36:26You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '11:36:26, Pending)' at line 1` – Yah... gt Last Name Jul 27 '13 at 21:37
  • @Yah...gtLastName can you please verify this line `'$tanggal', '$status'` in your code and make sure it is not similar to `'$tanggal, $status` – amaster Jul 27 '13 at 21:43
  • @Yah...gtLastName: What data type has `Tanggal` in your DB Table? I guess it is `date`. But you are trying to insert a `datetime`. That won't work. – juergen d Jul 27 '13 at 21:51
  • @Yah...gtLastName: If I am right with my guess then change `$tanggal=date('Y-m-d h:i:s');` to `$tanggal=date('Y-m-d');` – juergen d Jul 27 '13 at 22:01
  • Solved, it seems that i need to change '$tanggal' and '$status' into '".$tanggal."' and '".$tanggal."' Thanks a lot for ur help ^^ – Yah... gt Last Name Jul 27 '13 at 22:30