prepare('SELECT id FROM users WHERE username = ?'); $stmt->execute([$username]); if ($stmt->fetch()) { $error = 'This username is already taken.'; } else { // Hash password and insert user $password_hash = password_hash($password, PASSWORD_DEFAULT); $stmt = $pdo->prepare('INSERT INTO users (username, password_hash) VALUES (?, ?)'); $stmt->execute([$username, $password_hash]); // Log the user in and redirect $_SESSION['user_id'] = $pdo->lastInsertId(); $_SESSION['username'] = $username; header('Location: index.php'); exit; } } catch (PDOException $e) { $error = 'Database error: ' . $e->getMessage(); } } } ?>
Already have an account? Log In