Advertisement
Guest User

Untitled

a guest
Apr 9th, 2018
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. <?php
  2. // function to insert data in the database
  3. function registerusers($db) {
  4. if(!isset($_POST['submit'])) {
  5. return; }
  6. $errMsg = '';
  7.  
  8. // get the data from the front end
  9. $username = $_POST['username'];
  10. $email = $_POST['email'];
  11. $password = $_POST['password'];
  12. $passwordconf = $_POST['password_confirm'];
  13.  
  14. // check the values
  15. if( !$username || !$email || !$password || !$passwordconf) {
  16. echo 'One or more fields are empty.';
  17. return;
  18. }
  19. else {
  20. // escape special characters in a string for use in the SQL statement
  21. }
  22. // ENCRYPT THE PASSWORD
  23. $encrypt_pass = password_hash($password, PASSWORD_DEFAULT);
  24.  
  25. if(($_POST["password"])!=($_POST["password_confirm"])){
  26.  
  27. echo '<div id="alert1" class="alert alert-danger alert-dismissible" role="alert">
  28. <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>Oops! Password did not match! Try again..</div>';
  29. return;
  30. }
  31. // create a query
  32. $sqlQuery = "INSERT INTO register (username, email, password) VALUES (:username, :email, :password)";
  33.  
  34. //prepare the query
  35. $query = $db->prepare($sqlQuery);
  36.  
  37. //execute the query
  38. $query->execute(array(
  39. ':username' => $username,
  40. ':email' => $email,
  41. ':password' => $encrypt_pass
  42. ));
  43.  
  44. // check if the student was successfully inserted in the database
  45. if ($query) {
  46. echo '<div id="alert1" class="alert alert-success alert-dismissible" role="alert">
  47. <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>You have successfully registered. You can now login <a href="login.php">login</a></div>';
  48. }
  49. else {
  50. // print the error generated
  51. echo '<div id="alert1" class="alert alert-danger alert-dismissible" role="alert">
  52. <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>Oops! Something went wrong here. Your registration was not successful, please try again.</div>';
  53. }
  54.  
  55. }
  56. /* Main body */
  57. //connect to the DB and call the function
  58. require 'component/connect.php';
  59. registerusers($db);
  60. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement