0

hi, i have a problem in login form, whene i try to see if it works, but there is an error,
so hope if you can help me and thx ;)
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\wamp\www\test\login.php on line 11
Username or password

<?php
if(isset($_POST['user']))
{
$user = $_POST['user'];
$pass= md5 ($_POST['pass']);

$con = mysql_connect("localhost", "root", "");
if(!$con){die('could not connect: '.mysql_ereor());}

mysql_select_db("test", $con);
if(mysql_num_rows(mysql_query("SELECT * FROM users where username='$user' AND password='$pass' ")))
{
$result = mysql_query("SELECT * FROM users WHERE username = '$user' AND password = '$pass'");
while($row = mysql_fetch_array($result))
{
	$expire = time()+60*60*24*30;
	setcookie("uid", $row['uid'], $expire);
	echo "Logged in. as <b>".$row['username']."</b>";
	}

}else{
		echo"<b>Username or password</b><br><br>";
}
mysql_close($con);
}
echo "<form method='post'>
Username:<input type='text' name='user'><br>
Password:<input type='password' name='pass'><br>
<input type='submit' value='LOG IN'>
</form>";
?>
3
Contributors
18
Replies
20
Views
5 Years
Discussion Span
Last Post by yachile
0

run the query before using mysql_num_rows()

0
<?php
if(isset($_POST['user']))
{
$user = $_POST['user'];
$pass= md5 ($_POST['pass']);

$con = mysql_connect("localhost", "root", "");
if(!$con){die('could not connect: '.mysql_ereor());}

mysql_select_db("test", $con);

$query = "SELECT * FROM users where username='$user' AND password='$pass' ";
$result = mysql_query($query);
if(mysql_affected_rows() == 1)
{
   while($row = mysql_fetch_array($result))
   {
	$expire = time()+60*60*24*30;
	setcookie("uid", $row['uid'], $expire);
	echo "Logged in. as <b>".$row['username']."</b>";
   }
}else{
		echo"<b>Username or password</b><br><br>";
}
mysql_close($con);
}
echo "<form method='post'>
Username:<input type='text' name='user'><br>
Password:<input type='password' name='pass'><br>
<input type='submit' value='LOG IN'>
</form>";
?>

Try that =)

0

i'm beginner, so how can i run the query!!! can you correct this code plz!!!

0
$query = "SELECT * FROM users where username='$user' AND password='$pass' "; // This is the query
$result = mysql_query($query); // This executes the query

Is there an error?

0

thx phorce it works, but it display the same message even if i use the correct username and password

0

i want if the username and password are correct go to index page, but if the password or username is correct display "password or username is wrong"

0

both whene i use right and wrong info it displays "Username or password is wrong"

0

Change this part:

$query = "SELECT * FROM users where username='$user' AND password='$pass' ";
$result = mysql_query($query);
if(mysql_affected_rows() == 1)
{
    while($row = mysql_fetch_array($result))
    {
        $expire = time()+60*60*24*30;
	setcookie("uid", $row['uid'], $expire);         
        header("Location: index.php");
    }
}else{
  echo"<b>Your username/password did not match those on the system!</b><br><br>";
}

Hope this helps =)

Edited by phorce: n/a

0

the same problem it displays Your username/password did not match those on the system! even if they are correct

0

no it displays "Your username/password did not match those on the system!" even if the info are correct

0

Please try this then:

<?php
if(isset($_POST['user']))
{
$user = $_POST['user'];
$pass= md5($_POST['pass']);

$con = mysql_connect("localhost", "root", "");
if(!$con){die('could not connect: '.mysql_ereor());}

mysql_select_db("test", $con);

$query = "SELECT * FROM users where username='$user' AND password='$pass'";
$result = mysql_query($query);
if(mysql_num_rows($result) == 1)
{
   while($row = mysql_fetch_array($result))
   {
	$expire = time()+60*60*24*30;
	setcookie("uid", $row['uid'], $expire);
	echo "Logged in. as <b>".$row['username']."</b>";
   }
}else{
		echo"<b>Username or password</b><br><br>";
}
mysql_close($con);
}
echo "<form method='post'>
Username:<input type='text' name='user'><br>
Password:<input type='password' name='pass'><br>
<input type='submit' value='LOG IN'>
</form>";
?>

Also, please make sure that the value of 'password' in your database is in MD5 otherwise, it won't work :)

0

it displays Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\wamp\www\test\login.php on line 14
Username or password

0

hey force if you have login-logout form send it to me that will be better

0

change again to this:

if(mysql_affected_rows() == 1)
{
   while($row = mysql_fetch_array($result))
   {
	$expire = time()+60*60*24*30;
	setcookie("uid", $row['uid'], $expire);
	echo "Logged in. as <b>".$row['username']."</b>";
   }
}

Also, can you please make sure that the 'password' value is MD5 (i.e. NOT plain text) I don't see why there is a problem :(

0

no it doesnt work and im sure i work with md5 in database

0

Open up your database, in phpmyadmin or something and check to see if the value for 'password' in the table is like:

d713e38281718ed2d493fdcfa44dac7e

or if it's like:

sfsf

=)

0

it's like d713e38281718ed2d493fdcfa44dac7e
if you have login-logout form plz send it to me

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.