我对这个脚本有一些小问题。它允许用户上传CSV并写入MySQL表,同时检查现有记录对表的重复项。除了消息报告之外,脚本工作正常。请参阅下文。
<?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;
?>
当脚本找到重复内容时,它回显出'数据库中存在Firstname Surname。请在上传记录之前删除此条目。已导入CSV文件,添加了1条记录“
没有任何内容写入表格,这就是我想要的。但是我希望这条消息说“已导入CSV文件,添加了0条记录”或者在找到重复内容时没有显示该消息,因为这可能会使用户感到困惑
我知道这是一个非常简单的修复(类似于错位的支架),但我无法弄清楚。
提前致谢。
答案 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
查询添加一个!
答案 1 :(得分:0)
我的第一个猜测是,在您使用$showError
附加wt之前未定义$showError.=
。它应解析为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;
}
看看这是否有助于输出错误。