36 lines
1.0 KiB
PHP
36 lines
1.0 KiB
PHP
<?php
|
|
require_once 'db.php';
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$username = trim($_POST['username']);
|
|
$email = trim($_POST['email']);
|
|
$password = $_POST['password'];
|
|
|
|
if (!$username || !$email || !$password) {
|
|
die('All fields are required.');
|
|
}
|
|
|
|
$conn = getConnection();
|
|
|
|
// Check if user exists
|
|
$stmt = $conn->prepare("SELECT id FROM Users WHERE email = ?");
|
|
$stmt->execute([$email]);
|
|
if ($stmt->fetch()) {
|
|
die('Email already registered.');
|
|
}
|
|
|
|
$hashed = password_hash($password, PASSWORD_DEFAULT);
|
|
$stmt = $conn->prepare("INSERT INTO Users (username, email, password) VALUES (?, ?, ?)");
|
|
$stmt->execute([$username, $email, $hashed]);
|
|
|
|
header('Location: login.php');
|
|
exit;
|
|
}
|
|
?>
|
|
|
|
<form method="POST">
|
|
<input type="text" name="username" placeholder="Username" required>
|
|
<input type="email" name="email" placeholder="Email" required>
|
|
<input type="password" name="password" placeholder="Password" required>
|
|
<button type="submit">Register</button>
|
|
</form>
|