未定义的索引PHP:找不到表单值

时间:2013-09-25 18:51:37

标签: php mysql forms

完成新手到PHP,但尝试做一个简单的表单提交到MySQL数据库。我一直收到通知:未定义的索引:表单输入变量的警告。这可能是什么问题。表单位于提交到process.php文件的单独PHP页面上。

表格代码:

<form name="example" id="example" method="POST" action="process.php" enctype="multipart/form-data">
    <p>
        <label for="first_name">First Name:</label>
        <input size="20" name="first_name" id="first_name" />
    </p>
    <p>
        <label for="city">city</label>
        <input size="20" name="city" id="city" />
    </p>
    <p>
        <label for="state">state</label>
        <input size="20" name="state" id="state" />
    </p>
    <p>
        <label for="zip">zip</label>
        <input size="20" name="zip" id="zip" />
    </p>
    <p>
        <label for="email">e-mail</label>
        <input size="20" name="email" id="email" />
    </p>
    <p>
        <label for="form_upload">file</label>
        <input size="40" type="file" name="form_data" id="form_data" />
    </p>
    <p>
        <input type="submit" value="Submit form" name="submit" />
    </p>
</form>

Process.php

<?php
   mysql_connect("localhost", 'root', '') or die('Could not connect:' . mysql_error());
   mysql_select_db("practice") or die(mysql_error());
   mysql_query("CREATE TABLE user (id INT(5)NOT NULL AUTO, name VARCHAR(30), email     VARCHAR(40), city VARCHAR(40), state VARCHAR(3), zip INT(5) )");
   if (isset($_POST)) {
       $name  = $_POST['first_name'];
       $email = $_POST['email'];
       $city  = $_POST['city'];
       $state = $_POST['state'];
       $zip   = $_POST['zip'];
   }
   mysql_query("INSERT INTO user VALUES('$name', '$email', '$city', '$state', '$zip')");
?>

1 个答案:

答案 0 :(得分:0)

我想你应该改变这个

    if (isset($_POST)){

并提供类似的索引

  if (isset($_POST['submit'])){

以及

将查询更改为

  mysql_query("INSERT INTO user (name , email, city,state, zip)
               VALUES('$name', '$email', '$city', '$state', '$zip')" );

并且在插入之前你应该像那样转义变量。

  $name = mysql_real_escape_string($name) ;
 // and so on 

最后一件事是你每次提交时创建表格的原因?你应该检查一下是否存在。