dongshou1991 2014-04-24 17:02
浏览 38
已采纳

PHP脚本中的错误报告

I am having minor issue with this script. It is allows the user to upload a CSV and write to a MySQL table along with checking for duplicates against the table for existing records. The script works OK except for message reporting. Please see below.

<?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;
?>

When the script finds a duplicate, it echoes out 'Firstname Surname exists in the database. Please remove this entry before uploading your records. CSV File Imported, 1 records added"

Nothing gets written to the table, which is what I want. But I would prefer the message to say 'CSV File Imported, 0 records added' or not have that message appear at all upon finding a duplicate, because that might confuse the user

I know this is a super easy fix (something like a misplaced bracket), but I can't figure out.

Thanks in advance.

  • 写回答

2条回答 默认 最新

  • douba1904 2014-04-24 17:10
    关注

    Move the line that has:

    $tot++;
    

    inside the preceding brace. Like so:

    //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++;
        }
    }
    

    To be honest, your code is very difficult to read. You may want to re-tab it so you can follow the flow of it better. After re-tabbing your code, the problem was quite easy to see.

    Well done on using a statement there, I've lost count of the PHP questions without them - you should add one for your $check query too!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决