You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
FutureCraft/register.php

98 lines
3.7 KiB

6 months ago
<?php
session_start();
require 'db_connection.php'; // Include database connection file
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$email = trim($_POST['email']);
$password = trim($_POST['password']);
$role = trim($_POST['role']);
$first_name = trim($_POST['first_name']);
$last_name = trim($_POST['last_name']);
$phone = trim($_POST['phone']);
// Validation
if (empty($email) || empty($password) || empty($role) || empty($first_name) || empty($last_name)) {
$error = "All fields except phone are required.";
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error = "Invalid email format.";
} elseif (!in_array($role, ['donor', 'recipient', 'admin'])) {
$error = "Invalid role selected.";
} else {
// Check if email already exists
$query = "SELECT id FROM users WHERE email = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param("s", $email);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
$error = "Email is already registered.";
} else {
// Hash the password
$hashed_password = password_hash($password, PASSWORD_BCRYPT);
// Insert into the database
$query = "INSERT INTO users (email, password, role, first_name, last_name, phone) VALUES (?, ?, ?, ?, ?, ?)";
$stmt = $conn->prepare($query);
$stmt->bind_param("ssssss", $email, $hashed_password, $role, $first_name, $last_name, $phone);
if ($stmt->execute()) {
$_SESSION['success'] = "Registration successful. Please log in.";
header("Location: login.php");
exit;
} else {
$error = "Registration failed. Please try again.";
}
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Register</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css">
</head>
<body>
<div class="container mt-5">
<h2>Register</h2>
<?php if (isset($error)): ?>
<div class="alert alert-danger"><?= $error ?></div>
<?php endif; ?>
<form method="POST" action="">
<div class="mb-3">
<label for="email" class="form-label">Email</label>
<input type="email" name="email" class="form-control" id="email" required>
</div>
<div class="mb-3">
<label for="password" class="form-label">Password</label>
<input type="password" name="password" class="form-control" id="password" required>
</div>
<div class="mb-3">
<label for="role" class="form-label">Role</label>
<select name="role" class="form-control" id="role" required>
<option value="donor">Donor</option>
<option value="recipient">Recipient</option>
<option value="admin">Admin</option>
</select>
</div>
<div class="mb-3">
<label for="first_name" class="form-label">First Name</label>
<input type="text" name="first_name" class="form-control" id="first_name" required>
</div>
<div class="mb-3">
<label for="last_name" class="form-label">Last Name</label>
<input type="text" name="last_name" class="form-control" id="last_name" required>
</div>
<div class="mb-3">
<label for="phone" class="form-label">Phone</label>
<input type="text" name="phone" class="form-control" id="phone">
</div>
<button type="submit" class="btn btn-primary">Register</button>
</form>
</div>
</body>
</html>