php - 单选按钮的验证

标签 php mysql

我尝试为新用户制作注册表。如果在字段中输入所有值,则该表单运行良好。但是当我直接提交表单时,我收到单选按钮的 mysql 错误。我还使用了一项功能来检查用户名是否已存在或不匹配密码。如果我将任何字段留空并按提交,然后页面变为空白,用户需要填写从开始开始的所有详细信息,并且用户名已存在的警报来了。我希望仅当用户名与数据库中的用户名相同时才显示此警报。请帮忙!

<?php session_start();
// define variables and set to empty values
$fname=$gender=$dept=$email=$uname=$pswd=$cpswd=$role="";
$fnameErr=$genderErr=$deptErr=$emailErr=$unameErr=$pswdErr=$cpswdErr=$roleErr="";
if ($_SERVER["REQUEST_METHOD"] == "POST")
    {
    if (empty($_POST["fname"])) 
            {
                $fnameErr = "Name is required";
            } 
    else 
            {
                $fname = test_input($_POST["fname"]);
                // check if name only contains letters and whitespace
                if (!preg_match("/^[a-zA-Z ]*$/",$fname)) 
        {
                        $fnameErr = "Only letters and white space allowed";
            } 
            }
    if (empty($_POST["gender"])) 
            {
                $genderErr = "Gender is required";
            } 
    else 
            {
                $gender = test_input($_POST["gender"]);
            }
    if (empty($_POST["dept"])) 
            {
                $deptErr = "Department is required";
            } 
    else 
            {
                $dept = test_input($_POST["dept"]);
            }
    if (empty($_POST["email"])) 
            {
                $emailErr = "Email is required";
            } 
    else 
            {
        $email = test_input($_POST["email"]);
                // check if e-mail address is well-formed
                if (!filter_var($email, FILTER_VALIDATE_EMAIL)) 
                    {
                        $emailErr = "Invalid email format"; 
                    }
            }
    if (empty($_POST["uname"])) 
            {
                $unameErr = "Username is required";
            }
    else 
            {
                $uname = test_input($_POST["uname"]);
            }
    if (empty($_POST["pswd"])) 
            {
                $pswdErr = "Password is required";
            } 
    else 
            {
                $pswd = test_input($_POST["pswd"]);
            }
    if (empty($_POST["cpswd"])) 
            {
                $cpswdErr = "Password is required";
            } 
    else 
            {
                $cpswd = test_input($_POST["cpswd"]);
            }
    if (empty($_POST["role"])) 
            {
                $roleErr = "Role is required";
            } 
    else 
            {
                $role = test_input($_POST["role"]);
            }
    if (!empty($_POST))
            { 
                $host="localhost"; // Host name 
                $username="root"; // Mysql username 
                $password=""; // Mysql password 
                $db_name="testmra"; // Database name 
                // Connect to server and select databse.
                $conn=mysqli_connect($host,$username,$password) or die("cannot connect"); 
                mysqli_select_db($conn,$db_name);
                $name = mysqli_real_escape_string($conn, $_POST['fname']);
                $gender =mysqli_real_escape_string($conn,$_POST['gender']);
                $department = mysqli_real_escape_string($conn, $_POST['dept']);
                $email = mysqli_real_escape_string($conn, $_POST['email']);
                $username = mysqli_real_escape_string($conn, $_POST['uname']);
                $userpass = mysqli_real_escape_string($conn, $_POST['pswd']);
        $cpass = mysqli_real_escape_string($conn, $_POST['cpswd']);
                $role= mysqli_real_escape_string($conn, $_POST['role']);
                $res=mysqli_query($conn,"SELECT username FROM newuser WHERE username='$username'");
                $row=mysqli_fetch_row($res);
                if($row>0)
                    {
                        echo '<script language="javascript">';
                        echo 'alert("Username '.$username.' already been selected")';
                        echo '</script>';
                    }
        elseif($userpass!=$cpass)
        {
            $cpswdErr="Password doesn't match";
        }
                else
                    {
                        $sql="INSERT INTO newuser (name,gender,department,emailid,username,userpass,role)VALUES('$name','$gender','$department','$email','$username','$userpass','$role')";
                        if (mysqli_query($conn,$sql))
                            {
                                header("location:trialregister.php");
                                exit();
                            }
                        else
                            {
                                die('Error: Cannot connect to db' );
                            }
                    }
            }       

}
else {  }
 function test_input($data) 
{
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
}
?>
<html>
<head><title>MRA</title></head>
<body>
<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<table align="center" cellspacing="5" cellpadding="5">
<tr><td align="right">Full Name :</td><td><input type="text" name="fname"></td><td align="left"><font color="red"><?php echo $fnameErr; ?></td></tr>
<tr><td align="right">Gender :</td><td><input type="radio" name="gender" value="Male">Male<input type="radio" name="gender" value="Female">Female</td><td align="left"><font color="red"><?php echo $genderErr; ?></td></tr>
<tr><td align="right">Department :</td><td><select name="dept">
<option value="">Select Department</option>
<option value="IT">IT</option>
<option value="HR">HR</option>
<option value="Accounts">Accounts</option>
<option value="Sales">Sales</option>
</select></td><td align="left"><font color="red"><?php echo $deptErr; ?></td></tr>
<tr><td align="right">EmailId :</td><td><input type="text" name="email"></td><td align="left"><font color="red"><?php echo $emailErr; ?></td></tr>
<tr><td align="right">Username :</td><td><input type="text" name="uname"></td><td align="left"><font color="red"><?php echo $unameErr; ?></td></tr>
<tr><td align="right">Password :</td><td><input type="password" name="pswd"></td><td align="left"><font color="red"><?php echo $pswdErr; ?></td></tr>
<tr><td align="right">Confirm Password :</td><td><input type="password" name="cpswd"></td><td align="left"><font color="red"><?php echo $cpswdErr; ?></td></tr>
<tr><td align="right">Role :</td><td><input type="radio" name="role" value="User">User<input type="radio" name="role" value="Admin">Admin</td><td align="left"><font color="red"><?php echo $roleErr; ?></td></tr>
<tr><td  colspan="3" align="center"><input type="submit" name="submit" value="Submit">&nbsp;<input type="reset" name="reset" value="Reset">&nbsp;<input type="button" name="cancel" value="Cancel"></td></tr>
</table>
</form>
</body>
</html>

