Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Database connection
- $conn = mysqli_connect("localhost", "root", "", "example_db");
- // Register logic
- if(isset($_POST['register'])) {
- $username = $_POST['username'];
- $password = $_POST['password'];
- $role = 'student';
- // Check if username is already taken
- $query = "SELECT * FROM users WHERE username='$username'";
- $result = mysqli_query($conn, $query);
- $isUsernameTaken = mysqli_num_rows($result) > 0;
- if(!$isUsernameTaken) {
- // Insert user into database
- $query = "INSERT INTO users (username, password, role) VALUES ('$username', '$password', '$role')";
- mysqli_query($conn, $query);
- echo "Registration successful. You can now login as a student.";
- } else {
- echo "Username is already taken";
- }
- }
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <title>Login and Register Form</title>
- </head>
- <body>
- <h1>Register</h1>
- <form method="POST">
- <input type="text" name="username" placeholder="Username" required><br>
- <input type="password" name="password" placeholder="Password" required><br>
- <button type="submit" name="register">Register</button>
- </form>
- <h1><a href="index.php">Beranda</a></h1>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement