PHP表单验证

I am have created a form and having problems validating it as I am getting a few errors when I try to preview it in the browser.

The errors

Undefined index: username in  on line 269
Undefined index: email in  on line 270
Undefined index: fname in  on line 271
Undefined index: lname in  on line 272
Undefined index: pnumber in  on line 273
Undefined index: address in  on line 274
Undefined index: password in  on line 275

**my php scripts starts on line  35 and ends on line 131**
 <!--DB connection-->     (line 35)
<?php
$localhost = "localhost";
$dbuser = "student";
$dbpass = "student";
$dbname = "Curiosity_Pizza";

$connect = mysqli_connect($localhost,$dbuser,$dbpass)or die ("Could not connect to database!");;
mysqli_select_db($connect, "$dbname" );
?>

<!-- inserting form data in to DB-->

<?php
$username =$_POST['username'];
$email =$_POST['email'];
$fname =$_POST['fname'];
$lname =$_POST['lname'];
$pnumber =$_POST['pnumber'];
$address =$_POST['address'];
$password = sha1($_POST['password']);   

$inssert = 'INSERT into client(username, email, fname, lname, pnumber ,address,  password) VALUES ("'.$username.'","'.$email.'","'.$fname.'","'.$lname.'", "'.$pnumber.'", "'.$address.'","'.$password.'")';

mysqli_query($connect,$inssert);    
?>

<!--Registration Valadation-->
<?php

//define varibles and sst to empty (w3Schhols)
$usernameErr = $emailErr = $fnameErr = $lnameErr = $pnumberErr = $addressErr = $passwordErr = "";
$username = $email = $fname = $lname = $pnumber = $address = $password = "";


if ($_SERVER["REQUEST_METHOD"]== "POST"){
    $username = test_input($_POST["username"]);
    $email =  test_input($_POST["email"]);
    $fname = test_input($_POST["fname"]);
    $lname = test_input($_POST["lname"]);
    $pnumber = test_input($_POST["pnumber"]);
    $address = test_input($_POST["address"]);
    $password = test_input($_POST["passoerd"]); 
}

function test_input($data){
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}

if ($_SERVER["REQUEST_METHOD"]== "POST"){
    if (empty($_POST["userename"])){
        $usernameErr = "Userename is a required field";
    }else{
     $username = test_input($_POST["username"]);
    }

    if (empty($_POST["email"])){
        $emailErr = "Email is a required field";
    }else{
     $email = test_input($_POST["email"]);
    }

    if (empty($_POST["fname"])){
        $fnameErr = "First Name is a required field";
    }else{
     $fname = test_input($_POST["fname"]);
    }

    if (empty($_POST["lanme"])){
        $lnameErr = "Last Name is a required field";
    }else{
     $lname = test_input($_POST["lname"]);
    }

    if (empty($_POST["pnumber"])){
        $pnumberErr = "Phone Number is a required field";
    }else{
     $pnumber = test_input($_POST["$number"]);
    }

    if (empty($_POST["address"])){
        $addressErr = "Address is a required field";
    }else{
     $address = test_input($_POST["address"]);
    }

    if (empty($_POST["$password"])){
        $passwordErr = "Password is a required field";
    }else{
     $password = test_input($_POST["password"]);
    }
}   (line 131)

**My html/form not sure if its relevant low**
<form method="post"  action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" />
                                <table width="600" border="0">
                                  <tr>
                                    <th><label for="Username">Username:</label></th>
                                    <td><input type="text" name="username" />
                                    <span class="error">*<?php echo $usernameErr;?></span></td>
                                  </tr>
                                  <tr>
                                    <th><label for="email">email:</label></th>
                                    <td><input type="email" name="email" />
                                    <span class="error">*<?php echo $emailErr;?></span></td>
                                  </tr>
                                  <tr>
                                    <th><label for="fname">First Name:</label></th>
                                    <td><input type="text" name="fname" />
                                    <span class="error">*<?php echo $fnameErr;?></span></td>
                                  </tr>
                                  <tr>
                                    <th><label for="lname">Last Name:</label></th>
                                    <td><input type="text" name="lname" />
                                    <span class="error">*<?php echo $lnameErr;?></span></td>
                                  </tr>
                                  <tr>
                                    <th><label for="pnumber">Phone Number:</label></th>
                                    <td><input type="number" name="pnumber" />
                                    <span class="error">*<?php echo $pnumberErr;?></span></td>
                                  </tr>
                                  <tr>
                                    <th><label for="address">Address:</label></th>
                                    <td><input type="text"  name="address" />
                                    <span class="error">*<?php echo $addressErr;?></span></td>
                                  </tr>
                                  <tr>
                                    <th><label for="password">Passowrd:</label</th>                                   <td><input type="password"  name="password"   />
                                    <span class="error">*<?php echo       $passwordErr;?></span></td>
                                  </tr>                                                                         
                                  <tr>
                                    <td><input type="submit" name="Submit"</td>
                                  </tr>
                                </table>
                            </form>

I have tried reading the other posts on here but have not found anything that would work.

The errors means that the array does not contain the index. Make sure that your $_POST contains username... etc. Try

print_r($_POST)

And make sure it contains "username."

You can also use

array_key_exists("username", $_POST)

to make sure that $_POST contains username.

array_key_exists: http://php.net/manual/en/function.array-key-exists.php

TL;DR

Make sure $_POST contains "username" and any other index that PHP complains is undefined.

Check on the form that is posting information to make sure that the names match up with the parameters you're using.

For instance, you should have something like <input name='username'> somewhere in your form, etc.

Also, you should always do:

if( isset($_POST[INDEX]) )
{
    // Stuff with $_POST[INDEX]
}

to make sure that the POST was successfully received.

Also, you can try using Fiddler:

http://www.telerik.com/fiddler

to figure out what is/isn't being posted.

Don't use $_POST['username'] as it might not contain username, first check if there is actually a value with isset isset($_POST['username']) ? $_POST['username'] : null

or better, create a function to handle input data for example:

<?php
function post($index, $default = null) {
    return isset($_POST[$index]) ? $_POST[$index] : $default;
}

//Use it like that:
$username = post('username');
$email = post('email');
$fname= post('fname');
...

Edit: Added more example for better clarity.