Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- if (isset($_POST['btnRegister'])) {
- try {
- $pdo = new PDO('mysql:host=host;dbname=name', 'username', 'password');
- } catch (PDOException $e) {
- exit($e->getMessage());
- }
- $username = $_POST['username'];
- $email = $_POST['email'];
- $password = $_POST['password'];
- $password2 = $_POST['password2'];
- if (empty($username)) {
- $error = 'username is empty';
- } elseif (empty($email) || filter_var($email, FILTER_VALIDATE_EMAIL)) {
- $error = 'email must be a valid email address';
- } elseif (empty($password)) {
- $error = 'you must enter a password';
- } elseif (strcmp($password, $password2) !== 0) {
- $error = 'your repeat password must be the same as your password';
- } else {
- $selectSql = $pdo->prepare('SELECT COUNT(*) FROM users WHERE username = ? OR email = ?');
- $selectSql->execute(array($username, $email));
- if($selectSql->fetchcolumn(0) == 0) {
- $insertSql = $pdo->prepare('INSERT INTO users VALUES (null, ?, ?, ?)');
- if ($insertSql->execute(array($username, $email, password_hash($password, PASSWORD_BCRYPT)))) {
- $success = 'user has been created';
- } else {
- $error = 'error creating the user';
- }
- } else {
- $error = 'username or email is taken';
- }
- }
- }
- ?>
- <form method="post">
- <div>
- <?=isset($error) ? "Error! {$error}" : ''?>
- <?=isset($success) ? "Success! {$success}" : ''?>
- </div>
- <input type="text" name="username" placeholder="username">
- <input type="email" name="email" placeholder="email">
- <input type="password" name="password" placeholder="password">
- <input type="password" name="password2" placeholder="retype password">
- <input type="submit" name="btnSubmit" value="Register">
- </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement