1
我上傳CSV代碼是這樣想提醒重複記錄它們已存在於數據庫我得到與$狀態變量的結果我應該怎麼做,提醒每一個重複的記錄是什麼方式提醒每重複記錄警報重複記錄
if(isset($_POST["Import"]))
{
$filename=$_FILES["file"]["tmp_name"];
if($_FILES["file"]["size"] > 0)
{
$row = 1;
if (($handle = fopen("$filename", "r")) !== FALSE)
{
while (($data = fgetcsv($handle)) !== FALSE)
{
$num = count($data);
$row++;
for ($c=0; $c < $num; $c++) {
if($c!="" && $c+1!="" && $c+2!=""){
$a[] = $data[$c];
}
}
}
$total = count($a);
$m=0;
$k = 1;
for ($c=0; $c < $total; $c++)
{
$address[$m][] = $a[$c] ;
$z = $address[$m][0];
$q = $address[$m][1];
$q .= $address[$m][2];
$q .= $address[$m][3];
$q .= $address[$m][4];
$q .= $address[$m][5];
$q .= $address[$m][6];
$q .= $address[$m][7];
$q .= $address[$m][8];
$q .= $address[$m][9];
if($a[$c]=="")
{
if($k!=1)
{
$selectcon = "SELECT user_id FROM contact
WHERE user_id = '".$z."'";
$selectRes = mysql_query($selectcon);
if($rows = mysql_fetch_array($selectRes))
{
$user_id = $rows['user_id'];
if($user_id == $z)
{
$staus = 1;
}
else
{
$staus = 1;
}
}
$insertParty = "INSERT INTO contact(user_id)values('$z')";
$res = mysql_query($insertParty);
$contact_id = mysql_insert_id();
$insertAddress = "INSERT INTO address(contact_id,address)VALUES($contact_id,'$q')";
$insertAddressRes = mysql_query($insertAddress);
if(!$insertAddressRes)
{
echo "<sctipt>";
echo "sweetAlert('oops','Import Data Fail','error')";
echo "</sctipt>";
}
else
{
echo "<script>";
echo "swal('Sucess!', 'File Imported Sucessfully!','success')";
echo "</script>";
}
$m++;
}
$k++;
}
}
fclose($handle);
}
}
}
?>
,但我想,以顯示重複的記錄列表,用戶,但這種解決方案這是不可能的.. –
1.您可以將之前使用SELECT,要檢查,如果該行已經存在。 2.通過使用UNIQUE行索引,執行INSERT命令後,檢查它是否是成功還是失敗,如果沒有,是否該行已經存在。如果已經存在,則該行已經存在。注:如果大多數行重複,則使用第一種方法,否則第二種(由於性能)。 – David
我已經插入之前使用select和我在得到結果,但問題是,結果後它仍然運行我應該怎麼做才能停止執行 –