tamaro_skaljic

register.php

Jun 27th, 2021 (edited)
476
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.27 KB | None | 0 0
  1. <?php
  2. // Check whether a user with the given username exist or not
  3. $query = "SELECT 1 FROM public.user WHERE username = :username";
  4. $query_params = array(':username' => $username);
  5. try{
  6.     $stmt = $pdo->prepare($query);
  7.     $result = $stmt->execute($query_params);
  8. }
  9. catch(PDOException $ex){
  10.     echo '<span style="color: red">ERROR! Code: 009</span>';
  11.     exit;
  12. }
  13.  
  14. $row = $stmt->fetch();
  15.  
  16. // When a user exist with the username, registration fails
  17. if($row){
  18.     $error = 1;
  19. }
  20.  
  21. // If no error has occurred so far, create the user
  22. if($error != 1) {
  23.     // If the register as administrator checkbox was activated in the registration form, create an administrator account, when not create a normal account
  24.     if($_POST['isAdmin'] == 'on'){
  25.         $query = "INSERT INTO public.user (username, unencrypted_password, is_admin) VALUES (:username, :unencrypted_password, TRUE)";
  26.     }else{
  27.         $query = "INSERT INTO public.user (username, unencrypted_password, is_admin) VALUES (:username, :unencrypted_password, FALSE)";
  28.     }
  29.     $query_params = array(':username' => $username, ':unencrypted_password' => $password);
  30.     try{
  31.         $stmt = $pdo->prepare($query);
  32.         $result = $stmt->execute($query_params);
  33.     }
  34.     catch(PDOException $ex){
  35.         echo '<span style="color: red">ERROR! Code: 010</span>';
  36.         exit;
  37.     }
  38. }
  39. ?>
Add Comment
Please, Sign In to add comment