I have the following code. I am trying to import a text file in to sql table using php as suggested by one of the users on this site.
Unfortunately my import got errors half way due to my text file having "Max and Min" words in the file.
I tried to find out what i can do to avoid it. Most of the stuff i found was about using reserved words in the column name. But mine is not a column name it is inserted in the columns as data.
Can this be avoided as I don't know how many other reserved words are present in the text file and I need to make my code run automatically everyday. I can't have it aborting every time. It is a huge text file so I can't manually replace keywords everytime either.
mysqli_query("CREATE TABLE IF NOT EXISTS `add_feature_id` (
`id_f` INT(10) unsigned NOT NULL AUTO_INCREMENT,
`id_product` INT(10) unsigned NOT NULL,
`id_feature` INT(10) unsigned NOT NULL,
`value` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL
DEFAULT NULL,PRIMARY KEY ( `id_f` ) )",$conn);
$fd = fopen('trial.txt', 'r');
$fheader = fgets($fd);
while (($data = fgetcsv($fd,0, "~")) !== FALSE) {
$id_product = $data[0];
$id_feature = $data[1];
$unitval = $data[2];
$value = mysql_real_escape_string($unitval);
mysqli_query("INSERT INTO `add_feature_id`(`id_product`,`id_feature`,`value`)
VALUES ($id_product,$id_feature,'$value')",$conn) or die(mysql_error());
}
fclose($fd);
$result = mysqli_query("SELECT * FROM `add_feature_id`",$conn);
//I print my result here but i get error while insert is executed
The error i am getting is
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 ' Max and Min Requirements
Low entry level requirement Minimu' at line 2
This is the part of my text file where the error occurs Max and Min Requirements as can be seen in the text file below
IMSKU~AttributeID~Value~Unit~StoredValue~StoredUnit(header row)
1006854 ~ 16257 ~Licensing Program: Max and Min Requirements<ul><li>Low entry level requirement</li><li>Minimum 1 server (Band S) OR 5 desktop (Band A)</li></ul> ~ ~ 0.00 ~