Hi! Could we please enable some services and cookies to improve your experience and our website?

SQLize | PHPize | SQLtest

Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code

A A A
Login    Share code      Blog   FAQ
Copy Format Clear
<?php include("includes/db.php"); session_start(); if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['register'])) { $username = trim($_POST['username']); $email = trim($_POST['email']); $password = $_POST['password']; $confirm_password = $_POST['confirm_password']; $role = $_POST['role']; // In production, you might want to set a default role // Validate inputs $errors = []; if (empty($username)) $errors[] = "Username is required"; if (empty($email)) $errors[] = "Email is required"; if (!filter_var($email, FILTER_VALIDATE_EMAIL)) $errors[] = "Invalid email format"; if (empty($password)) $errors[] = "Password is required"; if ($password !== $confirm_password) $errors[] = "Passwords do not match"; // Check if username/email already exists $stmt = $conn->prepare("SELECT id FROM users WHERE username = ? OR email = ?"); $stmt->bind_param("ss", $username, $email); $stmt->execute(); if ($stmt->get_result()->num_rows > 0) { $errors[] = "Username or email already exists"; } if (empty($errors)) { $hashed_password = password_hash($password, PASSWORD_DEFAULT); $stmt = $conn->prepare("INSERT INTO users (username, email, password, role) VALUES (?, ?, ?, ?)"); $stmt->bind_param("ssss", $username, $email, $hashed_password, $role); if ($stmt->execute()) { $_SESSION['message'] = "Registration successful! Please login."; header("Location: login.php"); exit(); } else { $errors[] = "Registration failed: " . $conn->error; } } } ?> <!DOCTYPE html> <html> <head> <title>Register</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet"> </head> <body class="bg-light"> <div class="container mt-5"> <div class="row justify-content-center"> <div class="col-md-6 bg-white p-4 rounded shadow"> <h3 class="mb-4 text-center">User Registration</h3> <?php if (!empty($errors)): ?> <div class="alert alert-danger"> <?php foreach ($errors as $error): ?> <p><?php echo htmlspecialchars($error); ?></p> <?php endforeach; ?> </div> <?php endif; ?> <form method="POST" action=""> <div class="mb-3"> <label>Username</label> <input type="text" name="username" required class="form-control" value="<?php echo htmlspecialchars($_POST['username'] ?? ''); ?>"> </div> <div class="mb-3"> <label>Email</label> <input type="email" name="email" required class="form-control" value="<?php echo htmlspecialchars($_POST['email'] ?? ''); ?>"> </div> <div class="mb-3"> <label>Password</label> <input type="password" name="password" required class="form-control"> </div> <div class="mb-3"> <label>Confirm Password</label> <input type="password" name="confirm_password" required class="form-control"> </div> <div class="mb-3"> <label>Role</label> <select name="role" class="form-control" required> <option value="attendee" <?php echo ($_POST['role'] ?? '') === 'attendee' ? 'selected' : ''; ?>>Attendee</option> <option value="organizer" <?php echo ($_POST['role'] ?? '') === 'organizer' ? 'selected' : ''; ?>>Organizer</option> <!-- Remove admin option or restrict it --> </select> </div> <button type="submit" name="register" class="btn btn-primary w-100">Register</button> <p class="mt-3 text-center">Already have an account? <a href="login.php">Login</a></p> </form> </div> </div> </div> </body> </html>

Stuck with a problem? Got Error? Ask AI support!

Copy Clear