PHP LogIn Error

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
brmcdani
Forum Newbie
Posts: 17
Joined: Wed Sep 02, 2009 8:21 pm

PHP LogIn Error

Post by brmcdani »

I just built a login page for my web site. I have looked through my code about 10 million times or so it seems and still cannot get it fixed. Whenever I click the submit button it just brings me back to the login page. Can someone please look over my code and see if you see anything wrong? Thanks!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<?php
$con = mysql_connect("localhost", "tutorial", "**************");
mysql_select_db("tutorial");

$usernamefield = $_POST['username'];
$passwordfield = $_POST['password'];

$logincheck = mysql_query("SELECT * FROM logintable WHERE user='$usernamefield' AND pass='$passwordfield'");

$rows = mysql_fetch_array($logincheck);

if ($rows != "") {
echo "Welcome to the members area";
}
else {

?>
<form id="form1" name="form1" method="post" action="">
<p>
<label>
User Name:
<input type="text" name="username" id="username" />
</label>
</p>
<p>Password:
<label>
<input type="text" name="password" id="password" />
</label>
</p>
<p>
<label>
<input type="submit" name="submit" id="submit" value="Submit" />
</label>
</p>
</form>
</body>
</html>
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: PHP LogIn Error

Post by Weiry »

I noticed a couple of things with your database connection and query etc first,

Code: Select all

mysql_select_db("tutorial");
$logincheck = mysql_query("SELECT * FROM logintable WHERE user='$usernamefield' AND pass='$passwordfield'");
 
Its a good idea to actually tell your mysql_ commands which connection they are using to connect to the database, and storing your SQL in a variable is also a good idea for when you are debugging, so that you can print the query your doing to see what is actually getting passed to the database. This code is just slightly modified, saying that if the query to the database fails, stop the code and print the mysql_error being received and what query caused the error.

Code: Select all

 
mysql_select_db("tutorial",[b] $con[/b]);
$q = "SELECT * FROM [b]`logintable`[/b] WHERE user='$usernamefield' AND pass='$passwordfield'"
$logincheck = mysql_query[b]($q, $con) or die("Query Error: ".mysql_error($con)."<br/>SQL: ".$q)[/b];
 
You should then check if you are receiving any information from the database as well as adding a check to see if you are receiving any information from the $_POST.

Code: Select all

 
if(!isset($_POST['username']) || !isset($_POST['password'])){
   print "No username/password defined";}
print_r($logincheck)."<br/>"
print_r($rows)
This should give you a good idea where in your code the problem is happening
John.Mike
Forum Newbie
Posts: 18
Joined: Fri Sep 04, 2009 11:43 pm
Location: Canton.China

Re: PHP LogIn Error

Post by John.Mike »

Weiry points out the right solution... :D
brmcdani
Forum Newbie
Posts: 17
Joined: Wed Sep 02, 2009 8:21 pm

Re: PHP LogIn Error

Post by brmcdani »

Thanks for yalls help so far, but I am still having trouble. I tried to edit the code the way you described and now I am just getting on the screen when I preview it in the browser. I am extremely new with PHP...obviously and I am really confused. When I run it through my editor I get a parse error on line 19. Can I get some more ideas please?

Code: Select all

<?php
$con = mysql_connect("localhost", "tutorial", "************");
mysql_select_db("tutorial", $con);
$q = "SELECT * FROM `logintable` WHERE user='$usernamefield' AND pass='$passwordfield'";
$logincheck = mysql_query($q, $con) or die("Query Error: ".mysql_error($con)."<br/>SQL: ".$q);
 
if(!isset($_POST['username']) || !isset($_POST['password'])){
   print "No username/password defined";}
print_r($logincheck)."<br/>"
print_r($rows)
 
$usernamefield = $_POST['username'];
$passwordfield = $_POST['password'];
 
$rows = mysql_fetch_array($logincheck);
 
if ($rows != "") {
    echo "Welcome please select your lake";
}
else {
 
?>
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: PHP LogIn Error

Post by Weiry »

brmcdani wrote:I am just getting on the screen when I preview it in the browser.
Could you elaborate on this? are you getting any error's being displayed? Has $_POST got any values being returned (it should appear in an array)?
brmcdani wrote: When I run it through my editor I get a parse error on line 19.
This very well may be caused by

Code: Select all

9.   print_r($logincheck)."<br/>"
10.   print_r($rows)
your missing a ; after the end of those lines.
brmcdani
Forum Newbie
Posts: 17
Joined: Wed Sep 02, 2009 8:21 pm

Re: PHP LogIn Error

Post by brmcdani »

Here is a screenshot of what I am getting with the code above. I did close of the end of those with semi-colons.

[imgImage[/img]
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: PHP LogIn Error

Post by Weiry »

Hmm... I am not entirely sure why, but it seems to be breaking on the query line where the HTML <br/> tag is.

Code: Select all

$logincheck = mysql_query($q, $con) or die("Query Error: ".mysql_error($con)."<br/>SQL: ".$q);
Try removing all strings from the 'or die()' :

Code: Select all

$logincheck = mysql_query($q, $con) or die(mysql_error($con));
If that still prints out all the php code like that screenshot did, im not sure what would be causing it to do that.
Post Reply