So I am using php to upload a csv file into an array, de-dupe it, clean it and insert data into a mysql table.
The table (inboundnew
) has 9 fields, one date, one varchar, and 7 integer.
The script processes the file as requested, and builds an 'insert command into a string' which I then run with:
$result= mysql_query($string) or die(mysql_error())
The insert string outputs as:
insert into inboundnew
(ndate,branch,ans,unans,voice,unqueued,actioned,unactioned, total)
values
("2000-01-01","Name of Customer",0,0,0,0,0,0,0);
It is giving me the following 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 '' at line 1
Yet when I run the string through phpmyadmin, it inserts perfectly.
Would be extremely grateful if anyone has any idea what could be going wrong here.
OK - so after two days of banging my brains out on this, I am still having the problem.
I have posted below the code that I run to build the string which I then run in the query. It out puts no apostrophes and yet I am still getting the MySQL error. Apologies if the code is not formatted properly - I am unsure as how to do this here. I have uploaded and opened the CSV file...
while(!feof($file)) {
$lines[] = fgets($file); }
$clean=array();
$clean = array_unique($lines);//remove duplicates
$count=0;
$string="insert into inboundnew (date,branch,ans,unans,voice,unqueued,actioned,unactioned,total) values ";
//now write to table - skipping first 2 lines
enter code here
if ($count > 1) {
$parts=explode(',', $val);
$a=$parts[0];//ignore this field
$b=$parts[1];//ignore this field)
if ($b>""){ //ignore if blank line
$salon=$parts[2];
$ans=$parts[3];
$missed=$parts[4];
$voice=$parts[5];
$unq=$parts[6];
$act=$parts[7];
$unact=$parts[7];
$total=$parts[8];
$date=$date;
$string .= '("'.$date.'","'.$salon.'",'.$ans.','.$missed.','.$voice.','.$unq.','.$act.','.$unact.','.$total.'),'. PHP_EOL;
}
}
$count++;
}
//now remove last comma
$strlen= strlen($string);
$string= substr($string,0, $strlen-2);
This builds the string which fails in the script, but works in PhpMyAdmin and outputs the syntax error