简体   繁体   中英

What am I doing wrong? Prepared statement login

This is my login.php files, when the form directs to this page to run the code, the screen says

"this page is not working"

What am I doing wrong and how can I fix it? Also, when I have got the code to work, the cookies have not been set, which makes me think that the error is around there.

//IF REMEMBER ME IS TURNED ON
$stmt = $conn->prepare("SELECT * FROM users WHERE email=? AND password=?");
$stmt->bind_param("ss", $SearchEmail, $SearchPassword);

// set parameters and execute
$SearchEmail = $email;
$SearchPassword = $password;
$stmt->execute();
if($stmt->fetch() == true) {
    $result = $stmt->get_result();
    while($row = $result->fetch_assoc()) {
        setcookie("SIT_name", $row['FirstName'], time()+3600*24*365*10, '/');
        setcookie("SIT_acc_type", $row['acc_type'], time()+3600*24*365*10, '/');
    }

    $stmt = $conn->prepare("UPDATE users SET last_log=? WHERE email=? AND password=?");
    $stmt->bind_param("sss", $time, $SearchEmail, $SearchPassword);
    $time = 'Time: '.date("h:i:sa").', Date: '.date("d/m/Y").'.';
    $SearchEmail = $email;
    $SearchPassword = $password;
    if ($stmt->execute()) {
        echo "Success";
        //Header('Location: ../');
    }
} else {
    echo "Wrong Username or Password!";
}

Screenshot (from comment section) 在此处输入图片说明

Try this

//IF REMEMBER ME IS TURNED ON
$stmt = $conn->prepare("SELECT * FROM users WHERE email=? AND password=?");
$stmt->bind_param("ss", $SearchEmail, $SearchPassword);

// set parameters and execute
$SearchEmail = $email;
$SearchPassword = $password;
$stmt->execute();

if($stmt->fetch() == true) {
    $result = $stmt->get_result();
    while ($row = $result->fetch_array()) {
        setcookie("SIT_name", $row['FirstName'], time()+3600*24*365*10, '/');
        setcookie("SIT_acc_type", $row['acc_type'], time()+3600*24*365*10, '/');
    }

    $stmt = $conn->prepare("UPDATE users SET last_log=? WHERE email=? AND password=?");
    $stmt->bind_param("sss", $time, $SearchEmail, $SearchPassword);
    $time = 'Time: '.date("h:i:sa").', Date: '.date("d/m/Y").'.';
    $SearchEmail = $email;
    $SearchPassword = $password;
    if ($stmt->execute()) {
        echo "Success";
        //Header('Location: ../');
    }
} else {
    echo "Wrong Username or Password!";
}

After many hours of messing around with the code, I have figured out a fix, it's not the best but it'll do as I don't have time (I'm 16 and currently doing my GCSEs). The working code is as follows:

//IF REMEMBER ME IS TURNED ON
$stmt = $conn->prepare("SELECT FirstName, acc_type FROM users WHERE email=? AND password=?");
$stmt->bind_param("ss", $email, $password);

// set parameters and execute
$email = $OrigEmail;
$password = $OrigPassword;

$stmt->execute();
$stmt->bind_result($FirstName, $acc_type);
$stmt->store_result();

if ($stmt->num_rows == 0) 
{
    Header('Location: ../?err=1');

    $stmt->close();

    return 0;
}
else
{
    $FirstNames = array();
    $acc_types = array();

    while($stmt->fetch())
    {
        $FirstNames[] = $FirstName;
        $acc_types[] = $acc_type;

    }
    print $FirstNames[0];

    setcookie("FirstName", $FirstNames[0], time()+3600*24*365*10, '/');
    setcookie("SIT_acc_type", $acc_type[0], time()+3600*24*365*10, '/');

    $stmt = $conn->prepare("UPDATE users SET last_log=? WHERE email=? AND password=?");
    $stmt->bind_param("sss", $time, $SearchEmail, $SearchPassword);
    $time = 'Time: '.date("h:i:sa").', Date: '.date("d/m/Y").'.';
    $SearchEmail = $OrigEmail;
    $SearchPassword = $OrigPassword;
    if ($stmt->execute()) {
        //echo "Success";
        Header('Location: ../?suc=1');
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM