我有一个从外部文件中提取多行数据的脚本,然后我需要将这些行中的每一行上传到数据库。我在循环中放了一个查询,但它什么也没做。即使打开了错误报告,我也一无所获。我检查了数据库,但没有上传任何内容。
这样做的正确方法是什么?提前致谢!
我的代码:
$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);
}