Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.07 KB | None | 0 0
  1. <?php
  2. $error = [];
  3. $result = null;
  4. if (isset($_POST['signupBtn'])){
  5.  
  6.     $name = trim($_POST['name']);
  7.     $email = trim($_POST['email']);
  8.     $hashed_password = password_hash($pwd, PASSWORD_DEFAULT);
  9.  
  10.     // check if user already exist
  11.     $statement = $dbh->prepare("SELECT username FROM signup WHERE username =: name");
  12.     $statement->bindParam(':name', $name);
  13.  
  14.     if ($statement->execute()) {
  15.         if ($statement->rowCount() > 0) {
  16.             $error[] = 'User Name Taken";
  17.         }
  18.     }
  19.  
  20.     $statement = $dbh->prepare("SELECT username FROM signup WHERE email = :email");
  21.     $statement->bindParam(':email', $email);
  22.  
  23.     if ($statement->execute()) {
  24.         if ($statement->rowCount() > 0) {
  25.             $error[] = 'Email Address Taken";
  26.         }
  27.     }
  28.  
  29.     // you cannot use if/else since you can have username exists OR email exists OR both
  30.  
  31.     if (!$error) {
  32.         $insert="INSERT INTO signup(username, email, password) VALUES(:name, :email, :pwd)";
  33.         $statement = $dbh->prepare($insert);
  34.         $statement->bindParam(':name', $name);
  35.         $statement->bindParam(':email', $email);
  36.         $statement->bindParam(':pwd', $hashed_password);
  37.         if ($statement->execute() && $statement->rowCount() > 0) {
  38.             $result = "row inserted";
  39.         } else {
  40.             $result = "insertion failed";
  41.         }
  42.     }
  43. }
  44. ?>
  45. <html>
  46. <div class="form-group">
  47.             <label for="exampleInputUsername">Username</label>
  48.             <input type="text" name="name" class="form-control" id="exampleInputUsername" placeholder="enter username">
  49.             <span class="error_message"><?php echo $nameErr;?></span>
  50.           </div>
  51.  
  52.           <div class="form-group">
  53.               <label for="exampleInputEmail1">Email address</label>
  54.               <input type="email" name="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email">
  55.               <span class="error_message"><?php echo $emailErr;?></span>
  56.               <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
  57.             </div>
  58.  
  59.     <button type="submit" name="signupBtn" class="btn btn-primary">SIGNUP</button>
  60.     <?php echo $implode('<br>', $error); ?>
  61.     <?php echo $result; ?>
  62. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement