PHP LogIn Error
Moderator: General Moderators
PHP LogIn Error
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>
<!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>
Re: PHP LogIn Error
I noticed a couple of things with your database connection and query etc first,
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.
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.
This should give you a good idea where in your code the problem is happening
Code: Select all
mysql_select_db("tutorial");
$logincheck = mysql_query("SELECT * FROM logintable WHERE user='$usernamefield' AND pass='$passwordfield'");
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];
Code: Select all
if(!isset($_POST['username']) || !isset($_POST['password'])){
print "No username/password defined";}
print_r($logincheck)."<br/>"
print_r($rows)Re: PHP LogIn Error
Weiry points out the right solution... 
Re: PHP LogIn Error
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 {
?>Re: PHP LogIn Error
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:I am just getting on the screen when I preview it in the browser.
This very well may be caused bybrmcdani wrote: When I run it through my editor I get a parse error on line 19.
Code: Select all
9. print_r($logincheck)."<br/>"
10. print_r($rows)Re: PHP LogIn Error
Here is a screenshot of what I am getting with the code above. I did close of the end of those with semi-colons.
[img
[/img]
[img
[/img]Re: PHP LogIn Error
Hmm... I am not entirely sure why, but it seems to be breaking on the query line where the HTML <br/> tag is.
Try removing all strings from the 'or die()' :
If that still prints out all the php code like that screenshot did, im not sure what would be causing it to do that.
Code: Select all
$logincheck = mysql_query($q, $con) or die("Query Error: ".mysql_error($con)."<br/>SQL: ".$q);Code: Select all
$logincheck = mysql_query($q, $con) or die(mysql_error($con));