Free Web Hosting Forum
Go Back   Free Web Hosting Forum > Website Building > Web Programming
Reload this Page MySQL database user login not working
Reply
 
Thread Tools Display Modes
(#1 (permalink))
Old
Junior Member
aqcheryl is on a distinguished road
 
Posts: 23
Join Date: Mar 2009
Default MySQL database user login not working - 04-23-2012, 05:40 AM

I need help I am going by this video tutorial
http://www.youtube.com/watch?v=4oSCu...ure=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 Code:
<?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:

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!
Reply With Quote
Sponsored Links
(#2 (permalink))
Old
Leder678's Avatar
Senior Member
Leder678 is on a distinguished road
 
Posts: 1,615
Join Date: Jan 2009
Location: Norway
Send a message via MSN to Leder678
Default 04-23-2012, 06:07 AM

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?


Follow me on twitter @Mortenrb

W3Fools - Read and learn

Please AT LEAST read the 10 bolded lines of the TOS at:
http://www.000webhost.com/includes/tos.php
Reply With Quote
(#3 (permalink))
Old
Junior Member
aqcheryl is on a distinguished road
 
Posts: 23
Join Date: Mar 2009
Default 04-23-2012, 06:47 AM

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.
Reply With Quote
(#4 (permalink))
Old
Senior Member
grace1004 is on a distinguished road
 
Posts: 735
Join Date: Dec 2010
Default 04-23-2012, 12:46 PM

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 Code:
<?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 Code:
<?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.";
}
?>

Last edited by grace1004; 04-23-2012 at 01:05 PM.
Reply With Quote
(#5 (permalink))
Old
Senior Member
grace1004 is on a distinguished road
 
Posts: 735
Join Date: Dec 2010
Default 04-23-2012, 01:50 PM

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

Quote:
$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.

Last edited by grace1004; 04-23-2012 at 02:00 PM.
Reply With Quote
(#6 (permalink))
Old
Junior Member
aqcheryl is on a distinguished road
 
Posts: 23
Join Date: Mar 2009
Default 04-23-2012, 07:40 PM

Quote:
Originally Posted by grace1004 View Post
The explanation in my previous post seems to be out of issue. So, I would like to add further explanation
as follows:



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...

Last edited by aqcheryl; 04-23-2012 at 07:56 PM.
Reply With Quote
(#7 (permalink))
Old
Junior Member
aqcheryl is on a distinguished road
 
Posts: 23
Join Date: Mar 2009
Default 04-23-2012, 07:48 PM

Quote:
Originally Posted by grace1004 View Post
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 Code:
<?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 Code:
<?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.";
}
?>
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.
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!
Reply With Quote
(#8 (permalink))
Old
Junior Member
aqcheryl is on a distinguished road
 
Posts: 23
Join Date: Mar 2009
Default 04-23-2012, 08:16 PM

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'.
Reply With Quote
(#9 (permalink))
Old
Senior Member
grace1004 is on a distinguished road
 
Posts: 735
Join Date: Dec 2010
Default 04-23-2012, 11:09 PM

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 Code:
<?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!"); 

?>

Last edited by grace1004; 04-23-2012 at 11:40 PM.
Reply With Quote
(#10 (permalink))
Old
Junior Member
aqcheryl is on a distinguished road
 
Posts: 23
Join Date: Mar 2009
Default 04-24-2012, 12:10 AM

Quote:
Originally Posted by grace1004 View Post
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 Code:
<?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!!!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.5.2
vBulletin Skin developed by: vBStyles.com