-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPractical_13_Reg.php
More file actions
57 lines (47 loc) · 1.62 KB
/
Practical_13_Reg.php
File metadata and controls
57 lines (47 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
//To connect to DB
$conn = new mysqli("localhost", "root", "Heleninh@2022", "HAC_Services");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$password = $_POST['password'];
$role = $_POST['role'];
//To validate input
if (strlen($name) < 3) {
die("Name must be at least 3 characters.");
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
die("Invalid email format.");
}
if (!in_array($role, ['admin', 'broker', 'buyer'])) {
die("Invalid role selected.");
}
if (strlen($password) < 6) {
die("Password must be at least 6 characters.");
}
// To check if email already exists
$check = $conn->prepare("SELECT id FROM users WHERE email = ?");
$check->bind_param("s", $email);
$check->execute();
$check->store_result();
if ($check->num_rows > 0) {
die("Email already registered. Try logging in.");
}
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
$sql = "INSERT INTO users (name, email, password, role) VALUES (?, ?, ?, ?)";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ssss", $name, $email, $hashed_password, $role);
if ($stmt->execute()) {
header("Location: Practical_13_Login.html?success=1");
exit();
} else {
echo "Registration error: " . $stmt->error;
}
$check->close();
$stmt->close();
$conn->close();
}
?>