0

I'm working on my school project and I need a simple login functionality. It was working 20 minutes ago but then I perhaps made some mistake. It doesn't show any error message. The database seems to be alright.

'jmeno' = name, 'heslo' = password

<?php $mysqli = new mysqli("localhost","admin","admin","uzivatele");

    if(isset( $_POST['heslo']) && isset($_POST['jmeno'])){
        $username = $_POST['heslo'];
        $password = $_POST['jmeno'];
        /* defends SQL injection */
       // $username = stripslashes($username);
        //$password = stripslashes($password);
        //$password = mysqli_real_escape_string($mysqli, ($_POST['heslo']));
        //$username = mysqli_real_escape_string($mysqli, $_POST['jmeno']);

         $sqllogin = "SELECT * FROM prihlaseni WHERE jmeno = '".$username."'  AND heslo = '".$password."' LIMIT 1";

        $result = mysqli_query($mysqli, $sqllogin);
        if (!$result) {
        die(mysqli_error($mysqli));
        }
        $count = mysqli_num_rows($result);       

        if ($count == 1) {
        session_start();
        $_SESSION['loggedin'] = true;
        header('Location: home.php');

        }else {

        echo "<script language='javascript'>alert('Wrong password!');</script>";
    }
    }
?>
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Václav Zeman
  • 606
  • 7
  • 21

2 Answers2

3

I think you mixed post values. Try :

$username = $_POST['jmeno'];
$password = $_POST['heslo'];
Paul
  • 785
  • 2
  • 13
  • 21
0

I suggest debugging as follows:

<?php $mysqli = new mysqli("localhost","admin","admin","uzivatele");

    if(isset( $_POST['heslo']) && isset($_POST['jmeno'])){
        $username = $_POST['heslo'];
        $password = $_POST['jmeno'];
        /* defends SQL injection */
       // $username = stripslashes($username);
        //$password = stripslashes($password);
        //$password = mysqli_real_escape_string($mysqli, ($_POST['heslo']));
        //$username = mysqli_real_escape_string($mysqli, $_POST['jmeno']);

         $sqllogin = "SELECT * FROM prihlaseni WHERE jmeno = '".$username."'  AND heslo = '".$password."' LIMIT 1";


        echo $sqllogin; //check the sql query string
        $result = mysqli_query($mysqli, $sqllogin);
        print_r($result);
        if (!$result) {
        die(mysqli_error($mysqli));
        }
        $count = mysqli_num_rows($result);       

        if ($count == 1) {
        session_start();
        $_SESSION['loggedin'] = true;
        header('Location: home.php');

        }else {

        echo "<script language='javascript'>alert('Wrong password!');</script>";
    }
    }
?>

If sql string seems correct try querying the database directly and check output. Probably there its not getting the $_POST vars, and not returning a valid $result. Also I suggest you to not handle and save passwords like that but using hash functions like md5(string).

liu
  • 73
  • 7