34315-vm/register.php
Flatlogic Bot d627358851 v1.3
2025-09-23 16:15:21 +00:00

233 lines
13 KiB
PHP

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Register - MyMech</title>
<meta name="description" content="Join MyMech to find reliable mechanics and genuine spare parts.">
<!-- Bootstrap 5 CDN -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<!-- Google Fonts (Poppins) -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600;700&display=swap" rel="stylesheet">
<!-- Custom CSS -->
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
</head>
<body>
<!-- Navbar -->
<nav class="navbar navbar-expand-lg navbar-dark bg-navy-blue-darker">
<div class="container">
<a class="navbar-brand" href="index.php"><span class="golden-text">MyMech</span></a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav ms-auto align-items-center">
<li class="nav-item mb-2 mb-lg-0 me-lg-2">
<a class="btn btn-outline-golden px-3" href="login.php">Login</a>
</li>
<li class="nav-item">
<a class="btn btn-golden px-3" href="register.php">Register</a>
</li>
</ul>
</div>
</div>
</nav>
<!-- Registration Form Section -->
<section class="form-section">
<div class="container">
<div class="row justify-content-center">
<div class="col-lg-7 col-md-9">
<div class="form-container">
<h2 class="text-center golden-text mb-4">Create Your Account</h2>
<p class="text-center mb-5">Join our community of drivers, mechanics, and part suppliers.</p>
<?php
session_start(); // Start the session to store messages
// Display messages if any
if (isset($_SESSION['message'])) {
$message = $_SESSION['message'];
$alert_type = $_SESSION['alert_type'];
echo "<div class='alert alert-{$alert_type} alert-dismissible fade show' role='alert'>";
echo $message;
echo '<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>';
echo "</div>";
// Unset the session variables so they don't persist
unset($_SESSION['message']);
unset($_SESSION['alert_type']);
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
require_once 'db/config.php';
$pdo = db_connect();
$message = '';
$error = false;
if (!$pdo) {
$message = "Database connection failed.";
$error = true;
} else {
// Create table if it doesn't exist
try {
$pdo->exec("CREATE TABLE IF NOT EXISTS users (
id INT AUTO_INCREMENT PRIMARY KEY,
user_role VARCHAR(50) NOT NULL,
full_name VARCHAR(255) NOT NULL,
id_number VARCHAR(50) NOT NULL UNIQUE,
phone_number VARCHAR(50) NOT NULL UNIQUE,
garage_name VARCHAR(255),
location VARCHAR(255) NOT NULL,
password_hash VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;");
} catch (PDOException $e) {
$message = "Error creating table: " . $e->getMessage();
$error = true;
}
if (!$error) {
// --- Form Data ---
$user_role = $_POST['user_role'] ?? '';
$full_name = $_POST['full_name'] ?? '';
$id_number = $_POST['id_number'] ?? '';
$phone_number = $_POST['phone_number'] ?? '';
$garage_name = ($user_role === 'mechanic' || $user_role === 'shop_owner') ? ($_POST['garage_name'] ?? '') : null;
$location = $_POST['location'] ?? '';
$password = $_POST['password'] ?? '';
$confirm_password = $_POST['confirm_password'] ?? '';
// --- Validation ---
if (empty($user_role) || empty($full_name) || empty($id_number) || empty($phone_number) || empty($location) || empty($password)) {
$message = "Please fill in all required fields.";
$error = true;
} elseif ($password !== $confirm_password) {
$message = "Passwords do not match.";
$error = true;
} else {
// Check for existing user (ID number or phone)
$stmt = $pdo->prepare("SELECT id FROM users WHERE id_number = ? OR phone_number = ?");
$stmt->execute([$id_number, $phone_number]);
if ($stmt->fetch()) {
$message = "A user with this ID number or phone number already exists.";
$error = true;
}
}
// --- Insertion ---
if (!$error) {
$password_hash = password_hash($password, PASSWORD_DEFAULT);
$sql = "INSERT INTO users (user_role, full_name, id_number, phone_number, garage_name, location, password_hash) VALUES (?, ?, ?, ?, ?, ?, ?)";
$stmt = $pdo->prepare($sql);
try {
$stmt->execute([$user_role, $full_name, $id_number, $phone_number, $garage_name, $location, $password_hash]);
$_SESSION['message'] = "Registration successful! You can now log in.";
$_SESSION['alert_type'] = 'success';
header("Location: login.php"); // Redirect to login page on success
exit();
} catch (PDOException $e) {
// Check for duplicate entry specifically
if ($e->errorInfo[1] == 1062) {
$_SESSION['message'] = "A user with this ID number or phone number already exists.";
} else {
$_SESSION['message'] = "An error occurred during registration. Please try again.";
}
$_SESSION['alert_type'] = 'danger';
header("Location: register.php");
exit();
}
}
}
}
if ($error) {
$_SESSION['message'] = $message;
$_SESSION['alert_type'] = 'danger';
header("Location: register.php");
exit();
}
}
?>
<form action="register.php" method="POST" id="registrationForm">
<div class="mb-3">
<label for="user_role" class="form-label">I am a...</label>
<select class="form-select" id="user_role" name="user_role" required>
<option value="driver" selected>Car Owner / Driver</option>
<option value="mechanic">Mechanic</option>
<option value="shop_owner">Spare Part Shop Owner</option>
</select>
</div>
<div class="mb-3">
<label for="full_name" class="form-label">Full Names (as on National ID)</label>
<input type="text" class="form-control" id="full_name" name="full_name" required>
</div>
<div class="mb-3">
<label for="id_number" class="form-label">National ID Number</label>
<input type="text" class="form-control" id="id_number" name="id_number" required>
</div>
<div class="mb-3">
<label for="phone_number" class="form-label">Phone Number</label>
<input type="tel" class="form-control" id="phone_number" name="phone_number" required>
</div>
<div class="mb-3" id="garage_name_field" style="display: none;">
<label for="garage_name" class="form-label">Garage/Shop Name</label>
<input type="text" class="form-control" id="garage_name" name="garage_name">
</div>
<div class="mb-3">
<label for="location" class="form-label">Location / Address</label>
<input type="text" class="form-control" id="location" name="location" required>
</div>
<div class="row">
<div class="col-md-6 mb-3">
<label for="password" class="form-label">Password</label>
<input type="password" class="form-control" id="password" name="password" required>
</div>
<div class="col-md-6 mb-3">
<label for="confirm_password" class="form-label">Confirm Password</label>
<input type="password" class="form-control" id="confirm_password" name="confirm_password" required>
</div>
</div>
<div class="d-grid">
<button type="submit" class="btn btn-golden btn-lg">Register</button>
</div>
</form>
</div>
</div>
</div>
</div>
</section>
<!-- Footer -->
<footer class="footer text-center">
<div class="container">
<p>&copy; <?php echo date("Y"); ?> MyMech. All Rights Reserved.</p>
<p>
<a href="privacy.php">Privacy Policy</a> | <a href="terms.php">Terms of Service</a>
</p>
</div>
</footer>
<!-- Bootstrap 5 JS Bundle -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
<!-- Custom JS -->
<script src="assets/js/main.js?v=<?php echo time(); ?>"></script>
</body>
</html>