2014-04-24 80 views
0

我遇到這個腳本的小問題。它允許用戶上傳CSV並寫入MySQL表格,同時檢查現有記錄對應表格的重複項。該腳本工作正常,除了消息報告。請看下面。在PHP腳本中報告錯誤

<?php 
require_once("models/config.php"); //db connection is in this file 



function get_file_extension($file_name) { 
return end(explode('.',$file_name)); 
} 

function errors($error){ 
if (!empty($error)) 
{ 
     $i = 0; 
     while ($i < count($error)){ 
     $showError.= '<div class="msg-error">'.$error[$i].'</div>'; 
     $i ++;} 
     return $showError; 
}// close if empty errors 
} // close function 

if (isset($_POST['upfile'])){ 

if(get_file_extension($_FILES["uploaded"]["name"])!= 'csv') 
{ 
$error[] = 'Only CSV files accepted!'; 
} 

if (!$error){ 

$tot = 0; 
$handle = fopen($_FILES["uploaded"]["tmp_name"], "r"); 
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { 

for ($c=0; $c < 1; $c++) { 

    //only run if the first column if not equal to firstname 
    if($data[0] !='firstname'){   
     $check=mysqli_query($mysqli,"select * from persons where  firstname='$data[0]' and surname='$data[1]'"); 
    $checkrows=mysqli_num_rows($check); 
    if($checkrows>0){echo "$data[0] $data[1] exists in the database. Please remove this  entry before uploading your records.";} 
else{ 

     $order = "INSERT INTO persons (firstname, surname, gender, email, address, city, province, postalcode, phone, secondphone, organization, inriding, ethnicity, senior, political_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; 
$stmt = mysqli_prepare($mysqli, $order); 
mysqli_stmt_bind_param($stmt, "sssssssssssssss", $data[0], $data[1], $data[2], $data[3], $data[4], $data[5], $data[6], $data[7], $data[8], $data[9], $data[10], $data[11], $data[12], $data[13], $data[14]); 
$result = mysqli_stmt_execute($stmt);   
    } 

$tot++;} 

} 
} 
fclose($handle); 
$content.= "<div class='success' id='message'> CSV File Imported, $tot records added  </div>"; 
}// end no error 
}//close if isset upfile\\ 
$er = errors($error); 
$content.= <<<EOF 
<h3>Import CSV Data</h3> 
$er 
<form enctype="multipart/form-data" action="" method="post"> 
File:<input name="uploaded" type="file" maxlength="20" /><input type="submit"  name="upfile" value="Upload File"> 
</form> 
EOF; 
echo $content; 
?> 

當腳本發現重複,呼應了「姓姓氏存在於數據庫中。請在上傳記錄之前刪除此條目。 CSV文件已導入,已添加1條記錄「

沒有任何內容會寫入到表格中,這是我想要的內容,但我希望信息說'已導入CSV文件,添加了0條記錄'或者根本沒有該信息當發現一個重複的,因爲這可能會混淆用戶

我知道這是一個超級簡單的解決方法(有點像放錯位置的支架),但我想不通。提前

感謝。

回答

1

移動有:

$tot++; 

裏面的大括號。像這樣:

//only run if the first column if not equal to firstname 
if ($data[0] != 'firstname') { 

    $check  = mysqli_query($mysqli, "select * from persons where firstname='$data[0]' and surname='$data[1]'"); 
    $checkrows = mysqli_num_rows($check); 

    if ($checkrows > 0) { 
     echo sprintf('%s %s exists in the database. Please remove this entry before uploading your records.', $data[0], $data[1]); 
    } 
    else { 
     $order = "INSERT INTO persons (firstname, surname, gender, email, address, city, province, postalcode, phone, secondphone, organization, inriding, ethnicity, senior, political_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; 
     $stmt = mysqli_prepare($mysqli, $order); 
     mysqli_stmt_bind_param($stmt, "sssssssssssssss", $data[0], $data[1], $data[2], $data[3], $data[4], $data[5], $data[6], $data[7], $data[8], $data[9], $data[10], $data[11], $data[12], $data[13], $data[14]); 
     $result = mysqli_stmt_execute($stmt); 

     $tot++; 
    } 
} 

說實話,你的代碼很難閱讀。您可能需要重新選擇它,以便更好地跟蹤它的流程。重新制作代碼後,問題很容易看出。

使用有一個說法幹得好,我已經失去了的,沒有他們的PHP問題計數 - 你應該添加一個用於您的$check查詢呢!

+0

完美,謝謝! – JLA

0

我的第一個猜測是$showError沒有被定義,然後用$showError.=追加到wt。它應該解析成PHP通知「PHP Notice:Undefined variable:$ showError」,但它仍然應該輸出它。

另外,如果沒有錯誤,那麼該函數將輸出null

而且我會用foreach該循環。

function errors($errors) { 
    $showError = ''; 
    if (!empty($errors)) { 
     foreach ($errors as $error) { 
      $showError .= '<div class="msg-error">'.$error.'</div>'; 
     } 
    } 
    return $showError; 
} 

看看是否有助於輸出錯誤。