By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,086 Members | 1,442 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,086 IT Pros & Developers. It's quick & easy.

Get mysql error while inserting data

100+
P: 250
I wan to add some data using php page to mysql db. But I got the error saying "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 'sql' at line 1". Could someone please help me.
This is my php code
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.     if(isset($_POST['Submit'])){ 
  4.     $VehicleNo = $_POST['VehicleNo'];
  5.     $Type= $_POST['Type'];
  6.     $Make=$_POST['Make'];
  7.     $Model=$_POST['Model'];
  8.     $Price=$_POST['Price'];
  9.     $OwnerName=$_POST['OwnerName'];
  10.     $ContactNo=$_POST['ContactNo'];
  11.     $Email=$_POST['Email'];
  12.     $Other=$_POST['Other'];
  13.  
  14.     // selected and uploaded a file
  15.     if (($_FILES['image']) && $_FILES['image']['size'] > 0) { 
  16.  
  17.     // Temporary file name stored on the server
  18.     $tmpName  = $_FILES['image']['tmp_name'];  
  19.  
  20.     // Read the file 
  21.     $fp      = fopen($tmpName, 'r');
  22.     $data = fread($fp, filesize($tmpName));
  23.     $data = addslashes($data);
  24.     fclose($fp);
  25.  
  26.       // Create the query and insert
  27.       // into our database.
  28.       //$query = "INSERT INTO tbl_images ";
  29.       //$query .= "(image) VALUES ('$data')";
  30.       //$results = mysql_query($query, $link);
  31.  
  32.       // Print results
  33.       //print "Thank you, your file has been uploaded.";
  34.  
  35.     }
  36. //
  37.     $sql = "INSERT INTO vehicles('$VehicleNo','$data','$Price','$OwnerName','$ContactNo','$Email','$Type','$Make','$Model','$Other')"; 
  38.     echo $sql;
  39.     $result = mysql_query(sql);
  40.     if (!$result) 
  41.         {
  42.             echo "Error in SQL query: " . mysql_error();
  43.         }
  44.         else{
  45.             echo "Data successfully inserted!"; 
  46.         }
  47.     }
  48.     // main else
  49.     else{
  50. ?>
  51.  
  52. <table width="800" align="center" height="200" border="0" cellspacing="0" bgcolor="#EBE9ED">
  53.   <tr>
  54.     <td colspan="3"><?php //require_once 'common/hedder.php';?></td>
  55.     </tr>
  56.     <tr>
  57.         <td width="200" colspan="2"><?php require_once 'common/left.php'; ?></td>
  58.     </tr>
  59.     <tr><td bgcolor="#EBE9ED">
  60.     <form action="addVehicle.php" method="post" name="changer">
  61.         <table align="center" bgcolor="#EBE9ED"  width="650" ><tr><td>
  62.         <tr>
  63.             <td>Vehicle No</td>
  64.             <td><input name="VehicleNo" type="text" /></td>
  65.             <td>Image</td>
  66.             <td>
  67.             <input name="MAX_FILE_SIZE" value="102400" type="hidden">
  68.             <input name="image" accept="image/jpeg" type="file">
  69.             </td>
  70.         </tr>
  71.         <tr>
  72.             <td>Type</td>
  73.             <td><?php GetVehicleType(); ?></td>
  74.             <td>Vehicle Make</td>
  75.             <td><?php GetVehicleMake(); ?> </td>
  76.         </tr>
  77.         <tr>
  78.             <td>Model</td>
  79.             <td><input type="text" name="Model"></td>
  80.             <td>Year</td>
  81.             <td><input name="Year" type="text" /></td>
  82.         </tr>
  83.         <tr>
  84.             <td>Price</td>
  85.             <td><input name="Price" type="text" /></td>
  86.             <td>Owner Name</td>
  87.             <td><input name="OwnerName" type="text" /></td>
  88.         </tr>
  89.         <tr>
  90.             <td>Contact Number</td>
  91.             <td><input name="ContactNo" type="text" /></td>
  92.             <td>Email</td>
  93.             <td><input name="Email" type="text" /></td>
  94.         </tr>
  95.         <tr><td colspan="4">
  96.             <table><tr>
  97.             <td>Other</td>
  98.             <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  99.             <textarea name="Other" cols=48 rows=3 ></textarea></td>
  100.             </tr></table></td>
  101.         </tr>
  102.         <tr><td align="right" colspan="4"><input type="submit"  class="btn" value="Submit" alt="Submit" height="30"  width="50" name="Submit"/></td></tr>
  103.     </td></tr></table>
  104.     </form>
  105.     </td></tr>
  106.     <tr>
  107.     <td colspan="3" align="center"><?php require_once 'common/footer.php'; ?></td>
  108.     </tr>
  109. </table>
  110. <?php } //close main else?>
  111.  
Sep 24 '09 #1

✓ answered by Atli

Hey.

Line #39. You forgot the $ in the variable name.

Share this Question
Share on Google+
4 Replies


Atli
Expert 5K+
P: 5,058
Hey.

Line #39. You forgot the $ in the variable name.
Sep 24 '09 #2

100+
P: 250
Thank You very much.
Sep 24 '09 #3

100+
P: 250
Correct it. But still got an error.
Error in SQL query: 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 ''gfdg','','gdf','fgdf','gdf','hgf','Jeep','BMW',' dsfs','fdg')' at line 1
Sep 24 '09 #4

Atli
Expert 5K+
P: 5,058
The INSERT query syntax is supposed to be:
Expand|Select|Wrap|Line Numbers
  1. /* Either */
  2. INSERT INTO `table`(`col1`, `colN`) VALUES('val1', 'valN')
  3. /* Or */
  4. INSERT INTO `table` VALUES(NULL, 'val1', 'valN')
(It's best to use the first version. Allows you to change the table later without having to alter all your queries as well.)

You just have
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO `table`('val1', 'valN')
Sep 24 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.