我的注册表单和验证它的脚本有问题(检查它是否有空白)。具体来说,如果注册表单中有一个或多个字段留空,则我的注册表单中的提交按钮的值会不断弹出错误消息。
以下是我用来检查注册表单中的空白的代码的摘录。
case "Register":
//Checks for blank fields
var_dump($_POST);
foreach($_POST as $fields=>$rvalue)
{
if($rvalue=="Button")
{
continue;
}
//Checks if fields other than rdesc are empty
if($fields!=="rdesc")
{
if(empty($rvalue))
{
//stores the empty field into an array
$blanks[]=$rvalue;
}
else
{
$good_data[$fields]=strip_tags(trim($rvalue));
}
}
}
//If there are bank fields, the following code will execute.
if(isset($blanks))
{
$errormsg="The fields below cannot be blank.Please fill all the fields: ";
extract($good_data);
$errormsg.="$rvalue ";
include("Register.php");
exit();
}
include("LoginPage.php");
?>
这是我的注册表
<?php
$regfields=array("remail"=>"Email","remail2"=>"Re-enter Email","rusername"=>"Username","rpassword"=>"Password","rpassword2"=>"Re-enter Password","rbiz"=>"BizName","rdesc"=>"Desc");
?>
<html>
<body>
<h3>Registration</h3>
<form action="Login.php" name="RegisterForm" method="post" >
<table border="0">
<?php
if(isset($errormsg))
{
echo"$errormsg";
}
foreach($regfields as $field=>$value)
{
if($field=="rdesc")
{
echo"<tr>";
echo"<td>";
echo"<label for='$field'>$value: </label>";
echo"</td>";
echo"<td>";
echo"<input type='text' name='$field' size='300' maxlength='300' style='width:400px;height:100px; \n' /></td></tr>";
echo"<tr>";
echo"<td>";
This Register value keeps showing up--->echo"<input type='submit' name='Button' value='Register'/>";
echo"</td>";
echo"<tr>";
}
else if($field=="remail"||$field=="remail2")
{
echo"<tr>";
echo"<td>";
echo"<label for='$field'>$value: </label>";
echo"</td>";
echo"<td>";
echo"<input type='text' name='$field' size='40' maxlength='50' /></td></tr>";
}
else if($field=="rpassword"||$field=="rpassword2")
{
echo"<tr>";
echo"<td>";
echo"<label for='$field'>$value: </label>";
echo"</td>";
echo"<td>";
echo"<input type='password' name='$field' size='40' maxlength='50' /></td></tr>";
}
else
{
echo"<tr>";
echo"<td>";
echo"<label for=$field>$value: </label>";
echo"</td>";
echo"<td>";
echo"<input type='text' name='$field' size='40' maxlength='50'/>";
echo"</td>";
echo"</tr>";
}
}
?>
我尝试使用 var dump 来查看里面有什么$_POST
,我得到了:
array(8) { ["remail"]=> string(5) "Email" ["remail2"]=> string(5) "Email" ["rusername"]=> string(0) "" ["rpassword"]=> string(6) "Testpw" ["rpassword2"]=> string(6) "Testpw" ["rbiz"]=> string(8) "Testname" ["rdesc"]=> string(8) "TestDesc" ["Button"]=> string(8) "Register" } **Register***<---This is the register value i was talking about that is outside of the array.
和错误信息
以下字段不能为空。请填写所有字段:注册
但是,当我填写没有任何空白的注册表单时,var dump 给了我:
array(8) { ["remail"]=> string(5) "Email" ["remail2"]=> string(5) "Email" ["rusername"]=> string(8) "TestUser" ["rpassword"]=> string(6) "Testpw" ["rpassword2"]=> string(6) "Testpw" ["rbiz"]=> string(8) "Testname" ["rdesc"]=> string(8) "TestDesc" ["Button"]=> string(8) "Register" }
数组外部的“寄存器”消失了。
我已经添加了代码
if($rvalue=="Button")
{
continue;
}
尝试跳过提交按钮的“注册”值,但它似乎不起作用。
有人可以告诉我如何从错误消息中删除“注册”值,以便正确显示留空的字段吗?
编辑:这些是完整的代码
登录.php
session_start();
switch ($_POST["Button"]) { case "登录": include("cxn.inc"); $cxn=mysqli_connect($host,$user,$pw,$db) or die("无法连接数据库"); $query="SELECT * FROM Users WHERE username='$_POST[lusername]'"; $result=mysqli_query($cxn,$query) or die (" Cant find Username from Users table"); $num=mysqli_num_rows($result);
if($num>0)//Username is found
{
$query="SELECT * FROM Users WHERE Password='$_POST[lpassword]'";
$result2=mysqli_query($cxn,$query)or die("Cant find Password");
$num2=mysqli_num_rows($result2);
if($num2>0)//Password is found
{
$_SESSION['Auth']="Yes";
$_SESSION['Type']="Admin";
header("Location:AdminMain.php");
}
else
{
echo"Invalid Username and/or Password";
}
}
elseif($num==0)//Executes when Username not found in Users table
{
$query="SELECT * FROM SubUsers WHERE Username='$_POST[lusername]'";
$result3=mysqli_query($cxn,$query) or die(" Cant find Username from SubUsers table");
$num3=mysqli_num_rows($result3);
if($num3>0)//Username is found
{
$query="SELECT * FROM SubUsers WHERE Password='$_POST[lpassword]'";
$result4=mysqli_query($cxn,$query);
$num4=mysqli_num_rows($result4);
if($num4>0)//Password is found
{
$_SESSION['Auth']="Yes";
$_SESSION['Type']="Sub";
header("Location:SubMain.php");
}
else
{
echo"Cant find Password";
}
}
else
{
echo"Username and/or Password is non-existent";
}
}
else
{
echo"Invalid Username and/or Password entered";
}
break;
case "Register":
//Checks for blank fields
var_dump($_POST);
foreach($_POST as $fields=>$rvalue)
{
if($fields=="Button"&& $rvalue == "Register")
{
continue;
}
//Checks if fields other than rdesc are empty
if($fields!=="rdesc")
{
if(!empty($rvalue))
{
//stores the empty field into an array
$blanks[]=$fields;
}
else
{
$good_data[$fields]=strip_tags(trim($rvalue));
}
}
}
if(!empty($blanks))
{
$errormsg="The fields below cannot be blank.Please fill all the fields: ";
extract($good_data);
$errormsg.="$rvalue ";
include("Register.php");
exit();
}
}
包括(“登录页面.php”);
?>