简体   繁体   中英

Php registration page

I created a sign up page for my website in php . In my code I put if the username , password confirm password and/or email is empty it would give then an error , which it does , but it still says that the user was created . I don't want that to happen . If any of the user fields are empty I want them to go back to the sign up page and fill out the missing fields . All it does is give the echo statement and then it says user was created .

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

So basically what I tried is putting the if the fields are empty statements before the code that sends the information to the database it doesn't work . I tried putting it after the code that sends the information to database it didn't work . I also tried putting it on the sign up page but it didn't work either .

What I want to happen is if any of the fields are empty nothing will be sent to the database . Can someone help me ?

update :

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

    ?>

You need to redirect to signup.php when validation fails..For this you can check the input and if either of it empty then you can redirect in process2.php as:

if(empty($username) || empty($pw) || empty($pw2) || empty($email))
{
    header( "refresh:5;url=signup.php" ); 
} else {
     //You can do your things here..    
}

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

I would place some of the signup and process code in the same page like this in signup.php (notice I have also removed the "action" code from the form tag:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM