Create Your Account

Join our community of drivers, mechanics, and part suppliers.

"; echo $message; echo ''; echo "
"; // 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(); } } ?>