Gcobani Posted February 26, 2023 Share Posted February 26, 2023 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"> Quote Link to comment https://forums.phpfreaks.com/topic/315952-signup-registration-not-working-for-register-new-users-to-the-database/ Share on other sites More sharing options...
mac_gyver Posted February 26, 2023 Share Posted February 26, 2023 9 minutes ago, Gcobani said: $_POST["cpassword"] ^ that's not the name you gave the form field. Quote Link to comment https://forums.phpfreaks.com/topic/315952-signup-registration-not-working-for-register-new-users-to-the-database/#findComment-1606043 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.