最佳答案

试试这个...

我已将 if 条件从“if (!empty($_POST))”更改为“if ($roleErr ==="")”。因为如果您不选择单选按钮“$_POST['gender'], $_POST['role']"不显示您的帖子

<?php session_start();
// define variables and set to empty values
$fname=$gender=$dept=$email=$uname=$pswd=$cpswd=$role="";
$fnameErr=$genderErr=$deptErr=$emailErr=$unameErr=$pswdErr=$cpswdErr=$roleErr="";
if ($_SERVER["REQUEST_METHOD"] == "POST")
    {

    if (empty($_POST["fname"])) 
            {
                $fnameErr = "Name is required";
            } 
    else 
            {
                $fname = test_input($_POST["fname"]);
                // check if name only contains letters and whitespace
                if (!preg_match("/^[a-zA-Z ]*$/",$fname)) 
        {
                        $fnameErr = "Only letters and white space allowed";
            } 
            }
    if (empty($_POST["gender"])) 
            {
                $genderErr = "Gender is required";
            } 
    else 
            {
                $gender = test_input($_POST["gender"]);
            }
    if (empty($_POST["dept"])) 
            {
                $deptErr = "Department is required";
            } 
    else 
            {
                $dept = test_input($_POST["dept"]);
            }
    if (empty($_POST["email"])) 
            {
                $emailErr = "Email is required";
            } 
    else 
            {
        $email = test_input($_POST["email"]);
                // check if e-mail address is well-formed
                if (!filter_var($email, FILTER_VALIDATE_EMAIL)) 
                    {
                        $emailErr = "Invalid email format"; 
                    }
            }
    if (empty($_POST["uname"])) 
            {
                $unameErr = "Username is required";
            }
    else 
            {
                $uname = test_input($_POST["uname"]);
            }
    if (empty($_POST["pswd"])) 
            {
                $pswdErr = "Password is required";
            } 
    else 
            {
                $pswd = test_input($_POST["pswd"]);
            }
    if (empty($_POST["cpswd"])) 
            {
                $cpswdErr = "Password is required";
            } 
    else 
            {
                $cpswd = test_input($_POST["cpswd"]);
            }
    if (empty($_POST["role"])) 
            {
                $roleErr = "Role is required";
            } 
    else 
            {
                $role = test_input($_POST["role"]);
            }
    if ($roleErr =="") 
            { 
                $host="localhost"; // Host name 
                $username="root"; // Mysql username 
                $password=""; // Mysql password 
                $db_name="testmra"; // Database name 
                // Connect to server and select databse.
                $conn=mysqli_connect($host,$username,$password) or die("cannot connect"); 
                mysqli_select_db($conn,$db_name);
                $name = mysqli_real_escape_string($conn, $_POST['fname']);
                $gender =mysqli_real_escape_string($conn,$_POST['gender']);
                $department = mysqli_real_escape_string($conn, $_POST['dept']);
                $email = mysqli_real_escape_string($conn, $_POST['email']);
                $username = mysqli_real_escape_string($conn, $_POST['uname']);
                $userpass = mysqli_real_escape_string($conn, $_POST['pswd']);
        $cpass = mysqli_real_escape_string($conn, $_POST['cpswd']);
                $role= mysqli_real_escape_string($conn, $_POST['role']);
                $res=mysqli_query($conn,"SELECT username FROM newuser WHERE username='$username'");
                $row=mysqli_fetch_row($res);
                if($row>0)
                    {
                        echo '<script language="javascript">';
                        echo 'alert("Username '.$username.' already been selected")';
                        echo '</script>';
                    }
        elseif($userpass!=$cpass)
        {
            $cpswdErr="Password doesn't match";
        }
                else
                    {
                        $sql="INSERT INTO newuser (name,gender,department,emailid,username,userpass,role)VALUES('$name','$gender','$department','$email','$username','$userpass','$role')";
                        if (mysqli_query($conn,$sql))
                            {
                                header("location:trialregister.php");
                                exit();
                            }
                        else
                            {
                                die('Error: Cannot connect to db' );
                            }
                    }
            }       

}
else {  }
 function test_input($data) 
{
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
}
?>
<html>
<head><title>MRA</title></head>
<body>
<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<table align="center" cellspacing="5" cellpadding="5">
<tr><td align="right">Full Name :</td><td><input type="text" name="fname"></td><td align="left"><font color="red"><?php echo $fnameErr; ?></td></tr>
<tr><td align="right">Gender :</td><td><input type="radio" name="gender" value="Male">Male<input type="radio" name="gender" value="Female">Female</td><td align="left"><font color="red"><?php echo $genderErr; ?></td></tr>
<tr><td align="right">Department :</td><td><select name="dept">
<option value="">Select Department</option>
<option value="IT">IT</option>
<option value="HR">HR</option>
<option value="Accounts">Accounts</option>
<option value="Sales">Sales</option>
</select></td><td align="left"><font color="red"><?php echo $deptErr; ?></td></tr>
<tr><td align="right">EmailId :</td><td><input type="text" name="email"></td><td align="left"><font color="red"><?php echo $emailErr; ?></td></tr>
<tr><td align="right">Username :</td><td><input type="text" name="uname"></td><td align="left"><font color="red"><?php echo $unameErr; ?></td></tr>
<tr><td align="right">Password :</td><td><input type="password" name="pswd"></td><td align="left"><font color="red"><?php echo $pswdErr; ?></td></tr>
<tr><td align="right">Confirm Password :</td><td><input type="password" name="cpswd"></td><td align="left"><font color="red"><?php echo $cpswdErr; ?></td></tr>
<tr><td align="right">Role :</td><td><input type="radio" name="role" value="User">User<input type="radio" name="role" value="Admin">Admin</td><td align="left"><font color="red"><?php echo $roleErr; ?></td></tr>
<tr><td  colspan="3" align="center"><input type="submit" name="submit" value="Submit">&nbsp;<input type="reset" name="reset" value="Reset">&nbsp;<input type="button" name="cancel" value="Cancel"></td></tr>
</table>
</form>
</body>
</html>
<form action="" method="post">

  Why don't they play poker in the jungle?<br>
  <input type="radio" name="jungle" value="treefrog"> Too many tree frogs.<br>
  <input type="radio" name="jungle" value="cheetah"> Too many cheetahs.<br>
  <input type="radio" name="jungle" value="river"> Too many rivers.<br><br>

  Check the box if you want your answer to be graded:
  <input type="checkbox" name="grade" value="yes"><br><br>

  <input type="submit" name="submit" value="Submit"><br>

</form>

关于php - 单选按钮的验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30449446/

相关文章:

java - 实现 SSO 进行跨 Web 访问

javascript - 过滤包含一些文本的 PHP 数据表

mysql - SQL:将信息连接到主表中

mysql - 具有约束 "UNIQUE"的列如何在 mysql -server 5.7 中接受重复值

mysql - 从MYSQL中的3个表中选择数据

javascript - 通过 php 将数据发布到我的 AngularjS 数据库

php - PDO dblib nextRowset 不工作

php - 修改后的 PHP 表单不起作用

mysql - 尝试创建 sql 过程,使用 concat 时出现错误

mysql - 我们可以直接在 MySql 数据库上执行 Mdx 查询吗?如果可以,怎么办?