Jump to content

MySQL error while trying to insert using PHP


asimac

Recommended Posts

Hello -  I am using php to read a tab delimited file which has product information in it.  I get an error when I try to insert into mysql.  Here's the error -

 

Error: 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 'Carat Diamond 14k White Gold Stackable Eternity Wedding Band, 1)' at line 2

 

I am on mysql version 5.0.5

 

Here's the SQL I am using to read the file and try to insert into mysql

 


foreach ($lines as $line_num => $line) {
  
if($line_num > 1) {
                $arr = explode("\t", $line);

sql="INSERT INTO buycom (ProductID, Code, ModelNo, Name, Quantity)
	VALUES ($line_num, $arr[0], $arr[1], $arr[2], $arr[3])";

	if (!mysql_query($sql,$con))
	{
		die('Error: ' . mysql_error());
	}
	echo "1 record added";
}

 

Here's an example of the data that I am entering -

Code        ModelNo      Name                                                                                                  QTY

442-3850 1000483 1/3 Carat Diamond 14k White Gold Stackable Eternity Wedding Band 1

 

Any ideas on how to fix this error?

 

Also, when I try to insert the Product Code only -3850 is getting inserted.  any ideas?

 

Thanks

Link to comment
Share on other sites

I suggest that you single-quote and escape every value.

 

$sql="INSERT INTO buycom (ProductID, Code, ModelNo, Name, Quantity)";
$sql .= "VALUES ('".mysql_real_escape_string($line_num)."','". mysql_real_escape_string($arr[0])."','". mysql_real_escape_string($arr[1])."','". mysql_real_escape_string($arr[2])."','". mysql_real_escape_string($arr[3])."')";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.