Monday, 19 August 2013

SQL syntax error in mysql

SQL syntax error in mysql

I'm inserting value to my MySQL table from php as:
$journey = $_POST['way'];
$from = $_POST['from'];
$to = $_POST['to'];
$dpdt = $_POST['dp_date'];
$rtdt = $_POST['rt_date'];
$fare = $_POST['fare'];
$sql = "insert into tours set " .
"journey='$journey', from='$from', to='$to', dp_date=CAST('$dpdt'
AS DATE), " .
"rt_date=CAST('$rtdt' AS DATE), fare='$fare'";
on trying echo for $sql I'm getting output as:
insert into tours set journey='round', from='Aurangabad', to='Kashmir',
dp_date=CAST('27-08-2013' AS DATE), rt_date=CAST('21-08-2013' AS DATE),
fare='2500'
but I'm continuously getting the same error message:
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
'from=Aurangabad, to='Kashmir', dp_date=CAST('27-08-2013' AS DATE),
rt_date=CAST(' at line 1
even if I try to remove ' around the values of column names.
I'm using the same syntax for inserting data and that's working fine.
What's wrong with this?

No comments:

Post a Comment