我尝试为新用户制作注册表单。如果在字段中输入所有值,表单效果很好。但是当我直接提交表单时,我收到单选按钮的mysql错误。我还使用了一项功能来检查用户名是否已经存在或者没有&匹配密码。如果我留下任何字段空白&按提交然后页面变为空白&用户需要填写从开始和放大的所有细节用户名已经存在的警报来了。我想只在用户名与db相同时显示此警报。请帮助!
<?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"> <input type="reset" name="reset" value="Reset"> <input type="button" name="cancel" value="Cancel"></td></tr>
</table>
</form>
</body>
</html>
答案 0 :(得分:2)
试试这个......
我已经改变了条件来自&#34; if(!empty($ _ POST))&#34; to&#34; if($ roleErr ==&#34;&#34;)&#34;。因为如果你没有选择单选按钮&#34; $ _ POST [&#39;性别&#39;], $ _POST [&#39;作用&#39;]&#34;不提供你的帖子
<?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"> <input type="reset" name="reset" value="Reset"> <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>