如果在任何查询中都没有找到记录,则完成循环然后插入

Complete Loop and then Insert if no records are found in any of the queries

当有人提交他们的付款凭证时,我对数据库执行查询以确保尚未提交相同日期和时间的凭证。如果是这样,则会出现一条错误消息,并且凭证不会插入到数据库中。如果记录集没有提取记录,则插入凭证。这很好用。复杂的地方是一个人可以添加多个日期和开始时间。因此,我对每个实例都有查询 运行。因此,当验证查询 运行 时,有时它可能没问题,但在其他时候,可能会匹配。因此,如果至少有一次验证,插入就会继续发生。我需要做的是循环完成所有迭代,然后确定是否有重复项,如果有,则插入不会发生。相反,它比较每个迭代而不先经过整个循环。这是我的代码:

<?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);
    }}
?>

您只检查了上次查询的 $validForm->TotalRows。您需要在循环中检查它。如果 none 的查询 returns 任何内容,则提交有效。

<?php
if (isset($_POST["submit"]) || isset($_POST["submit_x"])) {
    $submission_is_valid = true;
    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>';
            $submission_is_valid = false;
            break;
        }
    }
    $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 ($submission_is_valid) { 
        $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);
    }
}
?>