This is a code for inserting the barcode into a database but if the same barcode exists in the database, I don't need this to be inserted same again, so I coded like this but still, the same barcode is inserting in my database. What the error in this code?
I am using mysqli and Bootstrap to show the errors in the page, the page is executed from another page called product.php via ajax and it is showing in another page called showuser.php as it gets updated in a table which should give the output
<?php
include('conn.php');
if(isset($_POST['add'])){
$proname=$_POST['product'];
$bar=$_POST['barcode'];
$war=$_POST['warranty'];
$gar=$_POST['guarranty'];
$amount=$_POST['amount'];
$id = $_POST['cusid'];
$date = $_POST['date'];
$check = "SELECT * FROM warranty_update WHERE battery_serial = '$bar'";
$rs = mysqli_query($con,$check);
$data = mysqli_fetch_array($rs, MYSQLI_NUM);
if($data[0] > 1) {
?>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<h3 align="center"><i class=" glyphicon glyphicon-warning-sign"></i><br>
<br>
Sorry Barcode Already Been Inserted !<br><br>
Please Go Back</h3>
<?php } else {
$datesplit = explode("-",$date);
$warranty = $datesplit[0];
$guarante = $datesplit[0];
$day = $datesplit[1];
$month = $datesplit[2];
$fdate = strtotime($date);
$Gm=strtotime("+$gar Months",$fdate);
$wm=strtotime("+$war Months",$Gm);
$m1=date("Y-m-d", $Gm) ;
$m2 =date("Y-m-d", $wm) ;
$today = date("Y/m/d");
$expiry_warranty = $m2;
$expiry_guarante = $m1;
//mysql_query("INSERT INTO `cus_reg`(`reg_id`, `name`, `mobile`, `battery_serial`, `model`, `date`, `reg_by`, `warranty_expiry`, `waranty_span`,`guarantee_expiry`, `g_span`) VALUES (NULL,'$name','$mobile','$battery_serial','$vehicle_num','$date','$regby','$expiry_warranty', '$wmonth', '$expiry_guarante', '$gmonth')")==true or die(mysql_error);
//mysql_query("INSERT INTO `warranty_update`(`wa_id`, `cus_reg_id`, `new_barcode`, `date`) VALUES (NULL,LAST_INSERT_ID(),'$battery_serial','$today')");
mysqli_query($conn,"INSERT INTO `warranty_update`(`wa_id`, `reg_id`, `battery_serial`, `model`, `date`, `warranty_expiry`, `waranty_span`, `guarantee_expiry`, `g_span`, `amount`) VALUES (NULL,'$id','$bar','$proname','$today','$expiry_warranty','$war','$expiry_guarante','$gar','$amount')");
mysqli_query($conn,"INSERT INTO `replacements`(`r_id`, `reg_id`, `wa_id`, `new_barcode`, `date`) VALUES ('NULL','$id',LAST_INSERT_ID(),'$bar','$today')");
?>
<!--header("refresh:1;url=home.php");-->
<h3 align="center"><i class=" glyphicon glyphicon-warning-sign"></i><br>
<br>
Data Successfully Inserted !
<?php }
}
?>