获取应该发布的表单中的选择项的未定义索引

时间:2013-04-15 16:15:45

标签: php html forms select

为我的$ _POST ['state']获取未定义的索引错误无法理解为什么,在这里设置为表单中的select是代码。我不明白我是否需要以不同的方式设置选择,或者它是否只是不发布变量?

<html>

<?php

include("db.php");
if ((($_POST['email'])) and (($_POST['password'])) and (($_POST['name'])) and  (($_POST['password2'])) and (($_POST['name'])) and (($_POST['address'])) and (($_POST['city'])) and (($_POST['state'])))
{
    echo"<h1>variables are set</h1>";
    $con=mysql_connect("localhost", "useradmin", "FNEpsTa6qvmRjBBf", "userdata") or die("failed to connect");
    if ($con->connect_error) 
    {
        die('Connect Error (' . $con->connect_errno . ') '. $con->connect_error);
    }
    //Prevent SQL injections
    $name = mysql_real_escape_string($_POST['name']);
    $email = mysql_real_escape_string($_POST['email']);
    $address = mysql_real_escape_string($_POST['address']);
    $city = mysql_real_escape_string($_POST['city']);
    $state=mysql_real_escape_string($_POST['state']);
    $password = mysql_real_escape_string($_POST['password']);
    $password2 = mysql_real_escape_string($_POST['password2']);

    //Get MD5 hash of password
    $password = md5($_POST['password']);
    $query="SELECT email FROM users WHERE email = '".$email."'";
    $query2="INSERT INTO users (name, email, address, city, state, password) VALUES ('$name', '$email', '$address', '$city', '$state', '$password');";
    //Check to see if email exists
    $sql = mysql_query($con, $query2);

    $rows=(mysql_num_rows($sql));
    if($rows>0)
    {
        die ("email taken.");
    }
    if($password<>$password2)
    {
        die ("passwords do not match");
    }

    $sql = mysql_query($con, $query2);
    if($sql){
        header('location:welcome.php');
    }
}
?>

<html>
<center>





<form action="register.php" method="POST">
        <table>
        <tr><td>Name: </td><td><input name="name" type="text" /></td></tr>
        <tr><td>Email:</td><td> <input name="email" type="text" /></td></tr>
        <tr><td>Address: </td><td><input name="address" type="text" /></td></tr>
        <tr><td>City: </td><td><input name="city" type="text" /></td></tr>
        <tr><td>State: </td><td><select name="state"> 
            <option value="" selected="state">Select a State</option> 
            <option value="AL">Alabama</option> 
            <option value="AK">Alaska</option> 
            <option value="AZ">Arizona</option> 
            <option value="AR">Arkansas</option> 
            <option value="CA">California</option> 
            <option value="CO">Colorado</option> 
            <option value="CT">Connecticut</option> 
            <option value="DE">Delaware</option> 
            <option value="DC">District Of Columbia</option> 
            <option value="FL">Florida</option> 
            <option value="GA">Georgia</option> 
            <option value="HI">Hawaii</option> 
            <option value="ID">Idaho</option> 
            <option value="IL">Illinois</option> 
            <option value="IN">Indiana</option> 
            <option value="IA">Iowa</option> 
            <option value="KS">Kansas</option> 
            <option value="KY">Kentucky</option> 
            <option value="LA">Louisiana</option> 
            <option value="ME">Maine</option> 
            <option value="MD">Maryland</option> 
            <option value="MA">Massachusetts</option> 
            <option value="MI">Michigan</option> 
            <option value="MN">Minnesota</option> 
            <option value="MS">Mississippi</option> 
            <option value="MO">Missouri</option> 
            <option value="MT">Montana</option> 
            <option value="NE">Nebraska</option> 
            <option value="NV">Nevada</option> 
            <option value="NH">New Hampshire</option> 
            <option value="NJ">New Jersey</option> 
            <option value="NM">New Mexico</option> 
            <option value="NY">New York</option> 
            <option value="NC">North Carolina</option> 
            <option value="ND">North Dakota</option> 
            <option value="OH">Ohio</option> 
            <option value="OK">Oklahoma</option> 
            <option value="OR">Oregon</option> 
            <option value="PA">Pennsylvania</option> 
            <option value="RI">Rhode Island</option> 
            <option value="SC">South Carolina</option> 
            <option value="SD">South Dakota</option> 
            <option value="TN">Tennessee</option> 
            <option value="TX">Texas</option> 
            <option value="UT">Utah</option> 
            <option value="VT">Vermont</option> 
            <option value="VA">Virginia</option> 
            <option value="WA">Washington</option> 
            <option value="WV">West Virginia</option> 
            <option value="WI">Wisconsin</option> 
            <option value="WY">Wyoming</option>
        </select></td></tr>
        <tr><td>Password: </td><td><input type="password" name="password" /></td></tr>
        <tr><td>Confirm Password: </td><td><input type="password" name="password2" /></td></tr>
        <tr><td><input type="submit" value="Submit" /></td></tr>
    </table>
</form></center>


</html>

1 个答案:

答案 0 :(得分:1)

这是因为您正在访问$_POST数组而不检查密钥是否存在。在访问密钥之前,您必须检查该密钥是否存在:

if (isset($_POST['state'])) {
    $state = $_POST['state'];
}

如果您没有检查它是否已设置,那么如果表单未提交,您将收到错误:

$state = $_POST['state']; // Error