我正在将大量数据从csv文件导入到我的数据库中,但问题是,如果sql中有错误,我的插入就会停止,从而使我的大容量插入使用.i必须返回,删除上传的数据,删除在我的插入中引起问题的条目,然后重新启动。我想要程序跳过它,继续插入,我知道我必须申请尝试和捕捉,我已经应用在我的algo,但我不知道如何使用,以便它继续插入。这是我的密码
$num=35; //number of columns
$dum=true; // a check
$sum=0;// count total entries
if (($handle = fopen($_FILES['file']['name'], 'r')) !== FALSE) {
while (($data = fgetcsv($handle, 10000, ',')) !== FALSE)
{
if($dum)
{
for ($qwe=0; $qwe < $num; $qwe++) { //searching the columns exact position in case they have been changed
if($data[$qwe]=='ID')
{$a=$qwe;}
.
.
.
.
.else if($data[$qwe]=='PowerMeterSerial')
{$aa=$qwe;}
else if($data[$qwe]=='Region')
{$ab=$qwe;}
else if($data[$qwe]=='Questions')
{$ac=$qwe;}
}
}
if(!$dum)
{
for($qwe=0;$qwe<$num;$qwe++)
{
if($qwe==7||$qwe==8)
{
if($qwe==7){$asd=$data[$qwe];}
$data[$qwe]=date('Y-m-d h-i-s',strtotime($data[$qwe]));
}
}
$data[57]=date('Y-m-d ',strtotime($asd));try {
$sql="INSERT INTO pm (ID, .......... Questions, dateofdata ,Unsuccessful) VALUES ('$data[$a]','$data[$b]','$data[$c]','$data[$d]','$data[$e]','$data[$f]','$data[$g]','$data[$h]' ,'$data[$i]','$data[$j]','$data[$k]', '$data[$l]', '$data[$m]','$data[$n]','$data[$o]','$data[$p]','$data[$q]','$data[$r]','$data[$s]','$data[$t]','$data[$u]','$data[$v]','$data[$w]', '$data[$x]','$data[$y]','$data[$z]','$data[$aa]','$data[$ab]','$data[$ac]','$data[57]','$data[30]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error(). $sql);
}
}
catch (Exception $e)
{
echo'<br>'; echo $sql;
}
$sum++;
}$dum=false;
}
}
?> 恳请注意,上传算法或sql没有问题,当输入数据与sql生成的数据类型不匹配时,就会出现错误,因为我正在尝试捕捉。请帮助
发布于 2011-08-17 16:09:27
将die()命令更改为print()。您将看到错误是什么,脚本将转到下一行。
考虑到代码的结构,我猜只要插入字符串(特别是其中带有引号),它就会爆炸,从而导致SQL语法错误。在将这些值插入查询字符串之前,必须将每个文本字段从csv传递到mysql_real_escape_string()中。
https://stackoverflow.com/questions/7095641
复制相似问题