0

I have now reinstalled XAMPP and i got rid of most errors.

But i still have one error. This one :

Warning: mysql_connect(): Access denied for user 'admin'@'localhost' (using password: YES) in D:\xampp\htdocs\login_form\login.php on line 7
Could not connect to database.

SQL DATABASE :

login > users > 
Id = 1
Username = admin
Password = admin

Id = 2
Username = login_system
Password = login_system

index.php :

<!DOCTYPE html>
<html>
  <body>
    <form action="login.php" method="POST">
      <p>Username : </p><input type="text" name="user">
      <p>Password : </p><input type="password" name="pass">
      <br>
      <input type="submit" value="Login">
    </form>
  </body>
</html>

login.php

<?php
  //$ = variables?
  $username = "admin";
  $password = "admin";
  $hostname ="localhost";

  $database_handle = mysql_connect($hostname, $username, $password) or die ("Could not connect to database.");

  $selected = mysql_select_db("login", $database_handle);

  $myusername = $_POST['user'];
  $mypassword = $_POST['pass'];

  $myusername = stripslashes($myusername);
  $mypassword = stripslashes($mypassword);

  //Checks if the mySQL database matches the user inputs.
  $query = "SELECT * FROM users WHERE Username='".$myusername."' and Password='".$mypassword."'";

  $result = mysql_query($query,$database_handle);
  $count = mysql_num_rows($result);

  if ($count==1){
    echo 'Databse connection and code was successful!';
  }
 ?>

It said i needed to add more info which i couldn't.

Random text here..

Happy Crayfish
  • 47
  • 1
  • 11
  • There is nothing in this code sample that would cause an infinite loading loop. So you issue is probably in the code you don't want to share. – Sean Jun 29 '15 at 22:46
  • By code you don't want to share you mean what exactly? This is all the .php files i have :o – Happy Crayfish Jun 29 '15 at 22:47
  • 1
    The page just keeps loading? Anything error logged? Why is the `stripslashes` being used? You should switch to a DB driver that supports prepared statements, PDO or mysqli. In the meantime mysql_real_escape_string should prevent injections.. You also don't need to declare the variable twice, just use the function in the first declaration. – chris85 Jun 29 '15 at 22:47
  • 1
    Don't use `mysql_*` functions as they are deprecated, instead use [mysqli](http://php.net/manual/en/book.mysqli.php) or [PDO](http://php.net/manual/en/book.pdo.php). Thus, you should use your connection while querying `$result = mysql_query($query,$database_handle);`. – Burak Jun 29 '15 at 22:48
  • i watched a tutorial, which used stripslashes, he said it was for security reasons. – Happy Crayfish Jun 29 '15 at 22:48
  • I have no idea what sqli or PDO is, i'll do some research. – Happy Crayfish Jun 29 '15 at 22:50
  • I would really reconsider that tutorial, for that matter any video tutorial. Here's the doc on that function, http://php.net/manual/en/function.stripslashes.php. `Returns a string with backslashes stripped off. (\' becomes ' and so on.) Double backslashes (\\) are made into a single backslash (\).` Example http://sandbox.onlinephpfunctions.com/code/838309fffd2ce0b63ef9b1796ee2b343c7e4f070. Also what if the user has a slash or quote in their password? You are now changing it going this route. PDO and MYSQLi are other drivers to connect to your DB. – chris85 Jun 29 '15 at 22:54
  • As to your current issue I see nothing that would put you in a loop. What does the network tab of your developer console show? – chris85 Jun 29 '15 at 22:55
  • All this doesn't make sense to me. I'm just gonna try to find a different mysql - php tutorial... :3 – Happy Crayfish Jun 29 '15 at 22:56
  • It shows nothing at all.... – Happy Crayfish Jun 29 '15 at 22:57
  • @HappyCrayfish check the answer please.thanks – Alive to die - Anant Jun 29 '15 at 22:59
  • Go to the index page keep the developer console open check the "preserve log". Process the form. Does the form processing show? Is the submitted data in there? Does the form show as pending or some sort of failure? – chris85 Jun 29 '15 at 23:09
  • Can't find the preserve.log. :/ – Happy Crayfish Jun 29 '15 at 23:26
  • Have errors inside apache php log. PHP Warning: mysqli_connect(): MySQL server has gone away in D:\xampp\htdocs\phpTutorial\login.php on line 9 PHP Warning: mysqli_connect(): Error while reading greeting packet. PID=11044 in D:\xampp\htdocs\phpTutorial\login.php on line 9 [ PHP Warning: mysqli_connect(): (HY000/2006): MySQL server has gone away in D:\xampp\htdocs\phpTutorial\login.php on line 9 PHP Fatal error: Maximum execution time of 30 seconds exceeded in D:\xampp\htdocs\phpTutorial\login.php on line 9 – Happy Crayfish Jun 29 '15 at 23:28
  • You need to isolate the issue, then post a question. Heres a thread that may be useful. http://stackoverflow.com/questions/10474922/error-2006-hy000-mysql-server-has-gone-away – chris85 Jun 30 '15 at 00:16

1 Answers1

0

Please try this and let me know if this works

<?php
  //$ = variables?
  $username = "root";
  $password = "test123";
  $hostname ="localhost:8080";

  $database_handle = mysql_connect($hostname, $username, $password) or die ("Could not connect to databse. Check your internet connection.");

  $selected = mysql_select_db("login", $database_handle);

  $myusername = $_POST['user'];
  $mypassword = $_POST['pass'];

  $myusername = stripslashes($myusername);
  $mypassword = stripslashes($mypassword);

  //Checks if the mySQL database matches the user inputs.
  $query = "SELECT * FROM users WHERE Username='".$myusername."' and Password='".$mypassword."'";

  $result = mysql_query($query,$database_handle);
  $count = mysql_num_rows($result);

  if ($count==1){
    echo 'Databse connection and code was successful!';
  }
 ?>
Narayan Bhandari
  • 426
  • 3
  • 11
  • No progress. I have a ton of errors in apache php logs though! PHP Warning: mysqli_connect(): MySQL server has gone away in D:\xampp\htdocs\phpTutorial\login.php on line 9 PHP Warning: mysqli_connect(): Error while reading greeting packet. PID=11044 in D:\xampp\htdocs\phpTutorial\login.php on line 9 [ PHP Warning: mysqli_connect(): (HY000/2006): MySQL server has gone away in D:\xampp\htdocs\phpTutorial\login.php on line 9 PHP Fatal error: Maximum execution time of 30 seconds exceeded in D:\xampp\htdocs\phpTutorial\login.php on line 9 – Happy Crayfish Jun 29 '15 at 23:31
  • try putting $hostname ="localhost"; instead of $hostname ="localhost:8080"; – Narayan Bhandari Jun 30 '15 at 01:31