如何防止重复数据

时间:2018-05-22 07:03:18

标签: php mysqli duplicates

我试图从多个表格中插入数据库中的数据 我想防止数据重复,它可以工作,但

当插入新数据时它不起作用,它仍会插入新数据,除非存在所有先前数据。我怎样才能防止它重复。

我尝试过INSERT ON DUPLICATE KEY但是可以从多个表中获取值。怎么样?

这是我的代码。 tmno是userID

<?php
session_start();
include ('configure.php');

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

$tmno = $_SESSION['tmno'];
$fname = $_SESSION['fname'];
$date = $_SESSION['date'];
$month = $_SESSION['month'];
$paymentID = $_SESSION['paymentID'] ;
$cartID = $_SESSION['cartID'];
$statusName = $_SESSION['statusName'];
$feeID = $_SESSION['feeID'];

$check = mysqli_query($con,"SELECT * FROM invoice WHERE paymentID='$paymentID' AND feeID='$feeID'");
$checkrows = mysqli_num_rows($check);
if($checkrows){
echo "<script>alert('Already Update!');
            window.location='AdminFee.php'</script>";
}
else{ 

$yo = "INSERT IGNORE INTO invoice (paymentID,cartID,date,feeID,month,tmno,statusName)
       SELECT payment.paymentID, cart.cartID,payment.date,fee.feeID,fee.month,cart.tmno,payment.statusName
       FROM payment
       inner join cartitem on payment.cartID = cartitem.cartID
       inner join fee on cartitem.feeID = fee.feeID
       inner join cart on payment.cartID = cart.cartID";

$keputusan = mysqli_query($con,$yo);

    if($keputusan){

    echo "<script>alert('Invoice Updated!');
            window.location='AdminFee.php'</script>";
    }
    else{
    echo "<script>alert('Failed to Update Invoice!');
            window.location='AdminFee.php'</script>";
    }

}
}
?>

Database Image : the red box show the duplication data and the yellow box show the new data inserted

0 个答案:

没有答案