繁体   English   中英

防止空的多个输入插入数据库 mysql

[英]Prevent empty multiple inputs from insertion to data base mysql

我的 web 页面中有 200 个输入(200 行),后端用户有时输入 20 或 50 个输入并将 rest 留空

如何防止空输入插入数据库

我通过删除有条件的行来解决这个问题但是很耗时

这是我的代码

<?php
include("db.php");
include("header.php");

if (isset($_POST['invoice_btn'])) {

    $userId = $_POST['userId'];
    $invoice_to = $_POST['companyName'];
    $subTotal = $_POST['subTotal'];
    $taxAmount = $_POST['taxAmount'];
    $taxRate = $_POST['taxRate'];
    $totalAftertax = $_POST['totalAftertax'];
    $amountPaid = $_POST['amountPaid'];
    $amountDue = $_POST['amountDue'];
    $notes = $_POST['notes'];
    $productCode = $_POST['productCode'];
    $productName = $_POST['productName'];
    $quantity = $_POST['quantity'];
    $price = $_POST['price'];
    $total = $_POST['total'];
    $dateTime = $_POST['dateTime'];

    $submitbutton = $_POST['invoice_btn'];

    if ($submitbutton) {
        if (empty($productCode)) {

            die(" Product code empty ");
        } else {



            $sqlInsert = "INSERT INTO invoice_order (invoice_to,order_date, order_total_before_tax, order_total_tax, order_tax_per, order_total_after_tax, order_amount_paid, order_total_amount_due, notes) 
                    VALUES ('$invoice_to' ,'$dateTime', '$subTotal', '$taxAmount','$taxRate', '$totalAftertax','$amountPaid', '$amountDue','$notes')";
            $result = mysqli_query($conn, $sqlInsert);

            // The mysqli_insert_id() function returns the id (generated with AUTO_INCREMENT) from the last query.
            $lastInsertId = mysqli_insert_id($conn);

            foreach ($productCode as $index => $productCodes) {

                $s_productCode  = $productCodes;
                $s_productName  = $productName[$index];
                $s_quantity     = $quantity[$index];
                $s_price     = $price[$index];
                $s_total     = $total[$index];

                $sqlInsertItem = "INSERT INTO invoice_order_item (order_id, item_code, item_name, order_item_quantity, order_item_price, order_item_final_amount) 
                    VALUES ( '$lastInsertId' , '$s_productCode' , '$s_productName' , '$s_quantity', '$s_price', '$s_total')";
                $result2 = mysqli_query($conn, $sqlInsertItem);

                // Update Quantity on Hand from Produc table 
                $sqlUpdateQty = "UPDATE product SET pro_quantity = pro_quantity-$s_quantity WHERE pro_id = $s_productCode ";
                $result3 = mysqli_query($conn, $sqlUpdateQty);


                //delete extra rows that are empty .
                $sqlDelete = "DELETE FROM `invoice_order_item` WHERE `item_code`=''";
                $Delresult = mysqli_query($conn, $sqlDelete);
            } // end foreach
        }; // end else
    } // end if

在您的 foreach 循环中,在插入之前添加一个条件:

if (empty($s_productCode)) continue;

如果条件成立, continue将使解析器跳过循环中代码的 rest。

暂无
暂无

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

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