View difference between Paste ID: An44x52R and dEzhBJXV
SHOW: | | - or go back to the newest paste.
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
	} else {
19
		$error[] = 'System Error 1";
20
	}
21
22
	$statement = $dbh->prepare("SELECT username FROM signup WHERE email = :email");
23
	$statement->bindParam(':email', $email);
24
25
	if ($statement->execute()) {
26
		if ($statement->rowCount() > 0) {
27
			$error[] = 'Email Address Taken";
28
		}
29
	} else {
30
		$error[] = 'System Error 2";
31
	}
32
33
	// you cannot use if/else since you can have username exists OR email exists OR both
34
35
	if (!$error) {
36
		$insert="INSERT INTO signup(username, email, password) VALUES(:name, :email, :pwd)";
37
		$statement = $dbh->prepare($insert);
38
		$statement->bindParam(':name', $name);
39
		$statement->bindParam(':email', $email);
40
		$statement->bindParam(':pwd', $hashed_password);
41
		if ($statement->execute() && $statement->rowCount() > 0) {
42
			$result = "row inserted";
43
		} else {
44
			$result = "insertion failed";
45
		}
46
	}
47
}
48
?>
49
<html>
50
<div class="form-group">
51
		    <label for="exampleInputUsername">Username</label>
52
		    <input type="text" name="name" class="form-control" id="exampleInputUsername" placeholder="enter username">
53
		    <span class="error_message"><?php echo $nameErr;?></span>
54
		  </div>
55
56
		  <div class="form-group">
57
		      <label for="exampleInputEmail1">Email address</label>
58
		      <input type="email" name="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email">
59
		      <span class="error_message"><?php echo $emailErr;?></span>
60
		      <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
61
		    </div>
62
63
	<button type="submit" name="signupBtn" class="btn btn-primary">SIGNUP</button>
64
	<?php echo $implode('<br>', $error); ?>
65
	<?php echo $result; ?>
66
</html>