Advertisement
Guest User

Untitled

a guest
Feb 14th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. id | f_name | l_name | position | paygrade | users_id |..|
  2.  
  3. id | username | password | user_level | user_roles | email | employee_id
  4.  
  5. <?php
  6. //If the POST var "register" exists (our submit button), then we can
  7. //assume that the user has submitted the registration form.
  8. if(isset($_POST['register'])){
  9.  
  10. //Retrieve the field values from our registration form.
  11.  
  12. //to be inserted in employee table
  13. $first_name = !empty($_POST['first_name']) ? trim($_POST['first_name']) : null;
  14. $last_name = !empty($_POST['last_name']) ? trim($_POST['last_name']) : null;
  15. $email = !empty($_POST['email']) ? trim($_POST['email']) : null;
  16. $phone = !empty($_POST['phone']) ? trim($_POST['phone']) : null;
  17. $company_name = !empty($_POST['company_name']) ? trim($_POST['company_name']) : null;
  18. $subdomain_name = !empty($_POST['subdomain_name']) ? trim($_POST['subdomain_name']) : null;
  19.  
  20. //to be inserted in users table
  21. $username = !empty($_POST['username']) ? trim($_POST['username']) : null;
  22. $password = !empty($_POST['password']) ? trim($_POST['password']) : null;
  23.  
  24. //TO ADD: Error checking (username characters, password length, etc).
  25. //Basically, you will need to add your own error checking BEFORE
  26. //the prepared statement is built and executed.
  27.  
  28. //Now, we need to check if the supplied username already exists.
  29.  
  30. //Construct the SQL statement and prepare it.
  31. $sqlUsername = "SELECT COUNT(username) AS num FROM users WHERE username = :username";
  32. $stmt = $pdo->prepare( $sqlUsername);
  33.  
  34. //Bind the provided username to our prepared statement.
  35. $stmt->bindValue(':username', $username);
  36.  
  37. //Execute.
  38. $stmt->execute();
  39.  
  40. //Fetch the row.
  41. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  42.  
  43. //If the provided username already exists - display error.
  44. //TO ADD - Your own method of handling this error. For example purposes,
  45. //I'm just going to kill the script completely, as error handling is outside
  46. //the scope of this tutorial.
  47. if($row['num'] > 0){
  48. die('That username already exists!');
  49. }
  50.  
  51. //Hash the password as we do NOT want to store our passwords in plain text.
  52. $passwordHash = password_hash($password, PASSWORD_BCRYPT, array("cost" => 12));
  53.  
  54. //Prepare our INSERT statement.
  55. //Remember: We are inserting a new row into our users table.
  56. $sqlUsername = "INSERT INTO users (username, password) VALUES (:username, :password)";
  57. $stmt = $pdo->prepare( $sqlUsername);
  58.  
  59. //Bind our variables.
  60. $stmt->bindValue(':username', $username);
  61. $stmt->bindValue(':password', $passwordHash);
  62.  
  63. //Execute the statement and insert the new account.
  64. $result = $stmt->execute();
  65.  
  66. //If the signup process is successful.
  67. if($result){
  68. //do something here
  69. echo 'Thank you for registering with our website.';
  70. }
  71.  
  72. }
  73. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement