0

I am new at php and MySQL, and I might have a stupid mistake here, but I just cant find it.... The script returns that I have entered wrong password or username, but I am completely sure that both are in the database, in table login, and are entered correctly. The mistake must be in the script..

<?php
session_start(); 

$db = mysqli_connect ("127.0.0.1", "root", "", "hobbybook")or die("Cannot connect to database");

//to check what user has written in form, make $username and $password using fields 'username' and 'password'
$username = $_POST['username'];
$password = $_POST['password'];

//to take care of MySQL injection 
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysqli_real_escape_string($username);
$password = mysqli_real_escape_string($password);

$sql="SELECT * FROM login WHERE username='$username' AND password='$password'";
$result = mysqli_query($db, $sql);

$count = mysqli_num_rows($result);

//if result is one, corresponding username and password were found    
if ($count==1) { 

$_SESSION['username']=$username;
header("location:memberarea.php");
}
else {
    echo $username, " - you have entered wrong username or password";
    //header("location:login.php");
}

mysqli_close($db);
?>
1

1 Answer 1

0

Do some debugging. Halt the execution using die() OR exit() and print the count result to see if everything is fine up to that point.

$count = mysqli_num_rows($result);

echo $count;

exit(0);

Steps for debugging :

  1. Check what $count is printing. If $count is not printing 1 then,
  2. repeat the steps with $sql, Print $sql, copy the statement from browser and directly execute in phpmyadmin or mysql console. Check the result,
  3. If you get some record then check connection

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.