Gcobani Mkontwana

Gcobani Mkontwana

  • 566
  • 1.9k
  • 405.9k

Signup registration is not registering users in db record

Feb 26 2023 3:12 PM

Hi Team

I am having a problem with my back end, i am using php to register new users but somehow its not registering them instead it throws an exception i created for password incorrect. 

// Back end

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

 


Answers (1)