Facebook
From Walloping Iguana, 1 Year ago, written in PHP.
Embed
Download Paste or View Raw
Hits: 151
  1. I want that if user enter same email or username already in db or even if they enter one not in db it show an alert on the same page with all the content but it is taking them to a new page and there is no content there the URL is same BTW
  2. Php part
  3. <?php
  4.  
  5. if(isset($_POST['submit'])){
  6.         require_once '_dbconnect.php';
  7.  
  8.         function sanitizeUserInput($userInput)
  9.         {
  10.                 global $conn;
  11.  
  12.                 return mysqli_escape_string($conn, stripcslashes($userInput));
  13.         }
  14.  
  15.         # COLLECT POST VARIABLES
  16.         $username = sanitizeUserInput($_POST['signupusername']);
  17.         $useremail = sanitizeUserInput($_POST['signupemail']);
  18.         $password = sanitizeUserInput($_POST['signuppassword']);
  19.         $password = password_hash($pass, PASSWORD_BCRYPT);
  20.  
  21.         $sqlToCheckUserEmailExists = $conn->query("SELECT * FROM Users WHERE user_email = '$useremail'");
  22.         $sqlTOCheckUsernameExists = $conn->query("SELECT * FROM Users WHERE user_name = '$username'");
  23.         if(!$sqlToCheckUserEmailExists || !$sqlTOCheckUsernameExists)
  24.         {
  25.                 echo "SQL FAILED HERE IS WHY " . mysqli_error($conn);
  26.                 exit;
  27.         }
  28.         $doEmailExist = mysqli_num_rows($sqlToCheckUserEmailExists);
  29.         $doUsernameExist = mysqli_num_rows($sqlTOCheckUsernameExists);
  30.         if ($doEmailExist != 0) {
  31.                 $showError = "Email Is In Use";
  32.                 echo '<div class="alert alert-danger d-flex align-items-center my-0" role="alert">
  33. <svg class="bi flex-shrink-0 me-2" width="24" height="24" role="img" aria-label="Success:"><use xlink:href="#check-circle-fill"/></svg>
  34. <div>
  35. <strong>Email already in use pls use a new one</strong>
  36. </div>
  37. </div>';
  38.                 exit;
  39.         }
  40.         elseif($doUsernameExist != 0)
  41.         {
  42.                 $showError = "Username Is In Use";
  43.         echo '<div class="alert alert-danger d-flex align-items-center my-0" role="alert">
  44. <svg class="bi flex-shrink-0 me-2" width="24" height="24" role="img" aria-label="Success:"><use xlink:href="#check-circle-fill"/></svg>
  45. <div>
  46. <strong>Username already in use pls use a new one</strong>
  47. </div>
  48. </div>';
  49.                 exit;
  50.         }
  51.         else
  52.         {
  53.                 $sqlToINsertUserIntoDatabase = $conn->query("INSERT INTO Users (sno, user_name, user_email, user_pass, user_date) VALUES(NULL, '$username', '$useremail', '$password', CURRENT_TIMESTAMP)");
  54.                 if (!$sqlToINsertUserIntoDatabase)
  55.                 {
  56.                         echo "FAILED TO INSERT USER INTO DATABSE, HERE IS WHY<b>" . mysqli_error($conn) . "</b>";
  57.  
  58.                 }
  59.                 else
  60.                 {
  61.                           echo '<div class="alert alert-success d-flex align-items-center my-0" role="alert">
  62. <svg class="bi flex-shrink-0 me-2" width="24" height="24" role="img" aria-label="Success:"><use xlink:href="#check-circle-fill"/></svg>
  63. <div>
  64. <strong>Successfully Signed up you can now login.</strong>
  65. </div>
  66. </div>';
  67.                         exit;
  68.                 }
  69.         }
  70.  
  71.  
  72. }
  73. ?>