简体   繁体   English

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

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

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

You're only checking $validForm->TotalRows from the last query.您只是从最后一个查询中检查$validForm->TotalRows You need to check this inside the loop.您需要在循环内检查这一点。 If none of the queries returns anything, then the submission is valid.如果没有任何查询返回任何内容,则提交是有效的。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM