60 lines
1.8 KiB
PHP
60 lines
1.8 KiB
PHP
<?php
|
|
require_once 'db/config.php';
|
|
|
|
// Basic validation
|
|
if (empty($_POST['owner_name']) || empty($_POST['email']) || empty($_POST['password']) || empty($_POST['restaurant_name'])) {
|
|
die('Please fill all required fields.');
|
|
}
|
|
|
|
if (strlen($_POST['password']) < 8) {
|
|
die('Password must be at least 8 characters long.');
|
|
}
|
|
|
|
$owner_name = $_POST['owner_name'];
|
|
$email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
|
|
$password = password_hash($_POST['password'], PASSWORD_DEFAULT);
|
|
$restaurant_name = $_POST['restaurant_name'];
|
|
$restaurant_description = $_POST['restaurant_description'] ?? '';
|
|
|
|
if (!$email) {
|
|
die('Invalid email format.');
|
|
}
|
|
|
|
$pdo = db();
|
|
|
|
// Check if email already exists
|
|
$stmt = $pdo->prepare("SELECT id FROM users WHERE email = ?");
|
|
$stmt->execute([$email]);
|
|
if ($stmt->fetch()) {
|
|
die('An account with this email already exists. <a href="login.php">Log in here</a>.');
|
|
}
|
|
|
|
try {
|
|
$pdo->beginTransaction();
|
|
|
|
// 1. Create the user
|
|
$stmt_user = $pdo->prepare(
|
|
"INSERT INTO users (name, email, password, role) VALUES (?, ?, ?, 'restaurant_owner')"
|
|
);
|
|
$stmt_user->execute([$owner_name, $email, $password]);
|
|
$user_id = $pdo->lastInsertId();
|
|
|
|
// 2. Create the restaurant
|
|
$stmt_restaurant = $pdo->prepare(
|
|
"INSERT INTO restaurants (name, description, user_id, image_url) VALUES (?, ?, ?, ?)"
|
|
);
|
|
// Using a placeholder image for now
|
|
$stmt_restaurant->execute([$restaurant_name, $restaurant_description, $user_id, 'assets/images/placeholder.jpg']);
|
|
|
|
$pdo->commit();
|
|
|
|
// Redirect to a success page or login page
|
|
header("Location: login.php?signup=success");
|
|
exit();
|
|
|
|
} catch (PDOException $e) {
|
|
$pdo->rollBack();
|
|
// In a real app, you would log this error
|
|
die("Error creating account: " . $e->getMessage());
|
|
}
|