Home » Php » how to prevent duplicate data

how to prevent duplicate data

Posted by: admin October 20, 2018 Leave a comment

Questions:

I’m trying to insert data in database from multiple table and
I want to prevent the data from duplicating and it works, but

it does not work when there is a new data inserted, it still insert the new data except with all previous data exist. how can i prevent it from duplicating.

I had try INSERT ON DUPLICATE KEY but is it possible to take values from multiple table. How?

This is my code. tmno is 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

Answers: