Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP表单验证提交空白数据_Php_Validation_Login - Fatal编程技术网

PHP表单验证提交空白数据

PHP表单验证提交空白数据,php,validation,login,Php,Validation,Login,我试图创建一个页面,允许用户使用PHP编辑他们的详细信息,PHP在提交内容之前验证内容。 我想允许用户更新他们的用户名,第一个和第二个名字和电子邮件地址 验证包括: <?php if(preg_match("/^[0-9a-zA-Z_]{3,}$/", $_POST["username"]) == 0) $error_username = '<li>Usernames may contain only digits, upper and lower case letters a

我试图创建一个页面,允许用户使用PHP编辑他们的详细信息,PHP在提交内容之前验证内容。 我想允许用户更新他们的用户名,第一个和第二个名字和电子邮件地址

验证包括:

<?php
if(preg_match("/^[0-9a-zA-Z_]{3,}$/", $_POST["username"]) == 0)
$error_username = '<li>Usernames may contain only digits, upper and lower case letters and underscores</li>';

if(preg_match("/^[A-Za-z]+$/", $_POST["fname"]) == 0)
$error_fname = '<li>First Name may contain upper and lower case letters</li>';

if(preg_match("/^[A-Za-z]+$/", $_POST["sname"]) == 0)
$error_sname = '<li>Second Name may contain upper and lower case letters</li>';

if(preg_match("/^[a-zA-Z]\w+(\.\w+)*\@\w+(\.[0-9a-zA-Z]+)*\.[a-zA-Z]{2,4}$/", $_POST["email"]) == 0)
$error_email = '<li>Email Addresses must have a valid email address format</li>';

else header("Location: edit.php");  
?>

和显示错误:

<ul>
<?php  if(isset($error_username)) echo $error_username; ?>
<?php  if(isset($error_fname)) echo $error_fname; ?>
<?php  if(isset($error_sname)) echo $error_sname; ?>
<?php  if(isset($error_email)) echo $error_email; ?>
</ul>
我的表格是:

<form name="edit_account" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input class="form_field" name="username" type="text" value="<?php echo $_POST["username"]; ?>" placeholder="Username">
<input class="form_field" name="fname" type="text" value="<?php echo $_POST["fname"]; ?>" placeholder="First Name">
<input class="form_field" name="sname" type="text" value="<?php echo $_POST["sname"]; ?>" placeholder="Second Name">
<input class="form_field" name="email" type="text" value="<?php echo $_POST["email"]; ?>" placeholder="Email Address">
<input type="submit" name="Submit" value="Update Account">
</form>

看起来您正在从验证脚本重定向到edit.php(其中包含数据库插入代码)。问题是$\u POST变量在重定向时被重置

我将
包括('path/to/edit.php')
edit.php脚本,而不是重定向到它。如果不可能,我会将$\u POST数据保存在$\u会话变量中


希望这对您将它们发布到验证页面有所帮助,但在重定向到
edit.php
页面时会丢失它们。在转到
edit.php
之前,将信息存储在会话变量中,如下所示:

$_SESSION['username'] = $_POST["username"];
// other variables also
edit.php
上,不是从
$\u POST
中拉,而是从
$\u会话中拉

旁注


不要忘记每页顶部的
session\u start()
。此外,在使用用户输入时,您应该仔细检查。

您的验证将导致许多合法名称和电子邮件地址失败。
session_start()加载到某个地方?因为您正在使用会话。如果没有,请执行。@Fred ii-是的,它位于文档的顶部。我认为您应该在客户端执行输入验证,而不是serverside@MatthewJohnson说得好!“mysql\函数已弃用”*???OP正在使用
mysqli
。您不能将
real\u escape\u string
mysql\u*
函数一起使用。您是对的,正在编辑。每当我看到未准备好的语句时,我都会假设mysql。对不起!
$_SESSION['username'] = $_POST["username"];
// other variables also