how to check any of the input value has data before insert to db using PHP?

562 Views Updated: 17 Sep 2018
Follow Post

if any of the field has data save data otherwise dont save the table row data In a table six columns are there and add row button also there. i am facing an issue that blank row is adding to database if user doesnt enter the data in the table row.These fields are not mandatory.if the any of the input has value save the data to db otherwise do not save the data to db

if (isset($_POST["itemname"])) {

    $invoiceid = $_POST["invoiceid"];

    $itemname = $_POST["itemname"];

    $unit = $_POST["unit"];

    $quantity = $_POST["quantity"];

    $rate = $_POST["rate"];

    $totamount = $_POST["totamount"];

    $checkvat = $_POST["checkvat"];

    $query = '';

    for ($count = 0; $count < count($itemname); $count++) {

        $itemname_clean = mysqli_real_escape_string($link, $itemname[$count]);

        $unit_clean = (int) $unit[$count];

        $quantity_clean = (int) $quantity[$count];

        $rate_clean = $rate[$count];

        $totamount_clean = $totamount[$count];

        $checkvat_clean = $checkvat[$count];

        if ($itemname_clean != '' || $unit_clean != '' || $quantity_clean != '' || $rate_clean != '' || $totamount != '') {

            $query .= '

   INSERT INTO invoice_items (invoiceID,itemsName,unit,quantity,rate,amount,includeVAT)

   VALUES("' . $invoiceid . '", "' . $itemname_clean . '", "' . $unit_clean . '", "' . $quantity_clean . '", "' . $rate_clean . '", "' . $totamount_clean . '", "' . $checkvat_clean . '");

   ';

        }

    }

    if ($query != '') {

        if (mysqli_multi_query($link, $query)) {

            echo 'Item Data Inserted';

        } else {

            echo "Error: " . $query . "<br>" . mysqli_error($link);

        }

    } else {

        echo 'All Fields are Required';

    }

}

vote-icon.png
Posted by: nimmi4me Posts: (1) Opinions: (0) Points: 0 Rank: 0
0

Related polls