Guest User

Untitled

a guest
Jul 19th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. <form method "POST" id="regform" action="register.php">
  2. <input type="text" username="username" placeholder="Please enter your
  3. email"/>
  4. <input type="text" password="password" placeholder="Please create a
  5. password"/>
  6. <input type="submit" value="enter"/>
  7. </form>
  8.  
  9. <?php
  10.  
  11. $servername = "localhost";
  12. $dbusername = "root";
  13. $dbpassword = "root";
  14. $dbname = "fyp";
  15.  
  16.  
  17. try{
  18. $pdostmt = new PDO("mysql:host=$servername;dbname=$dbname",$dbusername,
  19. $dbpassword);
  20. $pdostmt->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  21. echo "connected succesfully";
  22. }
  23. catch(PDOException $e)
  24. {
  25. print "Error! Unable to connect: " . $e->getMessage() . "<br/>";
  26. die();
  27. }
  28. ?>
  29.  
  30. <?php
  31. // Starting the session and connecting to the DB
  32. session_start();
  33. require_once'connect.php';
  34.  
  35. if(isset($_POST['register'])){
  36.  
  37.  
  38. $username = !empty($_POST['username']) ? trim($_POST['username']) : null;
  39. $pass = !empty($_POST['password']) ? trim($_POST['password']) : null;
  40.  
  41. $sql = "SELECT COUNT(username) AS num FROM users WHERE username = :username";
  42. $stmt = $pdo->prepare($sql);
  43. $stmt->bindValue(':username', $username);
  44.  
  45.  
  46. $stmt->execute();
  47.  
  48.  
  49. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  50.  
  51.  
  52. if($row['num'] > 0){
  53. die('That username already exists!');
  54. }
  55.  
  56. $sql = "INSERT INTO users (username, password) VALUES (:username,
  57. :password)";
  58. $stmt = $pdo->prepare($sql);
  59.  
  60. $stmt->bindValue(':username', $username);
  61. $stmt->bindValue(':password', $passwordHash);
  62.  
  63. $result = $stmt->execute();
  64.  
  65. if($result){
  66. echo 'Thank you for registering with our website.';
  67. header("Location: index.php");
  68. }
  69. else {
  70. echo '<b> There has been an error please contact support </b>';
  71. }
  72.  
  73. }
  74.  
  75. ?>
Add Comment
Please, Sign In to add comment