检查重复记录,同时在Php中的Mysql DB中插入多行

时间:2017-12-24 21:34:47

标签: php mysqli

您好我想知道,如何在Php中的Mysql DB中添加多行时如何检查重复记录?

我正在使用php reader从excel表格向我的数据库添加数据。

这是我的代码,到目前为止我已尝试过,但它总是添加重复行而不提供任何通知/错误消息。

            $x=2;
        while($x<=$connection->sheets[$sheet]['numRows']) {//04     
        $bpsuplier = isset($connection->sheets[$sheet]['cells'][$x][1]) ? $connection->sheets[$sheet]['cells'][$x][1] : '';
        $borp = isset($connection->sheets[$sheet]['cells'][$x][2]) ? $connection->sheets[$sheet]['cells'][$x][2] : '';
        $bpslno = isset($connection->sheets[$sheet]['cells'][$x][3]) ? $connection->sheets[$sheet]['cells'][$x][3] : '';
        $bptype = isset($connection->sheets[$sheet]['cells'][$x][4]) ? $connection->sheets[$sheet]['cells'][$x][4] : '';
        $bprefno = isset($connection->sheets[$sheet]['cells'][$x][5]) ? $connection->sheets[$sheet]['cells'][$x][5] : '';
        $bpdate = isset($connection->sheets[$sheet]['cells'][$x][6]) ? $connection->sheets[$sheet]['cells'][$x][6] : '';
        $bpamnt = isset($connection->sheets[$sheet]['cells'][$x][7]) ? $connection->sheets[$sheet]['cells'][$x][7] : '';
        $bpcurr = isset($connection->sheets[$sheet]['cells'][$x][8])? $connection->sheets[$sheet]['cells'][$x][8] : '';
        $bpkcgmt = isset($connection->sheets[$sheet]['cells'][$x][9]) ? $connection->sheets[$sheet]['cells'][$x][9] : '';
        $bpkcgmtfor = isset($connection->sheets[$sheet]['cells'][$x][10]) ? $connection->sheets[$sheet]['cells'][$x][10] : '';
        $bprmks = isset($connection->sheets[$sheet]['cells'][$x][11]) ? $connection->sheets[$sheet]['cells'][$x][11] : '';

//======================section-001-->check duplicate=====
$res= mysqli_query($conn,"select * from account where 
factsuply='$bpsuplier'  and billpay='$borp'     and bpslno='$bpslno'    and bpdtyp='$bptype'    and bprefno='$bprefno' and bpdte='$bpdate'  and ackcgmt='$bpkcgmt'  and bpprog='$bpkcgmtfor'    and acamount='$bpamnt'  and currency='$bpcurr'  and acrmks='$bprmks' ");
 $total = mysqli_num_rows($res);
if($total==0)
//======================section-001-->check duplicate=====
  { 
$insert = $conn->prepare('
INSERT INTO account (factsuply, billpay, bpslno, bpdtyp, bprefno, bpdte, 
    ackcgmt, bpprog, acamount, currency, acrmks ) 
    VALUES (
    ?, ?, ?, ?,
    ?, ?, ?, ?,
    ?, ?, ?)
   ');
  $insert->bind_param(
   'sssssssssss',
  $bpsuplier, $borp, $bpslno, $bptype,
  $bprefno, $bpdate, $bpamnt, $bpcurr,
  $bpkcgmt, $bpkcgmtfor, $bprmks );
  $insert->execute();           
  $x++;


 //======================section-001-->message for duplicate entry=====
}
else
 {
   echo "This is seems DUPLICATE entry, please go back 
  and do some changes to add new entry.";
 }
 //======================section-001-->message for duplicate entry=====

}//04-x2numrows 

0 个答案:

没有答案