Guest User

Untitled

a guest
Dec 13th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.22 KB | None | 0 0
  1. <?php
  2. $con = mysql_connect("localhost","root","");
  3. if (!$con)
  4.   {
  5.   die('Could not connect: ' . mysql_error());
  6.   }
  7.  
  8. mysql_select_db("users", $con) or die("Can't select db.");
  9.  
  10. $username = $_POST['username']; // or $_POST["username"]; Doesn't matter if " or ' here
  11. $password = $_POST['password'];
  12. $error = false;
  13.  
  14. if($_POST){
  15. if(empty($username){ //$username is exactly the same as $_POST['username']
  16.    echo "Empty username!";
  17.    $error = true;
  18. }
  19.  
  20. if(empty($password)){
  21.    echo "Empty password";
  22.    $error = true;
  23. }
  24.    
  25. if($error != true){ //Or if($error == false){ - it's the same
  26. $usercheck = mysql_query("SELECT * FROM users WHERE username='$username' LIMIT 1");
  27.  
  28. if(mysql_num_rows($usercheck) == 0){
  29. mysql_query("INSERT INTO users (username, password)
  30. VALUES ('$username', '$password')") or die(mysql_error());
  31. echo "Registration Succesful";
  32. } else {
  33. echo "Username {$username} is already taken.";
  34. }
  35. }
  36. }
  37.  
  38. mysql_close($con);
  39. ?>
  40.  
  41.  
  42.  
  43. <!DOCTYPE html>
  44. <html>
  45. <body>
  46.  
  47. <form action="register.php" method="POST">
  48. Username: <input type="text" name="username"><br>
  49. Password: <input type="password" name="password"><br />
  50. <input type="submit" value="Login."></form> <br />
  51.  
  52.  
  53.  
  54. </body>
  55. </html>
Add Comment
Please, Sign In to add comment