Troubles with MySQL and PHP...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Troubles with MySQL and PHP...

Post by infolock »

Thought I would start a new thread. The old one has gotten so cluttered up, hell, I can't even read it anymore lol... And not to mention, I have new information on my on-going debut to php.

Alright, first things first, a little explanation of what's going on. Currently, I am using a login php script, to authenticate intranet logins. I also created a database with MySQL called auth and a table within auth called userinfo by using the following statement :

--------------------------------------------------------------------------------
create table userinfo (username varchar(12), password varchar(12));
--------------------------------------------------------------------------------

Then, I inserted 2 login/password entries, by using the following statement:

--------------------------------------------------------------------------------
mysql> INSERT INTO username VALUES ('joe','joe');
mysql> INSERT INTO username VALUES ('henry','henry');
--------------------------------------------------------------------------------

then, I use select * from userinfo, and it provides me with a table like this:
-------------------------------------------------------
+--------------+------------+
| username | password |
+--------------+------------+
| joe | joe |
| henry | henry |
+----------+----------------+
--------------------------------------------------------

so, I KNOW the table is working correctly, as I can now display field values in my database....

So, I build my php script, and html files. Here is the code I am using ( I have went ahead and changed the values to what I have on my system at home... ) :

THIS IS FOR LOGIN.PHP

Code: Select all

<? 
# Login Code Written by Matt Slavin 
# Email Me At neophite17@hotmail.com for questions or comments. 

if((!$username) | (!$password)) 
&#123; 
       print ("Please Enter In THe Correct Name");
&#125; 

mysql_connect("localhost","bob","bob") or die("Could not connect to MySQL!!"); 
mysql_select_db("auth") or die("Could not connect to auth Database!");

$sql = "select * from userinfo where password=$password and username=$username"; 

$result = mysql_query($sql) or die("Could not query table userinfo!!");

$num = mysql_numrows($result); 


# If Login Is Okay Do What You Want TO DO to Them 
if ($num > "1") 
&#123; 
          print ("Logged in okay"); 

#Or you can send them somwhere nice like hawaii 
          header("Location: http://localhost/bob/success.htm"); 
          exit; 

# or even fetch feild about them or save sessions 
          session_start(); 
          session_register(username); 
&#125; 

# And we must a have a default error message if login fails 
if ($num == "0")
&#123; 
          print ("Sorry Bub You Need Some Manners In Password"); 
&#125; 

?>

THIS IS FOR INDEX.HTM ( courtesy of mr_griff )

Code: Select all

<html> 
<head> 
  <title>Please Login</title> 
</head> 
<body> 

<form action="login.php" method="post"> 
Username: <input type="text" name="username"><br> 
Password: <input type="password" name="password"><br> 
<input type="submit" name="buttonSelection" value="Login"> 
</form> 

</body> 
</html>

So, I run the code, and try to login. I type joe, joe for the username/password, and instead of seeing the success.htm, I get

Please Enter In THe Correct Name

as if I entered the wrong username/password.

So, I redid the code, and put some more debugg scripts in there that would tell me what was going on, if it's a problem with the code, or connection to the sql database.

So, I changed the following around:

Code: Select all

mysql_connect("localhost","bob","bob") or die("Could not connect to MySQL!!"); 
Print "Connected to MySQL successfully...";
Print "<br>";
Print "Attempting to connect to MySQL Database";
Print "<br>";

mysql_select_db("auth") or die("Could not connect to auth Database!");
Print "Connected to auth Database successfully...";
Print "<br>";
Print "Attempting to connect to Table....";
Print "<br>";

$sql = "select * from userinfo where password=$password and username=$username"; 

$result = mysql_query($sql) or die("Could not query table userinfo!!");

Print "Connected to userinfo Table successfully... Checking Login/Password";
Print "<br>";

$num = mysql_numrows($result);


The result in this, is it displays that I connect to MySQL successfully, and then to the database successfully, but it stops there. It doesn't give me a message saying that I did not connect to the table successfully, NOR does it tell me that it queried the table successfully...

So, What I belive, is there is something wrong with my php script in how it is connecting to the table, or I don't have something setup correctly...

Hope someone can straighten this out for me 8O

heh, thanks in advance
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

This situation is because of register_globals (check out this sticky thread for more information: viewtopic.php?t=511)
Try changing this:

Code: Select all

if((!$username) | (!$password)) 
{ 
       print ("Please Enter In THe Correct Name"); 
} 

mysql_connect("localhost","bob","bob") or die("Could not connect to MySQL!!"); 
mysql_select_db("auth") or die("Could not connect to auth Database!"); 

$sql = "select * from userinfo where password=$password and username=$username";
to this

Code: Select all

if(!isset($_POST&#1111;'username']) || !isset($_POST&#1111;'password'])) { 
       print 'Please Enter In The Correct Name';
       exit();
}

mysql_connect("localhost","bob","bob") or die("Could not connect to MySQL!!"); 
mysql_select_db("auth") or die("Could not connect to auth Database!"); 

$sql = "select * from userinfo where password='{$_POST&#1111;'password']}' and username='{$_POST&#1111;'username']}'";
Mac
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

WHOOT! Thanks for the info!!! I'll try it when I get home ! :D

*praises the wise mods of php* 8)
Post Reply