我在php中为我的网站创建了一个注册页面。在我的代码中,我把如果用户名,密码确认密码和/或电子邮件为空它会给出一个错误,它确实如此,但它仍然说用户已创建。我不希望这种情况发生。如果任何用户字段为空,我希望他们返回注册页面并填写缺少的字段。它所做的只是给出echo语句,然后它表示用户已创建。
注册up.php:
<html>
<head>
<link rel="stylesheet" type="text/css" href="css.css">
<title>Sign Up</title>
</head>
<body bgcolor="#E6E6FA">
<h2 style="text-align: right"><b style="font-size: 25px">Sign Up Below</b></h2>
<form name="registration" method="post" action="process2.php">
<p align="right"><input type="text" name="username" size="35" id="Username" placeholder="User Name" /></p>
<br></br>
<p align="right"><input type="password" name="password" size="35" id="p w" placeholder="Password" /></p>
<br></br>
<p align="right"><input type="password" name="password2" size="35" id="pw2" placeholder="Confirm Password" /></p>
<br></br>
<p align="right"><input type="text" name="email" size="35" id="Email" placeholder="E-mail" /></p>
<p align="right"><input type="submit" name="submit" value="submit"></p>
</form>
<h3 style="font-size: 20px"><a href="register.php">Go Back To Home Screen</a> </h3>
</body>
</html>
<?php
if(empty($username)){ echo"Please enter a username to sign up.<br />";} else {}
if(empty($pw)){echo"Please enter a password to sign up.<br />";} else {}
if(empty($pw2)){echo"Please confirm your password to sign up.<br />";} else {}
if(empty($email)){ echo"Please enter a email to sign up.<br />";} else {}
?>
Process2.php:
<?php
include("db.php");
if(empty($username)){ echo"Please enter a username to sign up.<br />";} else {}
if(empty($pw)){echo"Please enter a password to sign up. <br />";} else {}
if(empty($pw2)){echo"Please confirm your password to sign up.<br />";} else {}
if(empty($email)){ echo"Please enter a email to sign up.<br />";} else {}
if (isset($_POST['submit'])) {
if ($_POST['password'] == $_POST['password2']) {
$username = $_POST['username'];
$pw = $_POST['password'];
$pw2 = $_POST['password2'];
$email = $_POST['email'];
// validate and sanitize all of these inputs
// and see that they are not blank at the same time
// Do your MySql here to find the $username and
// bring out result of find in $username_result
if($username_result > 0){
echo "This username is in use.<a href= signup.php>Enter a different username</a> ";
// exit; // or send them back to registration page
} else {
// it is not in use so put it in
$pw = password_hash($pw, PASSWORD_BCRYPT, array('cost' => 10));
$pw2 = password_hash($pw2, PASSWORD_BCRYPT, array('cost' => 8));
$sql = "INSERT into users VALUES(null, '$username', '$pw', '$pw2', '$email')";
if(mysqli_query($conn, $sql)){
// if insert checked as successful echo username and password saved successfully
echo"Your user was created. <a href= signin.php>Click here to login </a><br />";
}else{
echo "Sorry there has been an error, please try again."; // and send them back to registration page
}
}
}else{
echo "The passwords do not match. <a href= signup.php>Try again</a><br />"; // and send them back to registration page
}
}
?>
所以基本上我尝试的是在将信息发送到数据库的代码之前将字段设置为空语句,它不起作用。我尝试将它放在将信息发送到数据库的代码之后,它没有工作。我也尝试将它放在注册页面上,但它也没有用。
我想要发生的是,如果任何字段为空,则不会将任何内容发送到数据库。有人能帮我吗 ?
更新:
<?php
if (empty($_POST['username']) || empty($_POST['password']) || empty($_POST['password2']) || empty($_POST['email']) || empty($_POST['submit'])) {
$error = "";
if (!empty($_POST['submit'])){
if(empty($_POST['username']))
$error .= "Please enter a username. ";
if(empty($_POST['password']))
$error .= "Please enter a password. ";
if(empty($_POST['password2']))
$error .= "Please confirm your password. ";
if(empty($_POST['email']))
$error .= "Please enter your email. ";
}
include("db.php");
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css.css">
<title>Sign Up</title>
</head>
<body bgcolor="#E6E6FA">
<h2 style="text-align: right"><b style="font-size: 25px">Sign Up Below</b></h2>
<form name="registration" method="post">
<p align="right"><input type="text" name="username" size="35" id="Username" placeholder="User Name" /></p>
<br></br>
<p align="right"><input type="password" name="password" size="35" id="p w" placeholder="Password" /></p>
<br></br>
<p align="right"><input type="password" name="password2" size="35" id="pw2" placeholder="Confirm Password" /></p>
<br></br>
<p align="right"><input type="text" name="email" size="35" id="Email" placeholder="E-mail" /></p>
<p align="right"><input type="submit" name="submit" value="submit"></p>
</form>
<?php
if ($error)
echo $error;
?>
<h3 style="font-size: 20px"><a href="register.php">Go Back To Home Screen</a> </h3>
</body>
</html>
<?php
}
else if (($_POST['submit'])){
if (isset($_POST['submit'])) {
if ($_POST['password'] == $_POST['password2']) {
$username = $_POST['username'];
$pw = $_POST['password'];
$pw2 = $_POST['password2'];
$email = $_POST['email'];
// validate and sanitize all of these inputs
// and see that they are not blank at the same time
// Do your MySql here to find the $username and
// bring out result of find in $username_result
if($username_result > 0){
echo "This username is in use.<a href= signup.php>Enter a different username</a> ";
// exit; // or send them back to registration page
} else {
// it is not in use so put it in
$pw = password_hash($pw, PASSWORD_BCRYPT, array('cost' => 10));
$pw2 = password_hash($pw2, PASSWORD_BCRYPT, array('cost' => 8));
$sql = "INSERT into users VALUES(null, '$username', '$pw', '$pw2', '$email')";
if(mysqli_query($conn, $sql)){
// if insert checked as successful echo username and password saved successfully
echo"Your user was created. <a href= signin.php>Click here to login </a><br />";
}else{
echo "Sorry there has been an error, please try again."; // and send them back to registration page
}
}
}else{
echo "The passwords do not match. <a href= signup.php>Try again</a><br />"; // and send them back to registration page
}
}
?>
答案 0 :(得分:0)
您需要在验证失败时重定向到signup.php。为此您可以检查输入,如果其中任何一个为空,那么您可以在process2.php中重定向为:
if(empty($username) || empty($pw) || empty($pw2) || empty($email))
{
header( "refresh:5;url=signup.php" );
} else {
//You can do your things here..
}
答案 1 :(得分:0)
首先检查您与数据库的连接,如果字段可以为空,则检查您的用户表如果是,那么您可能想在代码上放置一些try-catch块以查看错误。
答案 2 :(得分:0)
我会在signup.php中将一些注册和处理代码放在同一页面中(注意我还从表单标签中删除了“action”代码:
<?php
if (empty($_POST['username']) || empty($_POST['password']) || empty($_POST['password2']) || empty($_POST['email']) || empty($_POST['submit'])){
$error = "";
if (!empty($_POST['submit'])){
if(empty($_POST['username']))
$error .= "Please enter a username. ";
if(empty($_POST['password']))
$error .= "Please enter a password. ";
if(empty($_POST['password2']))
$error .= "Please confirm your password. ";
if(empty($_POST['email']))
$error .= "Please enter your email. ";
}
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css.css">
<title>Sign Up</title>
</head>
<body bgcolor="#E6E6FA">
<h2 style="text-align: right"><b style="font-size: 25px">Sign Up Below</b></h2>
<form name="registration" method="post">
<p align="right"><input type="text" name="username" size="35" id="Username" placeholder="User Name" /></p>
<br></br>
<p align="right"><input type="password" name="password" size="35" id="p w" placeholder="Password" /></p>
<br></br>
<p align="right"><input type="password" name="password2" size="35" id="pw2" placeholder="Confirm Password" /></p>
<br></br>
<p align="right"><input type="text" name="email" size="35" id="Email" placeholder="E-mail" /></p>
<p align="right"><input type="submit" name="submit" value="submit"></p>
</form>
<?php
if ($error)
echo $error;
?>
<h3 style="font-size: 20px"><a href="register.php">Go Back To Home Screen</a> </h3>
</body>
</html>
<?php
}
else if (!empty($_POST['submit'])){
//place most of the code from your process.php code here
echo "processing";
}