注意:未定义索引:名称,不能插入数据

时间:2013-11-06 17:25:41

标签: php

另一个问题在这里。我创建了将数据插入数据库。然后,我收到了错误提示:未定义的索引:第252行的名称..这里是我的代码..下面是表单代码..

<form id="form1" name="form1" method="post" action="<?=$_SERVER['PHP_SELF']?>">
    <input type="submit" name="submit" id="submit" value="Submit" />
    <input name="reset" type="reset" id="reset" value="Reset" />

    <?php
        if(!isset($_POST['submit']))
        {
            //form not submitted
        }
        else
        {
            //get from input
            //escape input values for greater safety
            $name=(trim($_POST['name'])=='')? die ('ERROR : Enter name') : mysql_escape_string($_POST['name']); // line 252
            $address=(trim($_POSTname['address'])=='')? die ('ERROR : Enter Address') : mysql_escape_string($_POST['address']);
            $phone=(trim($_POST['phone'])=='')? die ('ERROR : Enter Phone') : mysql_escape_string($_POST['phone']);
            $email=(trim($_POST['email'])=='')? die ('ERROR : Enter Email') : mysql_escape_string($_POST['contact']);
            $item1=(trim($_POST['item1'])=='')? die ('ERROR : Enter Donut') : mysql_escape_string($_POST['item1']);
            $item2=(trim($_POST['item2'])=='')? die ('ERROR : Enter Donut') : mysql_escape_string($_POST['item2']);
            $item3=(trim($_POST['item3'])=='')? die ('ERROR : Enter Donut') : mysql_escape_string($_POST['item3']);
            $item4=(trim($_POST['item4'])=='')? die ('ERROR : Enter Donut') : mysql_escape_string($_POST['item4']);
            $item5=(trim($_POST['item5'])=='')? die ('ERROR : Enter Donut') : mysql_escape_string($_POST['item5']);

            //open connection
            $connection=mysql_connect('localhost', 'root', '') or die ('Unable to connect');

            //select database
            mysql_select_db('donut') or die ('Unable to select database');

            //create query
            $query= "INSERT INTO ordering (name,address,phone,email,item1,item2,item3,item4,item5) VALUES ('$name','$address','$phone','$email', '$item1','$item2','$item3','$item4','$item5')";

            //execute query
            $result=mysql_query($query) or die ("Error in query: $query.".mysql_error());

            //print ID of inserted record
            echo 'New record has been successfully inserted';

            //close connection
            mysql_close($connection);
        }
    ?>
</form>

好吧,这是我的表单代码..你能看到任何错误吗?

