Error when checking user input against data in database with php and SQL

I have a login form in php which I have connected to a sql database in phpmyadmin. The script I have written should get the email and password variables from the user. It should then check the database to make sure the user is registered. Then either redirect the user to their account or notify the user that their details didnt match up and ask them to try again. However no matter what way I change the code it will always redirect to the account page even if the data I submit through the form is random info.

Any help or advice would be great. Thanks. Here is my code

<?php
//require_once 'includes/db_connect.php';//
//require_once 'includes/functions.php';//

$con=mysqli_connect("x","m","z","m");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

session_start(); //starting a PHP session.


if (isset($_SESSION['logged'])){
    header("Location:account.html");
    exit();
}



    $email = $_POST['email'];
    $password =($_POST['password']); 

    $sql = mysqli_query($con,"SELECT * FROM `websiteusers` WHERE email ='" . $email . "' and password ='" . $password . "'") or die (mysqli_error($sql));
    $count = mysqli_num_rows($sql);
    //$row=mysqli_fetch_array($query);//



     if ($count==1){
        $_SESSION[logged] = 1;
        $_SESSION['email'] = $email;
     $_SESSION['password'] =$password;
         header("location:account.html"); /* Redirect the browser */
    }  

        else {
       echo "Sorry those details are not in the database. Click here <a href=\"memberlogin.html\"</a> to try again.";  
        }


exit();

?>

Answers


It's because of the Session Index which you used $_SESSION[logged] and should be $_SESSION['logged']

if ($count==1){
    $_SESSION['logged'] = 1;
    $_SESSION['email'] = $email;
    $_SESSION['password'] =$password;
    header("location:account.html"); /* Redirect the browser */
}  

You forgot '' in the $_SESSION[logged] variable, it should be $_SESSION['logged']


Need Your Help

TinyXML2 returns Null?

c++ xml tinyxml2

I'm currently embedding TinyXML2 into a game engine. I have this code, which in theory should work unless I have been staring at it for too long. Can somebody offer any help? Much appreciated!