When a person submits their payment voucher, I have a query performed on the database to make sure a voucher hasn't already been submitted for the same date and time. If so, an error message appears and the voucher doesn't insert into the database. If the recordset pulls no records then, the voucher is inserted. This works fine. Where it gets complicated is a person can add multiple dates and start times. So, I have the query run for each instance of this. So, when the validation query runs sometimes it might be fine but, other times, there might be a match. So, the insert keeps happening if there is at least one validation. What I need to happen is that the loop completes through all iterations and then, it determines if there was any duplicates and if so, the insert doesn't happen. Instead it is comparing each iteration without going through the whole loop first. Here is my code:
<?php
if (isset($_POST["submit"]) || isset($_POST["submit_x"])) {
for($i=0;$i<count($_POST['date_performed']);$i++){
$date_performed = $_POST['date_performed'][$i];
$start_time = $_POST['start_time'][$i];
$valid_form = new WA_MySQLi_RS("valid_form",$sa,0);
$valid_form->setQuery("SELECT * FROM voucher_hourly_items left join payment_vouchers on payment_vouchers.voucherID = voucher_hourly_items.voucherID WHERE staffID = ? and service_agreementID = ? and date_performed = ? and start_time = ?");
$valid_form->bindParam("i", "".$_GET['staffID'] ."", "-1"); //colname
$valid_form->bindParam("i", "".$_GET['service_agreementID'] ."", "-1"); //colname2
$valid_form->bindParam("t", "".$date_performed ."", "-1"); //colname3
$valid_form->bindParam("s", "".$start_time ."", "-1"); //colname4
$valid_form->execute();
}
if ($valid_form->TotalRows > 0) {
echo '<br><br><font color="#E03C31" size="+2"><strong>There is already a payment voucher in the system with the same date performed and start time. Please try again!</strong></font>';
}
$saID= $service_agreements->getColumnVal("service_agreementID");
$lname=$service_agreements->getColumnVal("lname");
$fname=$service_agreements->getColumnVal("fname");
$date = date('Y-m-d');
$thesignature = $date."_".$saID."_". $fname."_". $lname.".png";
if ($valid_form->TotalRows == 0) {
$InsertQuery = new WA_MySQLi_Query($sa);
$InsertQuery->Action = "insert";
$InsertQuery->Table = "payment_vouchers";
$InsertQuery->bindColumn("staffID", "i", "".((isset($_POST["staffID"]))?$_POST["staffID"]:"") ."", "WA_DEFAULT");
$InsertQuery->bindColumn("service_agreementID", "i", "".((isset($_POST["service_agreementID"]))?$_POST["service_agreementID"]:"") ."", "WA_DEFAULT");
if ($service_agreements->getColumnVal("payment_terms") == 'Flat Rate') {
$InsertQuery->bindColumn("start_date", "t", "".((isset($_POST["start_date"]))?$_POST["start_date"]:"") ."", "WA_DEFAULT");
$InsertQuery->bindColumn("end_date", "t", "".((isset($_POST["end_date"]))?$_POST["end_date"]:"") ."", "WA_DEFAULT");
$InsertQuery->bindColumn("description_of_services", "s", "".((isset($_POST["description_of_services"]))?$_POST["description_of_services"]:"") ."", "WA_DEFAULT");
$InsertQuery->bindColumn("total_stipend", "i", "".((isset($_POST["total_stipend"]))?$_POST["total_stipend"]:"") ."", "WA_DEFAULT"); }
$InsertQuery->bindColumn("phone_ext", "s", "".((isset($_POST["phone_ext"]))?$_POST["phone_ext"]:"") ."", "WA_DEFAULT");
$InsertQuery->bindColumn("status", "s", "".((isset($_POST["status"]))?$_POST["status"]:"") ."", "WA_DEFAULT");
$InsertQuery->bindColumn("submitted_by", "s", "".((isset($_POST["submitted_by"]))?$_POST["submitted_by"]:"") ."", "WA_DEFAULT");
$InsertQuery->bindColumn("date_submitted", "t", "".((isset($_POST["date_submitted"]))?$_POST["date_submitted"]:"") ."", "WA_DEFAULT");
$InsertQuery->bindColumn("signature", "s", "".$thesignature."", "WA_IGNORE");
$InsertQuery->saveInSession("voucherID");
$InsertQuery->execute();
$folderPath = "../received/";
$image_parts = explode(";base64,", $_POST['signed']);
$image_type_aux = explode("image/", $image_parts[0]);
$image_type = $image_type_aux[1];
$image_base64 = base64_decode($image_parts[1]);
$file = $folderPath . $date.'_'.$saID.'_'.$fname.'_'.$lname.'.'.$image_type;
file_put_contents($file, $image_base64);
$email_address = $service_agreements->getColumnVal("admin_email");
$title = $service_agreements->getColumnVal("title");
$fname= $service_agreements->getColumnVal("fname");
$lname= $service_agreements->getColumnVal("lname");
$website = 'https://secure1.cpsd.us/service_agreement/admin.php';
$message = "A payment voucher has been submitted by $fname $lname for $title. Please visit $website to approve or deny the voucher. \r\n";
$message=wordwrap($message);
$to = $email_address;
$subject = "Approval Needed: Payment Voucher Submitted for $title ";
$headers = "From: schoolnews@cpsd.us\r\n";
if ($service_agreements->getColumnVal("payment_terms") == 'Hourly') {
for($i=0;$i<count($_POST['date_performed']);$i++){
$voucherID = $_SESSION['voucherID'];
$date_performed = $_POST['date_performed'][$i];
$start_time = $_POST['start_time'][$i];
$end_time = $_POST['end_time'][$i];
$hours = $_POST['hours'][$i];
$rate = $_POST['rate'][$i];
$description_of_services = $_POST['description_of_services'][$i];
if($voucherID!=='' && $date_performed!=='' && $start_time!=='' && $end_time!=='' && $hours!=='' && $rate!=='' && $description_of_services!==''){
$sql="INSERT INTO voucher_hourly_items(voucherID,date_performed,start_time,end_time,hours,rate,description_of_services)VALUES('$voucherID','$date_performed','$start_time','$end_time','$hours','$rate','$description_of_services')";
$stmt=$sa->prepare($sql);
$stmt->execute();
//echo '<div class="alert alert-success" role="alert">Submitted Successfully</div>';
}
else{
echo '<div class="alert alert-danger" role="alert">Error Submitting in Data</div>';
}
}
echo "<script type='text/javascript'>";
echo "alert('Submitted successfully')";
echo "</script>";
}
$InsertGoTo = "manage_service_agreements.php";
if (function_exists("rel2abs")) $InsertGoTo = $InsertGoTo?rel2abs($InsertGoTo,dirname(__FILE__)):"";
$InsertQuery->redirect($InsertGoTo);
}}
?>