Get mysql error while inserting data

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ghjk
    Contributor
    • Jan 2008
    • 250

    Get mysql error while inserting data

    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
    Code:
    <?php
    
    	if(isset($_POST['Submit'])){ 
    	$VehicleNo = $_POST['VehicleNo'];
    	$Type= $_POST['Type'];
    	$Make=$_POST['Make'];
    	$Model=$_POST['Model'];
    	$Price=$_POST['Price'];
    	$OwnerName=$_POST['OwnerName'];
    	$ContactNo=$_POST['ContactNo'];
    	$Email=$_POST['Email'];
    	$Other=$_POST['Other'];
    
    	// selected and uploaded a file
    	if (($_FILES['image']) && $_FILES['image']['size'] > 0) { 
    
        // Temporary file name stored on the server
        $tmpName  = $_FILES['image']['tmp_name'];  
           
        // Read the file 
        $fp      = fopen($tmpName, 'r');
        $data = fread($fp, filesize($tmpName));
        $data = addslashes($data);
        fclose($fp);
    	
          // Create the query and insert
          // into our database.
          //$query = "INSERT INTO tbl_images ";
          //$query .= "(image) VALUES ('$data')";
          //$results = mysql_query($query, $link);
          
          // Print results
          //print "Thank you, your file has been uploaded.";
          
    	}
    //
    	$sql = "INSERT INTO vehicles('$VehicleNo','$data','$Price','$OwnerName','$ContactNo','$Email','$Type','$Make','$Model','$Other')"; 
    	echo $sql;
    	$result = mysql_query(sql);
    	if (!$result) 
    		{
    			echo "Error in SQL query: " . mysql_error();
    		}
    		else{
    			echo "Data successfully inserted!"; 
    		}
    	}
    	// main else
    	else{
    ?>
    
    <table width="800" align="center" height="200" border="0" cellspacing="0" bgcolor="#EBE9ED">
      <tr>
        <td colspan="3"><?php //require_once 'common/hedder.php';?></td>
        </tr>
    	<tr>
    		<td width="200" colspan="2"><?php require_once 'common/left.php'; ?></td>
    	</tr>
    	<tr><td bgcolor="#EBE9ED">
    	<form action="addVehicle.php" method="post" name="changer">
    		<table align="center" bgcolor="#EBE9ED"  width="650" ><tr><td>
    		<tr>
    			<td>Vehicle No</td>
    			<td><input name="VehicleNo" type="text" /></td>
    			<td>Image</td>
    			<td>
    			<input name="MAX_FILE_SIZE" value="102400" type="hidden">
    			<input name="image" accept="image/jpeg" type="file">
    			</td>
    		</tr>
    		<tr>
    			<td>Type</td>
    			<td><?php GetVehicleType(); ?></td>
    			<td>Vehicle Make</td>
    			<td><?php GetVehicleMake(); ?> </td>
    		</tr>
    		<tr>
    			<td>Model</td>
    			<td><input type="text" name="Model"></td>
    			<td>Year</td>
    			<td><input name="Year" type="text" /></td>
    		</tr>
    		<tr>
    			<td>Price</td>
    			<td><input name="Price" type="text" /></td>
    			<td>Owner Name</td>
    			<td><input name="OwnerName" type="text" /></td>
    		</tr>
    		<tr>
    			<td>Contact Number</td>
    			<td><input name="ContactNo" type="text" /></td>
    			<td>Email</td>
    			<td><input name="Email" type="text" /></td>
    		</tr>
    		<tr><td colspan="4">
    			<table><tr>
    			<td>Other</td>
    			<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    			<textarea name="Other" cols=48 rows=3 ></textarea></td>
    			</tr></table></td>
    		</tr>
    		<tr><td align="right" colspan="4"><input type="submit"  class="btn" value="Submit" alt="Submit" height="30"  width="50" name="Submit"/></td></tr>
    	</td></tr></table>
    	</form>
    	</td></tr>
        <tr>
        <td colspan="3" align="center"><?php require_once 'common/footer.php'; ?></td>
        </tr>
    </table>
    <?php } //close main else?>
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

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

    Comment

    • ghjk
      Contributor
      • Jan 2008
      • 250

      #3
      Thank You very much.

      Comment

      • ghjk
        Contributor
        • Jan 2008
        • 250

        #4
        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','B MW','dsfs','fdg ')' at line 1

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          The INSERT query syntax is supposed to be:
          [code=sql]
          /* Either */
          INSERT INTO `table`(`col1`, `colN`) VALUES('val1', 'valN')
          /* Or */
          INSERT INTO `table` VALUES(NULL, 'val1', 'valN')[/code]
          (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
          [code=sql]INSERT INTO `table`('val1', 'valN')[/code]

          Comment

          Working...