Jump to content

problem with simple login script, not displaying data.


87dave87

Recommended Posts

Hi,

 

I am trying to create a very simple login page, where the user types their details into this form: -

 

<form method="post" action="members.php" onsubmit="return dis(this)">

  <div class="heading">Members Area</div>

            Username

            <input name="username" type="text" size="15">

Password

<input name="password" type="password" size="15">

<input type="submit" value="Login">

          </form>

 

It then should match the results on members.php to those details entered, I want to then display information about the user from the database, see this query example: -

 

<?
$name_query = mysql_query("SELECT firstname FROM members WHERE username = '%".mysql_real_escape_string($_POST["username"])."%' and password = '%".mysql_real_escape_string($_POST["password"])."%'") or die( mysql_error() );

<p class="heading">Hello
<?  
while ($name = mysql_fetch_row($name_query)) 
{   
  	foreach ($name as $field) echo "$field";
}
?>
</p>

?>

All that displays though is 'Hello' and not the first name, which should output: Hello John
  

try removing the percentage signs from your query string, they're only used when doing a LIKE query

 

also if you check for a result to the query before trying to loop through it you have a chance of displaying an error message.

 

$result = mysql_query()
if(!$result){
die("There was an error with your query");
}
while ($name = mysql_fetch_row($name_query)) 
{   
foreach ($name as $field) echo "$field";
}
etc ...

You basically have that. If you return a firstname from the query then you have verified that the username and password match what was entered in the form.

 

Why are you doing a foreach inside a while loop?? Is there a valid reason for looping inside a loop?

 

$query="SELECT firstname FROM members WHERE username = mysql_real_escape_string($_POST["username"])' and password = 'mysql_real_escape_string($_POST["password"]);
$result = mysql_query($query);

if(!$result){
die("There was an error with your query");
}
while ($name = mysql_fetch_object($query)) 
{  
      echo 'Hello '. $name->firstname;
};

 

Hi,

 

I tried the above, this is my code: -

 

<?
$name_query = mysql_query("SELECT firstname FROM members WHERE username = '".mysql_real_escape_string($_POST["username"])."' and password = '".mysql_real_escape_string($_POST["password"])."'");
$details_query = mysql_query("SELECT memberid, firstname, surname, DATE_FORMAT(joined, '%d %b %Y'), rentals FROM members WHERE username = '".mysql_real_escape_string($_POST["username"])."' and password = '".mysql_real_escape_string($_POST["password"])."'");
$due_query = mysql_query("SELECT rentalsdue, DATE_FORMAT(rentalsduedate, '%d %b %Y') FROM members WHERE username = '".mysql_real_escape_string($_POST["username"])."' and password = '".mysql_real_escape_string($_POST["password"])."'"); 
$result = mysql_query($name_query);
?>

<p class="heading">
<?  
if(!$result){
echo "<script>window.location='index.php'</script>";
}
while ($name = mysql_fetch_row($name_query)) 
{   
echo "Hello ";
  	foreach ($name as $field) echo "$field";
}
?>

 

It redirects even if the username/password is correct.

the check that you are doing to see if the query ran properly is actually running a query against the result of the query that you ran earlier in your code.

 

jeez thats a bad explanation.... i'll explain further......

 

your variable, $name_query is actually storing the result of the query rather than the sql code itself, so when you are trying to run the query with $result = mysql_query($name_query); you are actually trying to query the result of a query you have already run

 

simple fix is...

 

where you have

$name_query = mysql_query("SELECT firstname FROM members WHERE username =

 

replace with

$name_query = "SELECT firstname FROM members WHERE username = ..............."

 

and double check whether you actually need to run the other $details_query and $due_query queries where they are

i'm guessing that the error is now something to do with trying to fetch a row from the sql string rather than from a result, in which case you will also need to rename this bit

 

from

while ($name = mysql_fetch_row($name_query))

 

to

while ($name = mysql_fetch_row($result))

this is what I now have then: -

 

<?
$name_query = "SELECT firstname FROM members WHERE username = '".mysql_real_escape_string($_POST["username"])."' and password = '".mysql_real_escape_string($_POST["password"])."'";
$details_query = mysql_query("SELECT memberid, firstname, surname, DATE_FORMAT(joined, '%d %b %Y'), rentals FROM members WHERE username = '".mysql_real_escape_string($_POST["username"])."' and password = '".mysql_real_escape_string($_POST["password"])."'");
$due_query = mysql_query("SELECT rentalsdue, DATE_FORMAT(rentalsduedate, '%d %b %Y') FROM members WHERE username = '".mysql_real_escape_string($_POST["username"])."' and password = '".mysql_real_escape_string($_POST["password"])."'"); 
$result = mysql_query($name_query);
?>

<p class="heading">
<?  
if(!$result){
echo "<script>window.location='index.php'</script>";
}
while ($name = mysql_fetch_row($result)) 
{   
echo "Hello ";
  	foreach ($name as $field) echo "$field";
}
?>

 

It logs in, but the redirect isnt working now  ???


<?php
$name_query = "SELECT firstname FROM members WHERE username = '".mysql_real_escape_string($_POST["username"])."' and password = '".mysql_real_escape_string($_POST["password"])."'";
$details_query = mysql_query("SELECT memberid, firstname, surname, DATE_FORMAT(joined, '%d %b %Y'), rentals FROM members WHERE username = '".mysql_real_escape_string($_POST["username"])."' and password = '".mysql_real_escape_string($_POST["password"])."'");
$due_query = mysql_query("SELECT rentalsdue, DATE_FORMAT(rentalsduedate, '%d %b %Y') FROM members WHERE username = '".mysql_real_escape_string($_POST["username"])."' and password = '".mysql_real_escape_string($_POST["password"])."'"); 
$result = mysql_query($name_query);

if(mysql_num_rows > 0) // This says if any results are returned ....
{
	while($row=mysql_fetch_object($result)) // while there are results to display
	{
		echo '<p class="heading"> Hello,'. $row->firstname; // echo Hello "FIRST NAME"

	}

}
else // else there were no results returned
{
	echo "<script>window.location='index.php'</script>"; // so echo the redirect statement

}
?>

 

Try that,

 

I restructured the code a little.

 

Nate

I tried the above code, if the user/pass is wrong it redirects, BUT it still redirects if the user/pass is right.

 

See: http://www.buttonbash.com/vortexvideos/index.php

 

type anything in the login box to see what happens with the wrong user/pass.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.