0

另一个问题在这里。我创建了将数据插入数据库。然后,我收到错误通知:未定义索引:第 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>
4

3 回答 3

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;

?>
于 2013-11-06T17:37:58.450 回答
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" />

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

于 2013-11-06T17:27:18.120 回答
0

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

if (!empty($_POST['name'])) {
    …
}
于 2013-11-06T17:30:05.557 回答