I have a form that calls this script, this is generating invoices but sometimes it creates duplicate, I cant figure it out, please help thanks in advance. The duplicate data was inserted at the same time as the table has timestamp, both records where inserted 2023-06-12 10:36:40 x 2. On the top of the php code I have added some code to check for duplicate, but it seems when inserting it inserts twice with the same time 2023-06-12 10:36:40.
The script
<?php
// inv binder stop the code if it wants to create dublicate give a message quotation created already
$inv_code = $_POST['inv_code'];
// Below is optional, remove if you have already connected to your database.
$mysqli = mysqli_connect('localhost', 'root', '', 'crmsigni_crm');
$sql=mysqli_query($mysqli,"SELECT * FROM si_invoices where inv_code='$inv_code'")or die(mysql_error());
$row=mysqli_fetch_assoc($sql);
$compareinv = $row['inv_code'];
$invclient = $row['main_client'];
$jobtitle = $row['job_title'];
if( $inv_code === $compareinv ){
echo "<h4>Same quotation is already created with the Client name of".$invclient." and Job title of: ".$jobtitle."</h4><br/>";
die("Browse to check the invoice, your quotation was already created!");
}
?>
<?php
if(isset($_POST['price']) && !empty($_POST['price'])) {
//conditional to insert itemsinv start
$conn = $mysqli = mysqli_connect('localhost', 'root', '', 'crmsigni_crm');
/*
$data = $_POST;
echo "<pre>";
var_dump($data);*/
$count = count($_POST['qty']);
$name = $_POST['name'];
$price = $_POST['price'];
for ($i=0; $i<$count; $i++) {
$sql = "INSERT IGNORE INTO itemsinv (ln_no, productname, particulars, qty, price, item_total, invidfk) VALUES ('{$_POST['ln_no'][$i]}','{$_POST['productname'][$i]}','{$_POST['particulars'][$i]}', '{$_POST['qty'][$i]}' , '{$_POST['price'][$i]}' , '{$_POST['item_total'][$i]}' , '{$_POST['invidfk'][$i]}')";
$conn->query($sql);
} //conditional to insert itemsinv end
}
else {
echo "ERROR! Invoice cannot be created with no any details<br/>";
echo "<a href='choose-invoice-type.php'>Re-create Profoma again here</a>";
exit();
}
?>
<?php
/*Create contact first*/
$conn = $mysqli = mysqli_connect('localhost', 'root', '', 'crmsigni_crm');
mysqli_select_db($conn,'crmsigni_crm');
$main_client = $_POST['main_client'];
$contact_name = $_POST['contact_name'];
$billing_name = $_POST['billing_name'];
$postal_address = $_POST['postal_address'];
$cell_clnt = $_POST['cell_clnt'];
$offc_loc = $_POST['offc_loc'];
$site_loc = $_POST['site_loc'];
$email_addr = $_POST['email_addr'];
$job_title = $_POST['job_title'];
$clnt_tin = $_POST['clnt_tin'];
$clnt_vrn = $_POST['clnt_vrn'];
$vat_expt = $_POST['vat_expt'];
$expt_cert = $_POST['expt_cert'];
$our_tin = $_POST['our_tin'];
$our_vrn = $_POST['our_vrn'];
$inv_code = $_POST['inv_code'];
$prepared_by = $_POST['prepared_by'];
$sales_person = $_POST['sales_person'];
// put discount and no discount values
$discamt = $_POST['amountUSD'];
if ($discamt == 0 ) {
$st = $_POST['sub_totl'];
$sub_totl = number_format($st / 1000, 3, ",", ",");
//discount value
$dis_perc_sub = 0;
//discount total
$dis_val_sub = 0;
$vp = $_POST['vat_percent'];
$vat_percent = number_format($vp / 1000, 3, ",", ",");
$gt = $_POST['grand_total'];
$grand_total = number_format($gt / 1000, 3, ",", ",");
} else if ($discamt > 0 ) {
$st = $_POST['sub_totl'];
$dt = $_POST['amountNGN'];
$nsubttl = $st;
$sub_totl = number_format($nsubttl / 1000, 3, ",", ",");
//discount value
$dis_perc_sub = $_POST['amountUSD'];
//discount total
$dstc = $_POST['amountNGN'];
$dis_val_sub = number_format($dstc / 1000, 3, ",", ",");
$vp = 0.18 * $nsubttl;
$vat_percent = number_format($vp / 1000, 3, ",", ",");
//grandtotal new
$gt = $vp + $nsubttl;
$grand_total = number_format($gt / 1000, 3, ",", ",");
}
//pending for lpo users to update lpo
$lpo_user = "UPDATE";
$lpo_status = "UPDATE";
date_default_timezone_set('Africa/Dar_es_Salaam');
$newDate = date('Y-m-d h:i:s T', time());
$sqlone= "INSERT IGNORE INTO si_invoices ( id, date_created, main_client, contact_name, billing_name, postal_address, cell_clnt, offc_loc, site_loc, email_addr, job_title, clnt_tin, clnt_vrn, vat_expt, expt_cert, our_tin, our_vrn, inv_code, prepared_by, sales_person, sub_totl, vat_percent, grand_total, dis_perc_sub, dis_val_sub, lpo_user, lpo_status)
VALUES (default, '$newDate', '$main_client', '$contact_name', '$billing_name', '$postal_address', '$cell_clnt','$offc_loc', '$site_loc', '$email_addr','$job_title','$clnt_tin','$clnt_vrn','$vat_expt','$expt_cert','$our_tin','$our_vrn','$inv_code','$prepared_by','$sales_person','$sub_totl','$vat_percent','$grand_total', '$dis_perc_sub', '$dis_val_sub', '$lpo_user','$lpo_status')";
if ($conn->query($sqlone) === TRUE) {
echo '<div class="alert alert-success">
<strong>OK!</strong>details added!.
</div>';
} else {
echo "Error: else statement of insert invoices" . $sqlone . "<br>" . $conn->error;
}
?>
<?php
$idswitch = $_POST['inv_code']; // id for viewing
// Below is optional, remove if you have already connected to your database.
$mysqli = $mysqli = mysqli_connect('localhost', 'root', '', 'crmsigni_crm');
$sql=mysqli_query($mysqli,"SELECT * FROM si_invoices WHERE inv_code ='$idswitch'")or die(mysql_error());
$row=mysqli_fetch_assoc($sql);
$idz = $row['id'];
header('Location: pdfy-invoices.php?id='.$row['id'].'');
echo '<a href="pdfy-invoices.php?id='.$row['id'].'" target="_blank" >VIEW PROFOMA IN PDF FORMAT</a>';
?>
</div>
</div>
</div>
<?php include('include/footer.php');?>