Poteito
Poteito

Reputation: 129

Array returns null

I'm trying at the moment to create a login with PHP and MySQL but I'm stuck. The array that's supposed to give me Data from the database only returns "Null" I used var_dumb().

This is the index.php file :

<?php
include_once './Includes/functions.php';
?>
<!DOCTYPE html
<html>
    <head>
        <meta charset="utf-8">
    </head>

    <body>
        <div>
            <form method="POST">
                <label>User ID :</label>
                <input id="login_username" name="login_username" type="login"><br>
                <label>Password :</label>
                <input id="login_password" name="login_password" type="password" ><br>
                <input id="login_submit" name="login_submit" type="submit">
            </form>
        </div>
    </body>
</html>

This is the function.php file :

<?php
    require_once 'dbconnect.php';

    function SignIn() {
        $lUser = $_POST['login_username'];
        $lPassword = md5($_POST['login_password']);
        $querySQL = "SELECT * FROM tblUser WHERE dtUser='$lUser' AND dtPassword='$lPassword'";

    $queryResult = mysqli_query($dbc, $querySQL);
    while ($row = mysqli_fetch_assoc($queryResult)) {
        $dataArrayLogin[] = $row;
    }
    if ($lUser == $dataArrayLogin['dtUser'] && $lPassword == $dataArrayLogin['dtPassword']) {
        echo $dataArrayLogin;
        $popup = "Login Succeed";
        echo "<script type='text/javascript'>alert('$popup');</script>";
        $_SESSION['user'] = $lUser;
        header("Location: ./british.php");
    } else {
        echo $dataArrayLogin;
        $popup = "Login Failed";
        echo "<script type='text/javascript'>alert('$popup');</script>";
    }
}

if (isset($_POST['login_submit'])) {
    SignIn();
}
?>

Could you help me out ?

Upvotes: 0

Views: 88

Answers (4)

Kanishka
Kanishka

Reputation: 363

Change query like this

$querySQL = "SELECT * FROM tblUser WHERE dtUser=' ".$lUser." ' AND dtPassword=' ".$lPassword." ' ";

Upvotes: 0

Peter
Peter

Reputation: 9123

You need to start debugging your script. Below are the steps I would take:

  • Do a var_dump() on the $_POST to see if it contains all values you want
  • echo the $querySQL to see if all values are put in correctly
  • Check your database if it actually contains the record with which you are trying to login
  • Fetch mysql errors using mysqli_error();

That should bring your error to light.

Edit:

  • I often find it usefull to place some echos throughout my script to find out what parts of the code are being accessed and what parts are being skipped.
  • I would add a LIMIT 1 to your query as there should only be one user with the entered credentials. This way you'll also be able to skip the while loop.

Upvotes: 0

vaso123
vaso123

Reputation: 12391

This could be, because you have no results?

Anyway, I've checked your code, and it's not good, because you are try to use this:

$dataArrayLogin['dtUser']

There is no 'dtUser' key in your $dataArrayLogin.

When you fetching the row, you are put it into a while cycle, and collect the data into an array:

while ($row = mysqli_fetch_assoc($queryResult)) {
    $dataArrayLogin[] = $row;
}

Remove the while cycle. Simple use:

$dataArrayLogin = mysqli_fetch_assoc($queryResult);

And if you echo an array, the result will be Array. Use var_dump instead.

Upvotes: 0

Ariful Haque
Ariful Haque

Reputation: 3750

in your html form, you don't have <form method="POST" action="SignIn">. so when you submit the form, its not going somewhere.

Upvotes: 0

Related Questions