PHP服务器端表单验证。将空表单字段插入数据库
我是PHP新手,正在尝试进行服务器端表单验证。有两个PHP文件PHP服务器端表单验证。将空表单字段插入数据库,php,Php,我是PHP新手,正在尝试进行服务器端表单验证。有两个PHP文件Login.PHP和Form.PHP。注册在Login.php中完成,验证在Form.php中完成。其思想是Form.php将处理Login.php 我的问题是:即使form字段为空,变量仍然被插入到数据库中 如果是空的,我不想插入。相反,它必须返回到Login.php,并将错误消息存储为会话变量 我已经使用检查了表单字段!使用if..else子句在Form.php中设置()和empty。在if..else子句中,您可以找出表单字段是
Login.PHP
和Form.PHP
。注册在Login.php
中完成,验证在Form.php
中完成。其思想是Form.php
将处理Login.php
我的问题是:即使form
字段为空,变量仍然被插入到数据库中
如果是空的,我不想插入。相反,它必须返回到Login.php
,并将错误消息存储为会话变量
我已经使用检查了表单
字段!使用if..else
子句在Form.php
中设置()和empty
。在if..else
子句中,您可以找出表单字段是否为空,如果为空,则必须转到会话变量子句(在if
条件中)。相反,它将进入else
条件,并将变量中的空值(“$username”、“$password”、“$phone”、“$mailid”、“$city”)插入数据库中
我已经阅读了之前关于类似问题的问题,甚至检查了Youtube的服务器端验证。我做错了什么?会话变量的使用是否存在问题。好心协助
Login.php:
<!Doctype HTML>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href= "Form.css" />
<script src="Form.js" type="text/javascript"></script>
</head>
<body>
<?php
session_start();
$passworderr='';
if(isset($_SESSION["passworderr"])) {
$passworderr=$_SESSION["passworderr"];
}
?>
<div id="Outer">
<div id="left" >
<form action="/DatabaseDrivenWebpage/Form.php" method="POST" name="form">
<p><label>Username</label> <input type="text" name="regusername" placeholder="Your name"/> </p>
<p><label>Password</label> <input type="text" name="regpassword" placeholder="Password"/> </p>
<input type="Submit" value="Login" />
</form>
</div>
<div id="right">
<form action="/DatabaseDrivenWebpage/Form.php" method="POST" id="formm">
<p>*Username <input required name="username" type="text" /><?php //echo $usernameerr;?></p>
<p>*Password <input name="password" type="password" /> <?php echo $passworderr;?></p>
<p> *Phone <input name="phone" type="tel" /><?php //echo $phoneerr;?></p>
<p> *MailId <input name="mailid" type="email" /><?php //echo $mailiderr;?></p>
<p> *City <input name="city" type="text" /><?php //echo $cityerr;?></p>
<input type="Submit" value="Signup" />
</form></div></div></body></html>
用户名
密码
*用户名
*密码
*电话
*MailId
*城市
Form.php:
<?php
session_start();
$dbservername='localhost';$dbname='mani';$dbusername='root';$dbpassword='';
$dbconn=mysqli_connect($dbservername,$dbusername,$dbpassword);
if(!$dbconn){
die("Connection failed:". mysqli_connect_error());
}
if(!isset($_POST["username"])) {
$_SESSION["usernameerr"]="UserName is required";
}
else{
$username=mysqli_real_escape_string($dbconn,$_POST["username"]);
}
if(!isset($_POST["password"])) {
$_SESSION["passworderr"]="Enter a password";
}
else{
$password=mysqli_real_escape_string($dbconn,$_POST["password"]);
}
if(!isset($_POST["phone"])) {
$_SESSION["phoneerr"]="Phone number is required";
}
else{
$phone=mysqli_real_escape_string($dbconn,$_POST["phone"]);
}
if(!isset($_POST["mailid"])) {
$_SESSION["mailiderr"]="Enter a valid mail id";
}
else{
$mailid=mysqli_real_escape_string($dbconn,$_POST["mailid"]);
}
if(!isset($_POST["city"])) {
$_SESSION["cityerr"]="Enter your resident city";
}
else{
$city=mysqli_real_escape_string($dbconn,$_POST["city"]);
}
$selected = mysqli_select_db($dbconn,"$dbname")
or die("Could not select examples".mysqli_error($dbconn));
if(isset($_POST["username"]) and isset($_POST["password"]) and isset($_POST["phone"]) and isset($_POST["mailid"]) and isset($_POST["city"]) )
{
$res=mysqli_query($dbconn,"Insert into user(username,password,phone,mailid,city) values('$username','$password','$phone','$mailid','$city')");
if($res)
{
header("location:Login.php");
}
}
else
{
print "Problem in inserting";
header("location:Login.php");
}
mysqli_close($dbconn);
?>
关于isset
的一点是,它检查变量是否存在,因此允许包含空字符串的变量,就像您所做的那样。当当前表单在没有任何用户输入的情况下提交时,它提交的是一大堆包含空字符串的变量
现在的解决方案是将所有isset()
更改为empty()
,这将解决您的问题
[注意]不需要像这样同时使用isset()
和empty()
:
if(!isset($_POST["username"]) && $_POST["username"]!="")
function DoesPostFormFieldHaveValue($formFieldName) {
return(
isset($_POST[$formFieldName])
&& strlen($_POST[$formFieldName]) > 0
);
}
if(!isset($\u POST['fieldname'])和&!empty($\u POST['fieldname']))
因为empty()
正在做isset()所做的一切。检查如下:
if(!isset($_POST["username"]) && $_POST["username"]!="")
function DoesPostFormFieldHaveValue($formFieldName) {
return(
isset($_POST[$formFieldName])
&& strlen($_POST[$formFieldName]) > 0
);
}
您的PHP代码只检查isset,我没有看到任何空检查。在您的情况下,isset对于任何一个表单都是真的,因为表单字段正在提交-只是值为空。
要防止空插入,请添加!空头支票到你的条件。您的条件语句应该如下所示-
if(!isset($_POST['fieldname']) && !empty($_POST['fieldname']))
首先,session\u start
应该始终是您需要在其上使用会话的php页面的第一行
另外,我不知道为什么要使用这么多会话变量来存储错误。与此相反,使用单个会话变量,将其声明为数组,并将所有错误存储在其中
这是您的最新表格:-
<?php
session_start();
if((isset($_SESSION['errors']))) //check if we have errors set by the form.php page
{
echo "Please fix the following errors";
foreach($_SESSION['errors'] as $error) //loop through the array
{
echo $error;
}
}
?>
<!Doctype HTML>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href= "Form.css" />
<script src="Form.js" type="text/javascript"></script>
</head>
<body>
<div id="Outer">
<div id="left" >
<form action="/DatabaseDrivenWebpage/Form.php" method="POST" name="form">
<p><label>Username</label> <input type="text" name="regusername" placeholder="Your name"/> </p>
<p><label>Password</label> <input type="text" name="regpassword" placeholder="Password"/> </p>
<input type="Submit" value="Login" />
</form>
</div>
<div id="right">
<form action="/DatabaseDrivenWebpage/Form.php" method="POST" id="formm">
<p>*Username <input required name="username" type="text" /><?php //echo $usernameerr;?></p>
<p>*Password <input name="password" type="password" /> <?php echo $passworderr;?></p>
<p> *Phone <input name="phone" type="tel" /><?php //echo $phoneerr;?></p>
<p> *MailId <input name="mailid" type="email" /><?php //echo $mailiderr;?></p>
<p> *City <input name="city" type="text" /><?php //echo $cityerr;?></p>
<input type="Submit" value="Signup" />
</form></div></div></body></html>
用户名
密码
*用户名
*密码
*电话
*MailId
*城市
后端处理文件:-
<?php
session_start();
$_SESSION['errors'] = array(); //declare an array
$dbservername='localhost';$dbname='mani';$dbusername='root';$dbpassword='';
$dbconn=mysqli_connect($dbservername,$dbusername,$dbpassword);
if(!$dbconn){
die("Connection failed:". mysqli_connect_error());
}
if((!isset($_POST["username"])) || (empty($_POST['username']))) {
$_SESSION["errors"][]="UserName is required"; //push error message to array if $_POST['username'] is empty or is not set
}
else{
$username=mysqli_real_escape_string($dbconn,$_POST["username"]);
}
if((!isset($_POST["password"])) || (empty($_POST['password']))) {
$_SESSION["errors"][]="Enter a password";
}
else{
$password=mysqli_real_escape_string($dbconn,$_POST["password"]);
}
if((!isset($_POST["phone"])) || (empty($_POST['phone']))) {
$_SESSION["errors"][]="Phone number is required";
}
else{
$phone=mysqli_real_escape_string($dbconn,$_POST["phone"]);
}
if((!isset($_POST["mailid"])) || (empty($_POST['mailid']))) {
$_SESSION["errors"][]="Enter a valid mail id";
}
else{
$mailid=mysqli_real_escape_string($dbconn,$_POST["mailid"]);
}
if((!isset($_POST["city"])) || (empty($_POST['city']))) {
$_SESSION["errors"][]="Enter your resident city";
}
else{
$city=mysqli_real_escape_string($dbconn,$_POST["city"]);
}
$selected = mysqli_select_db($dbconn,"$dbname")
or die("Could not select examples".mysqli_error($dbconn));
if(count($_SESSION['errors']) < 1) //check if the the $_SESSION['errors'] count is less than 1 (0), this means there are no errors.
{
$res=mysqli_query($dbconn,"Insert into user(username,password,phone,mailid,city) values('$username','$password','$phone','$mailid','$city')");
if($res)
{
header("location:Login.php");
}
}
else
{
print "Problem in inserting";
header("location:Login.php");
}
mysqli_close($dbconn);
?>
首先是一点建议。如果你想开始一个新项目,我建议你学习如何使用PDO连接到MySQL数据库,而不是MySQLi。因为PDO是更好的方法,而且安全(特别是在使用准备好的语句时)
无论如何,正如我看到的,您正在将错误存储在多个$\u seisson
变量中,但是在完成验证检查之后,您没有执行正确的if
语句
与其这样做,不如:
if(isset($u POST[“username”])和isset($u POST[“password”])以及isset($u POST[“phone”])和isset($u POST[“mailid”])和isset($u POST[“city”])
这样做:
if(!isset($u SESSION['usernameerr'])和&!isset($u SESSION['passworderr'])和&!isset($u SESSION['phoneerr']和&!isset($u SESSION['cityerr'])
应该有用
我建议的另一个想法是unset
错误会话,在您的情况下,我会在Login.php
页面的末尾这样做。以防万一,如果您修复表单输入并再次提交,就不会有任何问题
另一件事,基于unset
的想法。如果您愿意这样做,那么更改错误会话的设置将比以下方式更简洁:
$\u会话['cityer']
致:
$\会话['errors']['cityerr']
因此,您可以在一个命令中清除特定的表单错误会话,如下所示:
unset($\u会话['errors']);
希望有帮助;)有很多方法可以做到这一点。服务器端存在一个空表单字段,其值为空。因此,除了检查变量是否已设置外,在本例中还需要检查值是否为非空。
一种方法是使用函数。
你可以举个例子:
if(!isset($_POST["username"]) || strlen($_POST["username"]) == 0) {
注意:不要使用该函数,因为字符串“0”被视为“空”。阅读手册了解其他此类情况
您可能想考虑使用辅助函数来进行确定。基本上是这样的:
if(!isset($_POST["username"]) && $_POST["username"]!="")
function DoesPostFormFieldHaveValue($formFieldName) {
return(
isset($_POST[$formFieldName])
&& strlen($_POST[$formFieldName]) > 0
);
}
他们使用的mysqli没有被弃用。@johncode Oops,我错过了iSee my post中关于为什么不使用空函数进行验证的内容。虽然通常建议使用PDO,但mysqli很好。只要他们不使用mysql,就没有理由指出这一点。@Neo是的,我需要取消设置,但让我先完成这个错误。谢谢alot@Neo在Login.php中使用atlast。所有内容都已修复now@Lambo,不要使用会话\u unset
,因为您可能会在会话中存储其他内容,如登录会话或任何其他内容。如果您使用session\u unset
它将删除所有会话内容,这不是我的建议。这是完整的