473,396 Members | 1,895 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Get mysql error while inserting data

250 100+
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.

4 2866
Atli
5,058 Expert 4TB
Hey.

Line #39. You forgot the $ in the variable name.
Sep 24 '09 #2
ghjk
250 100+
Thank You very much.
Sep 24 '09 #3
ghjk
250 100+
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
5,058 Expert 4TB
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

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

Similar topics

1
by: Agathe | last post by:
Bonjour, Je souhaite insérer dans une table MySQL des données provenant d'un fichier texte grâce à un script PHP. Mon fichier porte l'extension "txt" et les données sont séparées par des ";'. ...
8
by: news | last post by:
I'm building an e-commerce site, where the owner needs to be able to upload a CSV file of product that gets inserted into mySQL. Unfortunately, I think my Web host may have some functions turned...
2
by: moller | last post by:
Im looking in to the possibility of moving from mySQL to an access database. My reasons are: (1) Database is single user. (2) Database local on users PC. (3) Database has only 8 tables where 4...
3
by: Taras_96 | last post by:
Hi everyone, I'm having a bit of trouble understanding the purpose of escaping nulls, and the use of addcslashes. Firstly, the manual states that: "Strictly speaking, MySQL requires only...
8
by: The Natural Philosopher | last post by:
This is so weird. What I am trying to do is to upload files and stuff them in a mysql database. Everything works except the file content is zero. using the load_file command from mysql...
30
by: Einstein30000 | last post by:
Hi, in one of my php-scripts is the following query (with an already open db-connection): $q = "INSERT INTO main (name, img, descr, from, size, format, cat, host, link, date) VALUES ('$name',...
1
by: paulq182 | last post by:
PLEASE HELP ME WITH MY CODE?? import java.sql.*; import java.io.*; class min_filmdb_rel_mysql { public static void main (String args ) throws SQLException, IOException {
0
by: Edwin.Madari | last post by:
-----Original Message----- statement prepared first and executed many times with exectemany - db API http://www.python.org/dev/peps/pep-0249/ inline statemets can be exeucuted only. hope that...
4
by: toadstool | last post by:
if (IN PHP) I call $cart->insert_Pcart($item1, $item2); class Cart { : function insert_Pcart($item_name,$p_qty) { $insert = "INSERT INTO Cart (pname, qty)
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.