Advertisement
Guest User

Untitled

a guest
Mar 31st, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.88 KB | None | 0 0
  1. <?php
  2.  
  3. if (isset($_POST['btnRegister'])) {
  4.  
  5.     try {
  6.         $pdo = new PDO('mysql:host=host;dbname=name', 'username', 'password');
  7.     } catch (PDOException $e) {
  8.         exit($e->getMessage());
  9.     }
  10.  
  11.     $username = $_POST['username'];
  12.     $email = $_POST['email'];
  13.     $password = $_POST['password'];
  14.     $password2 = $_POST['password2'];
  15.  
  16.     if (empty($username)) {
  17.         $error = 'username is empty';
  18.     } elseif (empty($email) || filter_var($email, FILTER_VALIDATE_EMAIL)) {
  19.         $error = 'email must be a valid email address';
  20.     } elseif (empty($password)) {
  21.         $error = 'you must enter a password';
  22.     } elseif (strcmp($password, $password2) !== 0) {
  23.         $error = 'your repeat password must be the same as your password';
  24.     } else {
  25.         $selectSql = $pdo->prepare('SELECT COUNT(*) FROM users WHERE username = ? OR email = ?');
  26.         $selectSql->execute(array($username, $email));
  27.         if($selectSql->fetchcolumn(0) == 0) {
  28.             $insertSql = $pdo->prepare('INSERT INTO users VALUES (null, ?, ?, ?)');
  29.             if ($insertSql->execute(array($username, $email, password_hash($password, PASSWORD_BCRYPT)))) {
  30.                 $success = 'user has been created';
  31.             } else {
  32.                 $error = 'error creating the user';
  33.             }
  34.         } else {
  35.             $error = 'username or email is taken';
  36.         }
  37.     }
  38.  
  39. }
  40.  
  41. ?>
  42.  
  43. <form method="post">
  44.     <div>
  45.         <?=isset($error) ? "Error! {$error}" : ''?>
  46.         <?=isset($success)  ? "Success! {$success}" : ''?>
  47.     </div>
  48.     <input type="text" name="username" placeholder="username">
  49.     <input type="email" name="email" placeholder="email">
  50.     <input type="password" name="password" placeholder="password">
  51.     <input type="password" name="password2" placeholder="retype password">
  52.     <input type="submit" name="btnSubmit" value="Register">
  53. </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement