我有一个从外部文件中提取几行数据的脚本,然后我需要将这些行中的每一行上传到数据库。我在循环中放了一个查询,但它什么也没做。即使启用了错误报告,我什么都没得到。我检查数据库但没有上传任何内容。
这样做的正确方法是什么? 提前谢谢!
我的代码:
$size = $_FILES['file']['size'];
$filename = $_FILES['file']['tmp_name'];
$max_filesize = 100000;
if($size > $max_filesize) //check file size
die('File is too large');
if(file_exists($filename)){
$fp = fopen($filename, "r");
$str = fread($fp, filesize($filename)); //file text stored in variable
$br = "\n"; //search for new row
$rows = substr_count($str,$br); //number of rows
echo "Rows: ". $rows."<br />";
$row = explode( "\n",$str);
$x=0;
while($x<=$rows)
{
$field = $row[$x];
$exp = explode("|",$field);
$case_number = $exp[1];
$unknown1 = $exp[2];
$chapter = $exp[3];
$filed = $exp[8];
//tons more variables, removed for readability
$addrow = mysql_query("INSERT INTO records (case_number, wild1, chapter, filed) VALUES('".$case_number."', '".$unknown1."', '".$chapter."', '".$filed."')");
if($addrow)
{
echo "<p style=\"font-weight:bold\">Row ".$x." uploaded</p>";
}else{
die(mysql_error());
}
$x++;
}
fclose($fp);
}
答案 0 :(得分:1)
您已提及错误Duplicate entry for key 'PRIMARY'
,因此您在设置为PRIMARY
的列中不能包含重复值。