<td width="24%" class="n"><label for="name">Name:</label></td>
    <td width="26%"><input type="text" name="name" id="name" /></td>
    <td width="50%">&nbsp;</td>
  </tr>
  <tr>
    <td class="n"><label for="address">Address:</label></td>
    <td><textarea name="address" id="address"></textarea></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td class="n"><label for="phone">Phone Number :</label></td>
    <td><input type="text" name="phone" id="phone" /></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td class="n"><label for="email">Email :</label></td>
    <td><input type="text" name="email" id="email" /></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td class="n"><label for="donut"> Choose Donut :</label></td>
    <td><p>
      <span class="k">
      <select name="item1" id="item1">
        <option>Colorful Choc </option>
        <option>Black Forest</option>
        <option>chezzy Gum</option>
        <option>Orange</option>
        <option>Coconut Bead</option>
        <option>Cheese Gaeden</option>
        <option>Creamy Straw</option>
        <option>Stripe Donut</option>
        <option>Choc Sugar</option>
        <option>Chocolate Chip</option>
        <option>Pillow Choc</option>
        <option>Star Wars</option>
        <option>Regular</option>
        <option>Diamond Bead</option>
        <option>Strawberry</option>
        <option>Munchys</option>
        <option>Popcorn</option>
        <option>Berry Cheese</option>
        <option>Sugar Bread</option>
        <option>Choc Chopstick</option>
        <option>Regular Planta</option>
        <option>Round Round</option>
        <option>Sprinkle Nuts</option>
        <option>Sprinkle Color</option>
      </select>
      </span></p>
      <p>
        <span class="k">
        <select name="item2" id="item2">
          <option>Colorful Choc </option>
          <option>Black Forest</option>
          <option>chezzy Gum</option>
          <option>Orange</option>
          <option>Coconut Bead</option>
          <option>Cheese Gaeden</option>
          <option>Creamy Straw</option>
          <option>Stripe Donut</option>
          <option>Choc Sugar</option>
          <option>Chocolate Chip</option>
          <option>Pillow Choc</option>
          <option>Star Wars</option>
          <option>Regular</option>
          <option>Diamond Bead</option>
          <option>Strawberry</option>
          <option>Munchys</option>
          <option>Popcorn</option>
          <option>Berry Cheese</option>
          <option>Sugar Bread</option>
          <option>Choc Chopstick</option>
          <option>Regular Planta</option>
          <option>Round Round</option>
          <option>Sprinkle Nuts</option>
          <option>Sprinkle Color</option>
        </select>
      </span></p>
      <p>
        <span class="k">
        <select name="item3" id="item3">
          <option>Colorful Choc </option>
          <option>Black Forest</option>
          <option>chezzy Gum</option>
          <option>Orange</option>
          <option>Coconut Bead</option>
          <option>Cheese Gaeden</option>
          <option>Creamy Straw</option>
          <option>Stripe Donut</option>
          <option>Choc Sugar</option>
          <option>Chocolate Chip</option>
          <option>Pillow Choc</option>
          <option>Star Wars</option>
          <option>Regular</option>
          <option>Diamond Bead</option>
          <option>Strawberry</option>
          <option>Munchys</option>
          <option>Popcorn</option>
          <option>Berry Cheese</option>
          <option>Sugar Bread</option>
          <option>Choc Chopstick</option>
          <option>Regular Planta</option>
          <option>Round Round</option>
          <option>Sprinkle Nuts</option>
          <option>Sprinkle Color</option>
        </select>
      </span></p>
      <p>
        <span class="k">
        <select name="item4" id="item4">
          <option>Colorful Choc </option>
          <option>Black Forest</option>
          <option>chezzy Gum</option>
          <option>Orange</option>
          <option>Coconut Bead</option>
          <option>Cheese Gaeden</option>
          <option>Creamy Straw</option>
          <option>Stripe Donut</option>
          <option>Choc Sugar</option>
          <option>Chocolate Chip</option>
          <option>Pillow Choc</option>
          <option>Star Wars</option>
          <option>Regular</option>
          <option>Diamond Bead</option>
          <option>Strawberry</option>
          <option>Munchys</option>
          <option>Popcorn</option>
          <option>Berry Cheese</option>
          <option>Sugar Bread</option>
          <option>Choc Chopstick</option>
          <option>Regular Planta</option>
          <option>Round Round</option>
          <option>Sprinkle Nuts</option>
          <option>Sprinkle Color</option>
        </select>
      </span></p>
      <p>
        <span class="k">
        <select name="item5" id="item5">
          <option>Colorful Choc </option>
          <option>Black Forest</option>
          <option>chezzy Gum</option>
          <option>Orange</option>
          <option>Coconut Bead</option>
          <option>Cheese Gaeden</option>
          <option>Creamy Straw</option>
          <option>Stripe Donut</option>
          <option>Choc Sugar</option>
          <option>Chocolate Chip</option>
          <option>Pillow Choc</option>
          <option>Star Wars</option>
          <option>Regular</option>
          <option>Diamond Bead</option>
          <option>Strawberry</option>
          <option>Munchys</option>
          <option>Popcorn</option>
          <option>Berry Cheese</option>
          <option>Sugar Bread</option>
          <option>Choc Chopstick</option>
          <option>Regular Planta</option>
          <option>Round Round</option>
          <option>Sprinkle Nuts</option>
          <option>Sprinkle Color</option>
        </select>
      </span></p></td>
    <td><p class="k">T</p></td>
  </tr>
  <tr>
    <td class="n">&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>

3 个答案:

答案 0 :(得分:2)

这一行有一个可能的错误(且很可能是)错误:

$address=(trim($_POSTname['address'])

应为:

$address=(trim($_POST['address'])

编辑示例

以下是我在自己的服务器上尝试成功的内容:

<form id="form1" name="form1" method="post" action="<?=$_SERVER['PHP_SELF']?>">
<input type="text" name="name" id="name" />
<br>
<input type="submit" name="submit" id="submit" value="Submit" />
<input name="reset" type="reset" id="reset" value="Reset" />
</form>

<?php
    if(!isset($_POST['submit']))
    {
    //form not submitted
    }
    else
    {
    //get from input
    //escape input values for greater safety
    $name=(trim($_POST['name'])=='')? die ('ERROR : Enter name') : mysql_escape_string($_POST['name']); // line 252

    echo $name;
}
?>

您也可以尝试相反的方法:

<form id="form1" name="form1" method="post" action="<?=$_SERVER['PHP_SELF']?>">
<input type="text" name="name" id="name" />
<br>
<input type="submit" name="submit" id="submit" value="Submit" />
<input name="reset" type="reset" id="reset" value="Reset" />
</form>

<?php
    if(isset($_POST['submit']))
    {
    //get from input
    //escape input values for greater safety
    $name=(trim($_POST['name'])=='')? die ('ERROR : Enter name') : mysql_escape_string($_POST['name']); // line 252
    }
    else
    {
die("You cannot do that from here.");
}

echo $name;

?>

答案 1 :(得分:1)

您的 <form> 没有您在此处使用过的名为“ name ”的输入字段

trim($_POST['name'])=='')

修复:

在表单上添加一个输入字段,如下所示..

<form id="form1" name="form1" method="post" action="<?=$_SERVER['PHP_SELF']?>">
      <input type="text" name="name" /><!-- Added here -->
      <input type="submit" name="submit" id="submit" value="Submit" />
      <input name="reset" type="reset" id="reset" value="Reset" />

您还缺少其他字段,如电话,联系人,电子邮件等....

答案 2 :(得分:0)

使用issetempty检查变量是否存在通常是个好主意。即使一个变量,例如$_POST['submit']存在,但不保证其他人也存在。

if (!empty($_POST['name'])) {
    …
}