php - 在数据库中插入多个值

标签 php mysql

我是 php 新手,我正在尝试在数据库中插入多个值。我编写了我的函数,但它抛出了一个错误。我在下面附加了我的 html 表单。我该如何解决这个问题...

错误显示:

A PHP Error was encountered
Severity: Notice
Message: Undefined index: itemNo
Filename: controllers/main.php
Line Number: 2406

我的代码:

public function item_save(){
    $con = mysql_connect("localhost","root","");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }

    mysql_select_db("inventory", $con);
    //$id_number = $_GET['id_user'];
    $val1=$_GET['itemNo'];
    $val2=$_GET['itemName'];
    $val3=$_GET['quantity'];
    $val4=$_GET['price'];
    $val5=$_GET['total'];
    $val6=$_GET['subTotal'];
    $val7=$_GET['tax'];
    $val8=$_GET['taxAmount'];
    $val9=$_GET['totalAftertax'];
    $val10=$_GET['amountPaid'];
    $val1=$_GET['amountDue'];
    $N = count($val1);
    for($i=0; $i < $N; $i++)
    {
    mysql_query("INSERT INTO item_save(item_no,item_name,qty,price,total,subtotal,tax,tax_amount,total_final,amount_paid,amount_due) VALUES ('$val1[$i]','$val2[$i]','$val3[$i]','$val4[$i]','$val5[$i]','$val6','$val7','$val8','$val9','$val10','$val11')");
    }
    redirect("main/multiple");

        }

表单.html

 <div class="container content">
    <?php

echo form_open_multipart('main/item_save');


?>
        <?php


$submit=array(

    'name'=>'submit',
    'type'=>'submit',
    'class'=>'btn btn-primary',
    'value'=>'Submit',

);
?>
        <div class='row'>
            <div class='col-xs-12 col-sm-12 col-md-12 col-lg-12'>
                <table class="table table-bordered table-hover">
                    <thead>
                        <tr>
                            <th width="2%"><input id="check_all" class="formcontrol" type="checkbox"/></th>
                            <th width="15%">Item No</th>
                            <th width="38%">Item Name</th>
                            <th width="15%">Price</th>
                            <th width="15%">Quantity</th>
                            <th width="15%">Total</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td><input class="case" type="checkbox"/></td>
                            <td><input type="text" data-type="productCode" name="itemNo[]" id="itemNo_1" class="form-control autocomplete_txt" autocomplete="off"></td>
                            <td><input type="text" data-type="productName" name="itemName[]" id="itemName_1" class="form-control autocomplete_txt" autocomplete="off"></td>
                            <td><input type="number" name="price[]" id="price_1" class="form-control changesNo" autocomplete="off" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;"></td>
                            <td><input type="number" name="quantity[]" id="quantity_1" class="form-control changesNo" autocomplete="off" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;"></td>
                            <td><input type="number" name="total[]" id="total_1" class="form-control totalLinePrice" autocomplete="off" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;"></td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
        <div class='row'>
            <div class='col-xs-12 col-sm-3 col-md-3 col-lg-3'>
                <button class="btn btn-danger delete" type="button">- Delete</button>
                <button class="btn btn-success addmore" type="button">+ Add More</button>
            </div>
            <div class='col-xs-12 col-sm-offset-4 col-md-offset-4 col-lg-offset-4 col-sm-5 col-md-5 col-lg-5'>
                <form class="form-inline">
                    <div class="form-group">
                        <label>Subtotal: &nbsp;</label>
                        <div class="input-group">
                            <div class="input-group-addon">$</div>
                                                        <input type="number" class="form-control" id="subTotal" name="subTotal" placeholder="Subtotal" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;">
                        </div>
                    </div>
                    <div class="form-group">
                        <label>Tax: &nbsp;</label>
                        <div class="input-group">
                            <div class="input-group-addon">$</div>
                                                        <input type="number" class="form-control" id="tax" name="tax" placeholder="Tax" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;">
                        </div>
                    </div>
                    <div class="form-group">
                        <label>Tax Amount: &nbsp;</label>
                        <div class="input-group">
                            <input type="number" class="form-control" id="taxAmount" name="taxAmount" placeholder="Tax" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;">
                            <div class="input-group-addon">%</div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label>Total: &nbsp;</label>
                        <div class="input-group">
                            <div class="input-group-addon">$</div>
                                                        <input type="number" class="form-control" id="totalAftertax" name="totalAftertax" placeholder="Total" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;">
                        </div>
                    </div>
                    <div class="form-group">
                        <label>Amount Paid: &nbsp;</label>
                        <div class="input-group">
                            <div class="input-group-addon">$</div>
                                                        <input type="number" class="form-control" id="amountPaid" name="amountPaid" placeholder="Amount Paid" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;">
                        </div>
                    </div>
                    <div class="form-group">
                        <label>Amount Due: &nbsp;</label>
                        <div class="input-group">
                            <div class="input-group-addon">$</div>
                                                        <input type="number" class="form-control amountDue" id="amountDue" name="amountDue" placeholder="Amount Due" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;">
                        </div>
                    </div>
                </form>

            </div>
             <?php echo form_submit($submit);?>
        </div>

    </div>

最佳答案

Notice: Undefined index: itemNo意味着你的$_GET['itemNo'];未设置。

您可能忘记添加&itemNo=xxx在您的 URL 中,但查看您缺少的其余代码 itemNo[]从表单获取参数(我猜是复选框)。

编辑:
现在,在您发布 HTML 后,我仍然看不到 <form>标签,我问你一下。您确定使用的是 GET方法?因为多部分表单通常使用 POST方法。

检查您的 HTML 输出(作为浏览器中的源代码),或进行一些网络调试并检查它是 POST 还是 GET。我打赌它是 POST,但你尝试检索 GET。

另外,您可以尝试更改 $_GET$_REQUEST (同时匹配 GET 和 POST),但这不是最好的主意。

关于php - 在数据库中插入多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28826713/

相关文章:

javascript - 日期时间选择器 - Chrome 和 Firefox 之间的奇怪行为

php - HTML mySQL 表单不起作用

php - 如何从 Zend_Auth session 中获取 session ID?

php - 使用 Behat 在 PHP 中找不到类 'Behat\Behat\Context\BehatContext'

c# - 如果行已存在,如何将行插入表或添加值

php - 在 php 中创建时间戳并替换旧日期

mysql - mysql 将两个结果合并为一个结果集

mysql - Xampp连接mysql时出错

php - 了解 jdoc :include in Joomla

php - 如何使用php从下拉菜单中获取所选项目的值