【问题标题】:Add multiple entries to mysql database through php form通过php表单向mysql数据库添加多个条目
【发布时间】:2017-03-28 11:16:10
【问题描述】:

我正在创建一个数据库系统来存储和检索零售商/公司的发票。我正在寻找一种通过 php 表单将多个条目添加到 mysql 数据库的方法,而无需单独添加每个项目。我的表格看起来像;

<div class="new_invoice">
<form action="addCustomerInvoice.php" method = "post" enctype= "multipart/form-data">
<fieldset>
<legend> Add new invoice for <?php echo $rsCustomer['forename']; echo ' '; echo $rsCustomer['surname']; ?></legend>
<h4>Invoice Number:</h4>
<input type="text" name="invoice_no">
<h4>Item Quantity:</h4>
<input type="text" name="quantity">
<h4>Item Name:</h4>
<input type="text" name="item_name">
<h4>Item Category:</h4>
<input type="text" name="item_category">
<h4>Manufacturer:</h4>
<input type="text" name="item_manufacturer">
<h4>Item Description:</h4>
<input type="text" name="item_description">
<h4>Item Price:</h4>
<input type="text" name="item_price">
<h4>Item Information:</h4>
<input type="text" name="item_info">
<input type="submit" value="Add new record">
</fieldset>
</form>
</div>

像这样处理;

<?php
                            include 'database_conn.php';
                                $InvoiceNumber = $_POST['invoice_no']; 
                                $Quantity = $_POST['quantity']; 
                                $ItemName = $_POST['item_name']; 
                                $ItemCat = $_POST['item_category'];
                                $ItemMan = $_POST['item_manufacturer']; 
                                $ItemDesc = $_POST['item_description'];
                                $ItemInfo = $_POST['item_info'];
                             $sql = "INSERT INTO hlinvoicetable (invoice_no, quantity, item_name, item_category, item_manufacturer, item_description, item_info) VALUES ('$InvoiceNo', '$Quantity', '$ItemName', '$ItemCat', '$ItemMan', '$ItemDesc', '$ItemInfo')";
                              $queryresult = mysqli_query($conn,$sql) or die(mysqli_error());
                            echo "New invoice added. 

                            mysqli_close($conn);
                            ?>

我想知道有没有办法重复表单并让它向数据库添加一个新条目,除非字段留空,因此它被忽略并且不添加任何行?是否所有添加的项目都具有相同的主键 (invoice_no)?

提前致谢!

【问题讨论】:

  • 如果我不说,其他人会:不要INSERT未清理的数据。在bobby-tables.com 上查看 PHP 的安全代码实践。 (如果是 mysqli,请使用准备好的语句。)
  • 你说得对,我只是想精简代码以尽量保持简单,但你的建议很好!

标签: php mysql web-applications phpmyadmin


【解决方案1】:

您需要在输入中使用数组名称。例如:

<input type="text" name="invoice_no[]">
...
<input type="text" name="invoice_no[]">

然后在 PHP 中,您将从$_POST['invoice_no'][0]$_POST['invoice_no'][1] 等获取值。

您可以遍历这些值,例如:

foreach ($_POST['invoice_no'] as $key => $invoice) {
    if (!empty($_POST['invoice_no'][$key])
        && !empty($_POST['quantity'][$key])
        && !empty($_POST['item_name'][$key])
        //... include all fields that can't be left empty
    ) {
        // Do insert
    }
}

此外,如上所述,请确保使用绑定参数,而不是将用户提供的数据直接放入 SQL 查询中。这实际上并没有太多额外的代码,而且是避免 SQL 注入攻击所必需的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-08
    • 1970-01-01
    • 2015-01-28
    • 1970-01-01
    • 1970-01-01
    • 2013-04-14
    • 2016-06-20
    相关资源
    最近更新 更多