MySQL database user login not working

I need help I am going by this video tutorial
http://www.youtube.com/watch?v=4oSCuEtxRK8&list=PL7C9AFC3942AC8E40&index=1&feature=plpp_video

which I have mimicked the coding letter for letter outside of changing the mysql login information to mirror the information from my account.

I have the database name as the one pre-assigned by 000webhost and in that I have the table ‘users’ which has 3 data fields, id, username and password. In that I inserted a test username and password.

I have checked over and over again, and made adjustments to the coding that was given to see if I had made an error but Im just not finding it, when comparing to his work.

What happens is at the very end, when he enters his own test username and password, it gives him a blank page because it was successful…

Thats not happening for me… Im getting the die statement “please enter a username and a password”… even though I know I am typing in my test username and pass correctly…

Here is my code (Ive removed my login information and replaced with cap letters) for my logintest.php file


<?php

$username = $POST['username'];
$password = $POST['password'];


if ($username && $password){
  $connect = mysql_connect('mysql10.000webhost.com', 'USERNAME', 'PASSWORD') or die("Couldn't connect!");
  mysql_select_db('DATABASE NAME') or die("Couldn't find DB");
	}
else 
	die("Please enter a username and a password!");

?>



If needed heres my index.php code:


<html>
	<form action='logintest.php' method="POST">
    	Username: <input type="text" name="username"><br>
		Password: <input type="password" name="password"><br>
        <input type="submit" value="Log in"><br>
        </form>
</html>


Also, I have tried to generate any of the errors in the if statement by altering the log in information so that it should obviously not connect to the database, but I still get taken to the else statement. Its making me think its not even making it that far… so the problem has to be elsewhere but I dont know where.

any help would be greatly appreciated! thanks!

first off, it’s $_POST and not $POST
Secondly, are you sure you are using the DATABASE un/pw and not the un/pw you inserted into the tables?

Hi

  1. Yeah I dont know why that didnt paste properly, I do have it as $_POST in my file.

  2. yes very sure. I am not using the test username/pass in the file, Im using the database one, as I mentioned in my post… with the information assigned by 000webhost, etc. I only use the test username/pass when trying to test the actual page to see if its working.

Your login test script is not appropriate. In your code, database connection is possible if
any username and password are submitted (even if they are not in users table).

Your code should be something like:


<?php 
$username = $_POST['username']; 
$password = $_POST['password']; 

if ($username && $password){ 
  $connect = mysql_connect('mysql10.000webhost.com', 'axxxxxx_username', 'mysql_password') or die("Couldn't connect!"); 
  mysql_select_db('axxxxxx_dbname') or die("Couldn't find DB"); 

  // To check entered username and password are matching with the data saved in `users` table
  $query = "select username and password from users where username='$username' and password='$password'";
  $result = mysql_query($query);
  $num = mysql_num_rows($result); 

  if($num==1) { //if matching data exist
  echo "Your login is successful.";
  } else {
  echo "Incorrect username or password. Please go back and try again.";  
  }

} 
else {
   die("Please enter a username and a password!"); 
}
?> 

To test if database connection is possible, you don’t need the form for entering username and password.
The following code would be enough:


<?php
$connect = mysql_connect('mysql10.000webhost.com', 'axxxxxx_username', 'mysql_password') or die("Couldn't connect!"); 
mysql_select_db('axxxxxx_dbname') or die("Couldn't find DB"); 

if($connect) {
echo "DB connected sucessfully.";
} else {
echo "DB connection failed.";
}
?>

The explanation in my previous post seems to be out of issue. So, I would like to add further explanation
as follows:

$connect = mysql_connect(‘mysql10.000webhost.com’, ‘USERNAME’, ‘PASSWORD’) or die(“Couldn’t connect!”);
mysql_select_db(‘DATABASE NAME’) or die(“Couldn’t find DB”);

Looks like your DB information included in the above script is not correct. Otherwise, you should
also have a blank page whatever you entered in the login form. You can get exact DB information
from MySQL section of your control panel except mysql password, which you entered when creating
database.

