SHARE
TWEET

Untitled

a guest Apr 2nd, 2019 221 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. // Error and success messages
  4. $messages = [
  5.     'error' => [],
  6.     'success' => [],
  7. ];
  8.  
  9. // Genders
  10. $genders = [
  11.     'male',
  12.     'female',
  13.     'other',
  14. ];
  15.  
  16. // Form sent
  17. if(!empty($_POST))
  18. {
  19.     // Default gender
  20.     if(!isset($_POST['gender']))
  21.     {
  22.         $_POST['gender'] = '';
  23.     }
  24.  
  25.     // Debug
  26.     echo '<pre>';
  27.     print_r($_POST);
  28.     echo '</pre>';
  29.  
  30.     // Get variables
  31.     $login = trim($_POST['login']);
  32.     $password = $_POST['password'];
  33.     $age = (int)$_POST['age'];
  34.     $gender = $_POST['gender'];
  35.  
  36.     // Handle errors
  37.     if(empty($login))
  38.     {
  39.         $messages['error'][] = 'Missing login';
  40.     }
  41.  
  42.     if(empty($password))
  43.     {
  44.         $messages['error'][] = 'Missing password';
  45.     }
  46.     else if(strlen($password) < 5)
  47.     {
  48.         $messages['error'][] = 'Password too short';
  49.     }
  50.  
  51.     if(empty($age))
  52.     {
  53.         $messages['error'][] = 'Missing age';
  54.     }
  55.     else if($age < 1 || $age > 127)
  56.     {
  57.         $messages['error'][] = 'Wrong age';
  58.     }
  59.  
  60.     if(empty($gender))
  61.     {
  62.         $messages['error'][] = 'Missing gender';
  63.     }
  64.     else if(!in_array($gender, $genders))
  65.     {
  66.         $messages['error'][] = 'Wrong gender';
  67.     }
  68.  
  69.     // Success
  70.     if(empty($messages['error']))
  71.     {
  72.         $messages['success'][] = 'All good';
  73.  
  74.         $post_user = $bdd->prepare('INSERT INTO users (login, password, age, gender) VALUES (?, ?, ?, ?)');
  75.         $post_user->execute([$login, $password, $age, $gender]);
  76.  
  77.         $_POST['login'] = '';
  78.         $_POST['password'] = '';
  79.         $_POST['age'] = '';
  80.         $_POST['gender'] = '';
  81.     }
  82. }
  83.  
  84. // Form not sent
  85. else
  86. {
  87.     $_POST['login'] = '';
  88.     $_POST['password'] = '';
  89.     $_POST['age'] = '';
  90.     $_POST['gender'] = '';
  91. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top