当表单完全填写时,PHP表单验证在提交时显示空白字段

时间:2014-06-08 06:01:52

标签: php mysql forms post

当表单完全填满时,我的PHP表单验证在提交时显示空白字段。

我不断收到我在add.php页面上创建的错误代码:

  

您未完成所有必填字段。按浏览器上的后退按钮再试一次。"

关于我做错了什么的任何建议?我是新手。

这是我的php代码:

//This code runs if the form has been submitted

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



 //This makes sure they did not leave any fields blank

 if (!$_POST['firstname'] | !$_POST['lastname'] | !$_POST['email'] | !$_POST['address']               | !$_POST['phonenumber'] | !$_POST['birthday'] | !$_POST['gender'] |     !$_POST['referralsource'] | !$_POST['username'] | !$_POST['password'] |     !$_POST['confirmpassword'] ) {

        die('You did not complete all of the required fields.  Press the back button on your browser to try again.');

}



 // checks if the email is in use

if (!get_magic_quotes_gpc()) {

    $_POST['email'] = addslashes($_POST['email']);

}

$usercheck = $_POST['email'];

$check = mysql_query("SELECT email FROM dbs WHERE email = '$usercheck'") 

or die(mysql_error());

$check2 = mysql_num_rows($check);



//if the name exists it gives an error

if ($check2 != 0) {

    die('Sorry, the email '.$_POST['email'].' is already in use.');

            }


// checks if the username is in use

 if (!get_magic_quotes_gpc()) {

    $_POST['username'] = addslashes($_POST['username']);

}

$usercheck = $_POST['username'];

$check = mysql_query("SELECT username FROM db WHERE username = '$usercheck'") 

or die(mysql_error());

 $check2 = mysql_num_rows($check);
// this makes sure both passwords entered match

if ($_POST['password'] != $_POST['confirmpassword']) {

    die('Your passwords did not match.  Press the back button on your browser to try again.');

}



// here we encrypt the password and add slashes if needed

$_POST['password'] = md5($_POST['password']);

if (!get_magic_quotes_gpc()) {

    $_POST['password'] = addslashes($_POST['password']);

    $_POST['username'] = addslashes($_POST['username']);

        }



 // now we insert it into the database

$insert = "INSERT INTO db (firstname, lastname, email, address1, address2, phonenumber, birthday, gender, referralsource, username, password)

        VALUES ('".$_POST['firstname']."', '".$_POST['lastname']."', '".$_POST['email']."', '".$_POST['address']."', '".$_POST['address2']."', '".$_POST['phonenumber']."', '".$_POST['birthday']."', '".$_POST['gender']."', '".$_POST['referralsource']."', '".$_POST['username']."', '".$_POST['password']."')";

$add_member = mysql_query($insert);

?>

 <h1>Registered</h1>

 <p>Thank you, you have registered - you may now <a href="login.php">login</a>.</p>

 <?php 
 } 

 else 
 {  
 ?>

这是我的表格

<tr><td>First Name:</td><td>

<input type="text" name="firstname" maxlength="60">
</td></tr> 
<tr><td>Last Name:</td><td>

<input type="text" name="lastname" maxlength="60">
</td></tr> 

<tr><td>Email:</td><td>

<input type="text" name="email" maxlength="60">
</td></tr> 

<tr><td>Address:</td><td>

<input type="text" name="address1" maxlength="60">
</td></tr> 

<tr><td>City, State, Zip:</td><td>

<input type="text" name="address2" maxlength="60">
</td></tr> 

<tr><td>Phone Number:</td><td>

<input type="text" name="phonenumber" maxlength="13">
</td></tr> 


<tr><td>Birthday:</td><td>

<input type="text" name="birthday" maxlength="60">

</td></tr>
<tr><td>Gender:</td><td>

<input type="text" name="gender" maxlength="15">

</td></tr>

<tr><td>Referral Source:</td><td>

<input type="text" name="referralsource" maxlength="60">



</td></tr> <tr><td>Username:</td><td>

<input type="text" name="username" maxlength="20">

</td></tr>

<tr><td>Password:</td><td>

<input type="password" name="password" maxlength="20">

</td></tr>

<tr><td>Confirm Password:</td><td>

<input type="password" name="confirmpassword" maxlength="20">

</td></tr>

<tr><th colspan=2><input type="submit" name="submit" 
value="Register"></th></tr> </table>

</form>


<?php

 }
 ?>`     

1 个答案:

答案 0 :(得分:1)

首先,您没有正确使用if条件中的OR,它应该是||

相反,如果文本框输入为空,则可以使用isset()。考虑这个例子:

if (!isset($_POST['firstname'], $_POST['lastname'], $_POST['email'], $_POST['address1'], $_POST['phonenumber'], $_POST['birthday'], $_POST['gender'], $_POST['referralsource'],$_POST['username'], $_POST['password'], $_POST['confirmpassword']) ) {
    die('You did not complete all of the required fields.  Press the back button on your browser to try again.');
}
  

编辑:重要说明。请改为迁移到mysqli_*函数,因为您使用的是已弃用的API。