因此,我使用php将csv文件上传到数组中,对其进行去杜撰,清理它并将数据插入mysql表中。
表(inboundnew)有9个字段,一个日期,一个varchar和7个整数。脚本将根据请求处理该文件,并构建一个“insert命令到字符串”,然后使用该命令运行:
$result= mysql_query($string) or die(mysql_error())插入字符串输出如下:
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);它给了我以下错误
您的SQL语法出现了错误;请检查与您的MySQL服务器版本对应的手册,以获得在第1行附近使用的正确语法
然而,当我通过phpmyadmin运行字符串时,它插入得非常完美。
如果有人知道这里可能出了什么问题,我会非常感激的。
好吧--在这件事上花了两天脑筋,我还是有问题。
我在下面发布了我为构建字符串而运行的代码,然后在查询中运行该字符串。它不产生撇号,但我仍然得到MySQL错误。抱歉,如果代码没有正确地格式化--我不确定如何在这里这样做。我上传并打开了CSV文件..。
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 ";//现在写入表-跳过前2行
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);这将生成脚本中失败但在PhpMyAdmin中工作的字符串,并输出语法错误。
发布于 2015-07-19 07:39:55
使用mysql_real_escape_string转义字符串值。
示例:
$name = mysql_real_escape_string($name);
$sql = "insert into test (name) values ('$name')";发布于 2015-07-19 07:47:05
确认列的数据类型。如果它的字符串,值应该用双引号或单引号括起来。
https://stackoverflow.com/questions/31498792
复制相似问题