Php注册页面

Php注册页面,php,html,mysql,Php,Html,Mysql,我用php为我的网站创建了一个注册页面。在我的代码中,如果用户名、密码、确认密码和/或电子邮件为空,它会给出一个错误,确实如此,但它仍然表示用户是创建的。我不想那样。如果任何用户字段为空,我希望他们返回到注册页面并填写缺少的字段。它所做的只是给出echo语句,然后说用户是被创建的 sign up.php: <html> <head> <link rel="stylesheet" type="text/css" href="css.css"> <t

我用php为我的网站创建了一个注册页面。在我的代码中,如果用户名、密码、确认密码和/或电子邮件为空,它会给出一个错误,确实如此,但它仍然表示用户是创建的。我不想那样。如果任何用户字段为空,我希望他们返回到注册页面并填写缺少的字段。它所做的只是给出echo语句,然后说用户是被创建的

sign 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
            }    
}
?>

当验证失败时,您需要重定向到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..    
}

首先检查您与数据库的连接,如果该字段是可为空的,也检查您的用户表,如果是可为空的,那么您可能需要在代码上放置一些try-catch块以查看错误。

我会将一些注册和处理代码放在signup.php中的同一页面中(注意,我还删除了“操作”)表单标记中的代码:

<?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";
}

注册
在下面注册








当某些内容为空时,您需要使进程停止。您的逻辑已关闭,因此
if{…}else{…}
。以及所有
if(empty($username))
等。之前没有分配任何内容。所有这些内容都属于
if(isset($\u POST['submit']){…
中,并且这是在错误的位置
if($\u POST['password]=$\u POST['password2']{
那么如果($username\u result>0){
这个选择查询在哪里?我所有的代码都在that@Fred-ii-如果密码相等,我应该把它们放在哪里?我所有的代码都在if(isset($_POST['submit']){…}中下面给了您一个答案。您是否可以详细说明一下您的建议。我收到了这个错误(!)解析错误:语法错误,意外'{'在C:\wamp\www\interpage\process2.php的第5行,这是因为@pritesh忘记关闭上面第一行的左括号。我在代码中忘记了几个右括号。它现在是固定的。它现在可以工作了。但是当用户单击sumit时,如何将它发送到进程文件?您不需要进程文件。您可以将任何进程放入其中你想把g代码放在我为你评论的底部部分。如果我把我的流程代码放在底部,它表明用户已经创建,我没有输入任何必需的字段。那么,如果字段都已填写,我如何将其发送到流程文件?你能解释一下你的意思吗?你不需要流程文件。上面的文件是d我分为两部分,一部分用于显示表单,另一部分用于处理。将所有处理代码放在我将注释放在else if部分的位置。(注意:您可以将else if…更改为else,但我将条件保留在那里,以便您更容易理解这是表单处理的地方。)
<?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";
}