在数据库中插入多个值

时间:2015-03-03 07:58:23

标签: 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");

        }

form.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>

1 个答案:

答案 0 :(得分:1)

Notice: Undefined index: itemNo表示您的$_GET['itemNo'];未设置。

您可能忘记在网址中添加&itemNo=xxx,但是在查看代码的其余部分时,您会遗漏itemNo[]来自表单的GET参数(我猜复选框)。

修改
现在,在您发布HTML后,我仍然没有看到<form>标记,让我问你。您确定使用GET方法吗?因为多部分表单通常使用POST方法。

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

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