Please note that you are the only user/owner of your mysql database. For other users, database connection
is possible through your username and password set up when creating database. In other words, for database
connection only, a separate login form is not necessary.

To address this one first, I think what you are telling me here is that my information is not correct in regards to the login information? If so… in my first post I had said I changed that for the purpose of this post as Im not going to put my log in information and password on a public post. If that wasnt what you were saying I dont think Im understanding correctly…

I will work with this last test to see what happens and let you know…
The reason there were no queries set up yet to compare the user login information is because we didnt get that far in the tutorial… thats in the next video. :slight_smile:
The guy doing the tutorials even says that theres no point testing the login information if theres not even a connection to the database set up… and that seems to be where my problem was.

Im going to look at the code you gave me to see if that works and let you know, thank you very much for your response!

Okay heres the update.
I did the test and it returned with that it connected successfully.

However, I copied and pasted the whole thing you had given, and edited the db login info and then when I went back to try and log in, I still get the die statement ‘please enter a username and password’.

What you are saying “login information” means actually database connection information. As I mentioned earlier,
a separate login is not necessary for database connection only. Database connection is possible if your database
connection script is correct. For member/user login, usernames and passwords entered by users should be saved
in the ‘users’ table. For this, you need a separate registration form, and to make an insert query to save
username and password in ‘users’ table. Please try the following code after saving it as “logintest.php”.
The username and password entered by you in the input form, should be displayed instead of a blank page.
If not, you will get again die statement.


<?php
$username = $_POST['username']; 
$password = $_POST['password']; 

echo $username."<br>";
echo $password;

if ($username && $password){ 
  $connect = mysql_connect('mysql10.000webhost.com', 'USERNAME', 'PASSWORD') or die("Couldn't connect!"); 
  mysql_select_db('DATABASE NAME') or die("Couldn't find DB"); 
    } 
else  
    die("Please enter a username and a password!"); 

?>

Oh Em Gee…
Well thanks to your echo code (I still got the die statement) it let me know where the problem was in the code, because it obviously was not recognizing the information I entered.

It was in the index.php file which I had included in my first post at the bottom… I had the values in quotations instead of apostrophes, including the POST section.

Thank you very much!!!

I saw the video tutorial. In index.php file of the video, form action=“login.php” while in your code
it’s ‘logintest.php’. Which one is correct? What’s your file name of php script, where database
connection script is included. Please put consistently double quotes around values.

No that is incorrect and that is where I had erred - double quotes are used primarily for statements and I had forgotton that. In all other cases it should be apostrophes. I hadn’t realized I had put quotation marks around both logintest.php and POST in the index.php file in the form action. I think I was getting thrown off because in HTML you do use quotation marks and so I had forgotton I was working in PHP rather than HTML.

I changed the quotation marks to apostrophes and everything started working fine. So there was nothing wrong with my code in the logintest.php file.

I would not have realized that I had made that error (in the index.php file) without your help though as I was looking in the wrong area.

In html form tag, it doesn’t matter whether the values are enclosed with single quotes (’ ') or
double quotes(" "). My suggestion is that keep the consistency. When I started learning php/form
tag, the example code used double quotes around values. So, I am acccustomed in using double
quotes. However, as far as values are strings it’s OK to use single quotes, and it is known that
using single quotes takes less time than double quotes in parsing/interpreting codes.

To test mixed using of single/double quotes, try the following code after saving it "logintest.php"
as file name. When I test this code, it works fine. This shows that your original code is OK as it is.
I don’t know why it didn’t work; may be it should have worked if you cleared browser cache .


<html> 
    <form action='logintest.php' method="POST"> 
        Username: <input type="text" name="username"><br> 
        Password: <input type="password" name="password"><br> 
        <input type="submit" value="Log in"><br> 
        </form> 
</html>

<?php
$username = $_POST['username'];
$password = $_POST['password'];

echo $username."<br>";
echo $password; 
?>