Advertisement
AzisMM

register.php

Oct 30th, 2023
1,167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.24 KB | None | 0 0
  1. <?php
  2.  
  3. // Database connection
  4. $conn = mysqli_connect("localhost", "root", "", "example_db");
  5.  
  6. // Register logic
  7. if(isset($_POST['register'])) {
  8.     $username = $_POST['username'];
  9.     $password = $_POST['password'];
  10.     $role = 'student';
  11.  
  12.     // Check if username is already taken
  13.     $query = "SELECT * FROM users WHERE username='$username'";
  14.     $result = mysqli_query($conn, $query);
  15.     $isUsernameTaken = mysqli_num_rows($result) > 0;
  16.  
  17.     if(!$isUsernameTaken) {
  18.         // Insert user into database
  19.         $query = "INSERT INTO users (username, password, role) VALUES ('$username', '$password', '$role')";
  20.         mysqli_query($conn, $query);
  21.         echo "Registration successful. You can now login as a student.";
  22.     } else {
  23.         echo "Username is already taken";
  24.     }
  25. }
  26.  
  27. ?>
  28.  
  29. <!DOCTYPE html>
  30. <html>
  31. <head>
  32.     <title>Login and Register Form</title>
  33. </head>
  34. <body>
  35.  
  36.     <h1>Register</h1>
  37.     <form method="POST">
  38.         <input type="text" name="username" placeholder="Username" required><br>
  39.         <input type="password" name="password" placeholder="Password" required><br>
  40.         <button type="submit" name="register">Register</button>
  41.     </form>
  42.     <h1><a href="index.php">Beranda</a></h1>
  43. </body>
  44. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement