-3

I am unable to understand what is keeping this code to insert data into mysql.. I am continuesly having this error while trying to inset the code "Insert Problem: 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 (f_name) values ('')' at line 1" Please HELP. Please use a simplified language as I am quite new in this field.

<div class="order_form"><form action="buy.php" method="post">

<table width="330" border="0">
<tr>
<td colspan="2" height="30" bgcolor="#705a6b"><div align="center" class="style4">Order Foram 

</div></td>
</tr>
<tr>
<td width="102" height="30">First Name* </td>
<td width="227" style="border:solid; border-color:#CC99FF; border-width:1px; "><input name="f_name" type="text" size="40" maxlength="100"/></td>
</tr>
<tr>
<td height="30"><input name="submit" type="submit"/></td>
</tr>

</table>
</form>     

<?php 
if (isset($_POST['submit']))
{

$f_name = mysql_real_escape_string($_POST['f_name']);

$que="insert into order (f_name) values ('$f_name')";   

if (mysql_query($que))

{

echo "say something";

}

else {

die("Insert Problem: " . mysql_error());     

}
}

?>
</div>          
Michael Irigoyen
  • 22,513
  • 17
  • 89
  • 131
  • looks like `$f_name` is `''` – karthikr May 15 '13 at 16:53
  • 2
    `order` is a reserved word in SQL. If you really need to use it, quote it out with backticks. But you should avoid using reserved words as table and column names if at all possible. – andrewsi May 15 '13 at 16:53
  • Add the following lines of code... `$que="insert into order (f_name) values ('$f_name')";echo($que); return;` And tell us what echoes out. This will make your problem much easier to solve. – Code Whisperer May 15 '13 at 16:53
  • Duplicate of [How can I write SQL for a table that shares the same name as a protected keyword in MySql?](http://stackoverflow.com/q/10706920/1409082) and many other questions... – Jocelyn May 15 '13 at 16:59

2 Answers2

2

ORDER is a reserved word in MySQL and needs to be surrounded by backticks to be used as table or column names;

$que="insert into `order` (f_name) values ('$f_name')"; 
Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
  • 1
    Thank you so much Bro.. I used to get irritated when I search something and get maximum number of results from "stackoverflow" now I feel guilty on that... Thank you once again for saving my time. – user2386781 May 15 '13 at 17:04
0

Change you query to this:

$que="insert into `order` (`f_name`) values ('$f_name')";  

Note the backtickes. some words like order is reserved in sql queries.

SAVAFA
  • 818
  • 8
  • 23