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

标签 php mysql forms post

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

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

You did not complete all of the required fields. Press the back button on your browser to try again."

关于我做错了什么有什么建议吗?我是新手。

这是我的 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

 }
 ?>`     

最佳答案

首先,您没有在 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.');
}

Edit: Important Note. Please migrate to mysqli_* functions instead, as you are using a deprecated API.

关于当表单完全填写时,PHP 表单验证在提交时显示空白字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24103443/

相关文章:

php - 将十六进制代码转换为字符

php - 通过比较两个数据库表来删除匹配值 Codeigniter/Mysql

php - Laravel 4 中的用户身份验证

html - 在 HTML 中为按钮设置 Action

php - 添加集合/实体会使表单渲染非常慢

javascript - 防止后退按钮漏洞利用? PHP

python - 从表中选择将截断它

datetime - MySQL 中重叠日期时间范围的总和

c# - 在 C# 中将字典作为参数传递给 Http.Post

javascript - 表单嵌入 Symfony2 数据原型(prototype)为 Null