asimac Posted October 3, 2010 Share Posted October 3, 2010 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 https://forums.phpfreaks.com/topic/215069-mysql-error-while-trying-to-insert-using-php/ Share on other sites More sharing options...
BlueSkyIS Posted October 3, 2010 Share Posted October 3, 2010 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 https://forums.phpfreaks.com/topic/215069-mysql-error-while-trying-to-insert-using-php/#findComment-1118644 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.