PHP mysql中未定义的索引错误

时间:2014-01-11 16:40:18

标签: php mysql forms

这是我的表单代码,

 <form role="form" id="order-form" action="db.php" method="post">
            <div class="form-group">
            <label for="name">Name</label>
            <input type="text" class="form-control" id="name" name="name" placeholder="Your Full Name">
            </div>
                <div class="form-group">
            <label for="address">Delivery Address</label>
    <textarea class="form-control" rows="3" id="address" name="address" placeholder="Address" ></textarea>

                </div>
              <div class="form-group">
             <label for="phone">Phone No</label>
        <input type="text" class="form-control" id="phone" name="mobile" placeholder="Phone No">
        </div>
       <div class="form-group">
        <label for="email">Email address</label>
      <input type="email" class="form-control" id="email" name="email" placeholder="Enter email">
    </div>
    <div class="form-group">
    <label for="exampleInputEmail1">Quantity</label>
    <select class="form-control" id="quantity" name="quantity">
        <option>1</option>
        <option>2</option>
        <option>3</option>
        <option>4</option>
        <option>5</option>
    </select>
    </div>
    <div class="form-group" >
    <label for="exampleInputEmail1">Expected Date</label>
        <input id="datepicker" name="date" >
        </div>


      <button type="submit" class="btn btn-default" >Submit</button>
    </form>

这是我的db.php。

<?php
$con=mysqli_connect("localhost","root","","honey");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$sql="INSERT INTO pre_order
            (name,delivery_location,mobile,email,qty,workdt)
            VALUES
            ('$_POST[name]','$_POST[address]','$_POST[mobile]','$_POST[email]','$_POST[quantity]','$_POST[date]')";
if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }
//'echo "Order Received";

mysqli_close($con);
?>

当我点击提交表单时,它说

PHP声明:

Notice: Undefined index: address in C:\xampp\htdocs\office_work\ironbull2\ironbull\db.php on line 12

Notice: Undefined index: date in C:\xampp\htdocs\office_work\ironbull2\ironbull\db.php on line 12

我在这里使用的日期选择器是jquery ui plugin

2 个答案:

答案 0 :(得分:2)

您没有在引号中包装数组键引用。

$_POST[name]

应该是

$_POST['name']

它将它们视为常数。

答案 1 :(得分:-1)

这是因为你没有将值传递给$ _POST数组。

在提交之前,您需要检查那些变量是否具有值。

OR

你可以在帖子前添加“@”。 LIKE:

@$_POST[name]