Jump to content

Signup registration not working for register new users to the database


Recommended Posts

Hi Team

 

I need some help with the following back end logic, i have code that register new users to the database. Currently its not registering new users instead it goes to my if else statement when password is not matching.

 

// back end code

<?php
    
$showAlert = false;
$showError = false;
$exists=false;
    
if($_SERVER["REQUEST_METHOD"] == "POST") {
    
    // Include file which makes the
    // Database Connection.
    include 'db_config.php';
    
    $username = $_POST["username"];
    $password = $_POST["password"];
    $cpassword = $_POST["cpassword"];
            
    
    $sql = "Select * from signup where username='$username'";
    
    $result = mysqli_query($conn, $sql);
    
    $num = mysqli_num_rows($result);
    
    // This sql query is use to check if
    // the username is already present
    // or not in our Database
    if($num == 0) {
        if(($password == $cpassword) && $exists==false) {
    
            $hash = password_hash($password,
                                PASSWORD_DEFAULT);
                
            // Password Hashing is used here.
            $sql = "INSERT INTO `signup` ( `username`,
                `password`, `date`) VALUES ('$username',
                '$hash', current_timestamp())";
    
            $result = mysqli_query($conn, $sql);
    
            if ($result) {
                $showAlert = true;
            }
        }
        else {
            $showError = "Passwords do not match";
        }    
    }// end if
    
if($num>0)
{
    $exists="Username not available";
}
    
}//end if
    
?>

<?php
    
    if($showAlert) {
    
        echo ' <div class="alert alert-success 
            alert-dismissible fade show" role="alert">
    
            <strong>Success!</strong> Your account is 
            now created and you can login. 
            <button type="button" class="close"
                data-dismiss="alert" aria-label="Close"> 
                <span aria-hidden="true">×</span> 
            </button> 
        </div> '; 
    }
    
    if($showError) {
    
        echo ' <div class="alert alert-danger 
            alert-dismissible fade show" role="alert"> 
        <strong>Error!</strong> '. $showError.'
    
       <button type="button" class="close" 
            data-dismiss="alert aria-label="Close">
            <span aria-hidden="true">×</span> 
       </button> 
     </div> '; 
   }
        
    if($exists) {
        echo ' <div class="alert alert-danger 
            alert-dismissible fade show" role="alert">
    
        <strong>Error!</strong> '. $exists.'
        <button type="button" class="close" 
            data-dismiss="alert" aria-label="Close"> 
            <span aria-hidden="true">×</span> 
        </button>
       </div> '; 
     }
   
?>
    
    
// front end

<form action="signup.php" method="post" class="relative z-5  wow fadeInUp">
                            <div class="form-group relative mb-25 mb-sm-20">
                                <input type="text" class="form-control input-lg input-white shadow-5" id="username" placeholder="Username" name="username" required>
                                <i class="far fa-user transform-v-center"></i>
                            </div>
                            <div class="form-group relative mb-25 mb-sm-20">
                                <input type="email" class="form-control input-lg input-white shadow-5" id="email" placeholder="Email" name="email" required>
                                <i class="far fa-envelope transform-v-center"></i>
                            </div>
                            <div class="form-group relative mb-20 mb-sm-20">
                                <input type="password" class="form-control input-lg input-white shadow-5" id="password" placeholder="Password" name="password" required>
                                <i class="fas fa-lock transform-v-center"></i>
                            </div>
                            <div class="form-group relative mb-20 mb-sm-20">
                                <input type="password" class="form-control input-lg input-white shadow-5" id="confirmpassword" placeholder="Confirm Password" name="confirmpassword" required>
                                <i class="fas fa-lock transform-v-center"></i>
                            </div>
                            <div class="form-group form-check pl-0">
                                <div class="d-flex justify-content-between">
                                    <div class="custom-control custom-checkbox">
                                        <input type="checkbox" class="custom-control-input" id="customCheck1" checked="">
                                        <label class="custom-control-label fs-13" for="customCheck1"><span class="label-check">Remember me</span></label>
                                    </div>
                                </div>
                            </div>
                            <button type="submit" class="btn btn-square btn-block shadow-4 mt-20">SIGNUP</button>
                            <div class="signup-login text-center">

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.