-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignup.php
64 lines (53 loc) · 2.22 KB
/
signup.php
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
58
59
60
61
62
63
64
<?php
session_start();
$conn = new mysqli("localhost", "root", "", "health_advice_group");
if ($conn->connect_error) {
die("Database connection failed: " . $conn->connect_error);
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get form data
$first_name = trim($_POST['first_name']);
$last_name = trim($_POST['last_name']);
$email = trim($_POST['email']);
$password = $_POST['password'];
$confirm_password = $_POST['confirm_password'];
// Validate fields
if (empty($first_name) || empty($last_name) || empty($email) || empty($password) || empty($confirm_password)) {
echo "<script>alert('All fields are required.'); window.location.href='signup.html';</script>";
exit();
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "<script>alert('Invalid email format.'); window.location.href='signup.html';</script>";
exit();
}
if ($password !== $confirm_password) {
echo "<script>alert('Passwords do not match.'); window.location.href='signup.html';</script>";
exit();
}
// Check if the email already exists
$stmt = $conn->prepare("SELECT id FROM users WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows > 0) {
echo "<script>alert('Email already registered. Try logging in.'); window.location.href='signup.html';</script>";
exit();
}
$stmt->close();
// Hash the password
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
// Insert into database
$stmt = $conn->prepare("INSERT INTO users (first_name, last_name, email, password) VALUES (?, ?, ?, ?)");
$stmt->bind_param("ssss", $first_name, $last_name, $email, $hashed_password);
if ($stmt->execute()) {
$_SESSION["user_id"] = $stmt->insert_id;
$_SESSION["email"] = $email;
echo "<script>alert('Signup successful! Redirecting to dashboard.'); window.location.href='dashboard.php';</script>";
exit();
} else {
echo "<script>alert('Error: Could not complete signup.'); window.location.href='signup.html';</script>";
}
$stmt->close();
}
$conn->close();
?>