2012-11-01 166 views
0

我有一个脚本从外部文件中提取多行数据,然后我需要将这些行中的每一行上传到数据库。我在循环中添加了一个查询,但它什么也没做。即使打开了错误报告,我也什么也得不到。我检查数据库,但没有任何内容正在上传。SQL查询不工作在循环中

这样做的正确方法是什么? 在此先感谢!

我的代码:

$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

您在查询中没有执行任何错误检查。添加它。手册显示了如何。 http://www.php.net/manual/en/function.mysql-query.php该手册还将向您展示过时的mysql库的替代方案。 –

+0

@ Mr.Alien @Pekka,我有'error_reporting(E_ALL); ini_set('display_errors','1');'在我的文档顶部。我没有得到任何错误。回路底部的回波也不显示。 –

回答

1

您刚才提到的错误Duplicate entry for key 'PRIMARY'所以你不能在它被设置为PRIMARY列重复值。