简体   繁体   中英

Ignore blank text boxes before insertion into SQL

I'm working on a system that has a foreach loop that inserts a group of text boxes into a database. This code will loop for every car_init text box that exists on the previous page.

    foreach($_POST['car_init'] as $key => $car_init)
    {
        $sql1 = "INSERT INTO CustBill_cars (C_ID, car_init, car_num, bolNum, bill_ID)
             SELECT $customer, UPPER('".$car_init."'), '".$car_num[$key]."', $pbolNum, MAX(CustBill_billing.bill_ID)
             FROM CustBill_billing";
    }

My question is: how do I ignore it if a text box is blank? Example: Someone enters in that they need 5 text boxes, but they actual only need 4. The 4 text boxes are filled and the 5th one is blank, how do I just insert the 4 text boxes with information and ignore the blank one.

EDIT: I fixed the problem. I needed to add

    if(empty($car_init['car_init'])) { continue; }

before my insert statement.

An if condition:

foreach($_POST['car_init'] as $key => $car_init)
{
    if ($car_init) {
        $sql1 = "INSERT INTO CustBill_cars (C_ID, car_init, car_num, bolNum, bill_ID)
             SELECT $customer, UPPER('".$car_init."'), '".$car_num[$key]."', $pbolNum, MAX(CustBill_billing.bill_ID)
             FROM CustBill_billing";
    }
}

Hope it helps you !!

  foreach($_POST['car_init'] as $key => $car_init)
     {

      $car_init = trim($car_init);
       if($car_init !=""){

             // add your insertion part here
         }

       }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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