表单提交时出现简单的php“Undefined index”错误

时间:2014-05-02 06:07:26

标签: php mysql

除了$_POST$fname$mname之外,我为每个$lname项都收到此错误。我不明白为什么。我已经搜索了一个解决方案,并且isset的建议是最常见的,但我已经尝试了几种不同的形式,它仍然不适合我。

注意:未定义的索引:第13行的C:\ xampp \ htdocs \ php \ signin.php中的邮政编码

注意:未定义的索引:第15行的C:\ xampp \ htdocs \ php \ signin.php中的家庭电话

注意:未定义的索引:第16行的C:\ xampp \ htdocs \ php \ signin.php中的mobphone

signin.php

<?php

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

    $fname=$_POST['firstname'];
    $mname=$_POST['middlename']; 
    $lname=$_POST['lastname'];
    $dob=$_POST['dateofbirth'];
    $address=$_POST['address'];
    $city=$_POST['city'];
    $state=$_POST['state'];
    $country=$_POST['country'];
    $postcode=$_POST['postcode'];
    $email=$_POST['email'];
    $homephone=$_POST['homephone'];
    $mobphone=$_POST['mobphone'];
    $preferred=$_POST['preferred'];

    include_once("includes/link.php");

    $sql=("INSERT INTO `member` VALUES ('$fname', '$mname', '$lname', '$dob', '$address', '$city', '$state', '$country', '$postcode', '$email', '$homephone', '$mobphone', 'preferred')");

    mysqli_query($link, $sql);

    mysqli_close($link);
    }
?>

link.php

<?php

    $hostname = "localhost";
    $user = "root";
    $password = "********";
    $link = mysqli_connect($hostname, $user , $password);

    if (!$link){
    echo "server is being lame";
    exit;
    };
    mysqli_select_db($link, "cp2cwonrlvDB3");
    if (!mysqli_select_db($link, "cp2cwonrlvDB3")){
    echo "Could not connect brudda";
    };

?>

表格html:

<html>
<head>
<title>Example form</title>
<style type="text/css">

label
{
width: 100px;
float: left;
text-align: right;
margin-right: 0.5em;
display: block
}

.reset {
margin-left:50px;
}

h3 {
margin-left:15px;
}

</style>
<script>
<?php
    include 'includes/connection.php'
?>
</script>
</head>

<body>

<div class="container">
    <h2>Sign up to CWON</h2>
    <form method="POST" action="signin.php" novalidate>

        <label for="fisrtname">First Name:</label>
        <input type="text" name="firstname" required placeholder="Enter your first name" maxlength="25"><br />

        <label for="middlename">Middle Name:</label>
        <input type="text" name="middlename" placeholder="Enter your middle name"  maxlength="25"><br />

        <label for="lastname">Last Name:</label> 
        <input type="text" name="lastname" required placeholder="Enter your last name"  maxlength="25"><br />

        <label for="dateofbirth">Date of Birth:</label> 
        <input type="date" name="dateofbirth" required maxlength="25"><br />

        <label for="address">Address:</label> 
        <input type="text" name="address" required maxlength="100" placeholder="Enter your home address"><br />

        <label for="city">City:</label> 
        <input type="text" name="city" required maxlength="100" placeholder="Enter your city"><br />

        <label for="state">State:</label> 
        <select name="state">
            <option value="NSW">NSW</option>
            <option value="QLD">QLD</option>
            <option value="VIC">VIC</option>
            <option value="SA">SA</option>
            <option value="WA">WA</option>
            <option value="NT">NT</option>
            <option value="TAS">TAS</option>
        </select> <br />

        <label for="country">Country:</label> 
        <input type="text" name="country" required maxlength="100" placeholder="Enter your country"><br />

        <h3>Preferred contact:</h3>
        <label for="preferred">Home Phone:</label>
        <input type="radio" name="preferred" value="Home Phone"><br /><br />

        <label for="preferred">Mobile Phone:</label>
        <input type="radio" name="preferred" value="Mobile Phone"><br /><br />

        <label for="preferred">Email:</label>
        <input type="radio" name="preferred" value="Email"><br /><br />

        <label for="homenumber">Home Number:</label>
        <input type="text" name="homenumber" placeholder="Enter your home number"><br />

        <label for="mobnumber">Mobile Number:</label>
        <input type="text" name="mobnumber" placeholder="Enter your mobile number"><br />

        <label for="email">Email Address:</label> 
        <input type="email" name="email" required maxlength="100" placeholder="Enter your email address"><br />

        <label for="postcode">Postcode:</label>
        <input type="number" name="postcode" required min="1000" max="9999" placeholder="4-digit"><br />

        <label for="occupation">Occupation:</label> 
        <input type="text" name="occupation" maxlength="50" placeholder="Enter your occupation"><br />

        <label for="hobbies">Hobbies:</label> 
        <input type="text" name="hobbies" maxlength="200" placeholder="separate with commas"><br />

        <label for="interest">Interests:</label> 
        <select name="interest">
            <option value="Fund Raisers">Fund Raisers</option>
            <option value="Domestic Volunteering">Domestic Volunteering</option>
            <option value="Foreign Volunteering">Foreign Volunteering</option>
            <option value="All of the above">All of the above</option>
        </select> <br /><br />

        <label for="terms">Terms and Conditions</label> 
        <input type="checkbox" name="terms"><br /><br /><br />

        <label for="newsletter">Sign up for the CWON newsletter</label> 
        <input type="checkbox" name="newsletter"><br /><br /><br /><br />

        <input type="reset" name="reset" value="Reset" class="reset" onclick="set_focus()" />
        <input type="submit" name="submit" value="Submit" class="submit">

    </form>
</div>

4 个答案:

答案 0 :(得分:4)

看到$数组中有$_POST。全部删除它们。

 $dob=$_POST['$dateofbirth'];
              ^------------------ That (Remove them all)

编辑:

您需要更改邮政编码的名称,因为它反映了手机。

 <label for="postcode">Postcode:</label>
 <input type="number" name="postcode" required min="1000" max="9999" placeholder="4-digit"><br />

现在将它们改为......

$homephone=$_POST['homenumber']; //<--- Should be homenumber.`
$mobphone =$_POST['mobnumber'];
$postcode =$_POST['postcode'];

答案 1 :(得分:2)

在尝试访问参数之前,您需要检查并确保有值。这将确保在您尝试使用它们之前设置它们。这可以使用isset()函数完成。例如:

// check values exist before using them
if(isset($_POST['$dateofbirth'])){
    $dob=$_POST['$dateofbirth'];
}

此外,您的SQL易受注入攻击。在查询中使用它们之前,您确实应该清理它们的参数。 MySQLi建议使用参数化查询或PDO查询。

请注意最终错误如何不包含$,因此删除完全无法解决问题。

答案 2 :(得分:1)

变化:

$dob=$_POST['$dateofbirth'];

要:

$dob=$_POST['dateofbirth'];

所有人都一样。

答案 3 :(得分:0)

您没有姓名&#39; homephone&#39;和&#39; mobphone&#39;,只是&#39; homenumber&#39;和&#39; mobnumber&#39;

$homephone=$_POST['homenumber'];
$mobphone=$_POST['mobnumber'];