2012-11-01 125 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列重複值。