doumouyi4039 2015-07-16 18:10
浏览 18

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.

  • 写回答

3条回答 默认 最新

  • duanchao4445 2015-07-16 18:13
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100