Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $error = [];
- $result = null;
- if (isset($_POST['signupBtn'])){
- $name = trim($_POST['name']);
- $email = trim($_POST['email']);
- $hashed_password = password_hash($pwd, PASSWORD_DEFAULT);
- // check if user already exist
- $statement = $dbh->prepare("SELECT username FROM signup WHERE username =: name");
- $statement->bindParam(':name', $name);
- if ($statement->execute()) {
- if ($statement->rowCount() > 0) {
- $error[] = 'User Name Taken";
- }
- }
- $statement = $dbh->prepare("SELECT username FROM signup WHERE email = :email");
- $statement->bindParam(':email', $email);
- if ($statement->execute()) {
- if ($statement->rowCount() > 0) {
- $error[] = 'Email Address Taken";
- }
- }
- // you cannot use if/else since you can have username exists OR email exists OR both
- if (!$error) {
- $insert="INSERT INTO signup(username, email, password) VALUES(:name, :email, :pwd)";
- $statement = $dbh->prepare($insert);
- $statement->bindParam(':name', $name);
- $statement->bindParam(':email', $email);
- $statement->bindParam(':pwd', $hashed_password);
- if ($statement->execute() && $statement->rowCount() > 0) {
- $result = "row inserted";
- } else {
- $result = "insertion failed";
- }
- }
- }
- ?>
- <html>
- <div class="form-group">
- <label for="exampleInputUsername">Username</label>
- <input type="text" name="name" class="form-control" id="exampleInputUsername" placeholder="enter username">
- <span class="error_message"><?php echo $nameErr;?></span>
- </div>
- <div class="form-group">
- <label for="exampleInputEmail1">Email address</label>
- <input type="email" name="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email">
- <span class="error_message"><?php echo $emailErr;?></span>
- <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
- </div>
- <button type="submit" name="signupBtn" class="btn btn-primary">SIGNUP</button>
- <?php echo $implode('<br>', $error); ?>
- <?php echo $result; ?>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement