使用PHP和MysQL无法让数据库更新上次登录时间?

时间:2009-10-08 05:29:16

标签: php mysql

这是我最后一个登录部分,它似乎在这里工作。我想我搞砸了?

    // Query the database:
    $q = "SELECT user_id, first_name, user_level FROM users WHERE (email='$e' AND pass=SHA1('$p')) AND active IS NULL";      
    $r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));


    if (@mysqli_num_rows($r) == 1) { // A match was made.

        // Register the values & redirect:
        $_SESSION = mysqli_fetch_array ($r, MYSQLI_ASSOC); 
        $q = "UPDATE users SET last_login = NOW() WHERE (email='$e' AND pass=SHA1('$p')) AND active IS NULL"; 
        // save the info to the database
        $r= mysqli_query( $dbc );
        mysqli_free_result($r);
        mysqli_close($dbc);

这是完整的脚本。

<?php
if (isset($_POST['submitted'])) {
    require_once (MYSQL);

    // Validate the email address:
    if (!empty($_POST['email'])) {
        $e = mysqli_real_escape_string ($dbc, $_POST['email']);
    } else {
        $e = FALSE;
        echo '<p class="error">You forgot to enter your email address!</p>';
    }

    // Validate the password:
    if (!empty($_POST['pass'])) {
        $p = mysqli_real_escape_string ($dbc, $_POST['pass']);
    } else {
        $p = FALSE;
        echo '<p class="error">You forgot to enter your password!</p>';
    }

    if ($e && $p) { // If everything's OK.

        // Query the database:
        $q = "SELECT user_id, first_name, user_level FROM users WHERE (email='$e' AND pass=SHA1('$p')) AND active IS NULL";     
        $r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));


        if (@mysqli_num_rows($r) == 1) { // A match was made.

            // Register the values & redirect:
            $_SESSION = mysqli_fetch_array ($r, MYSQLI_ASSOC); 
            $q = "UPDATE users SET last_login = NOW() WHERE (email='$e' AND pass=SHA1('$p')) AND active IS NULL"; 
            // save the info to the database
            $r= mysqli_query( $dbc );
            mysqli_free_result($r);
            mysqli_close($dbc);

            $url = BASE_URL . 'index.php'; // Define the URL:
            ob_end_clean(); // Delete the buffer.
            header("Location: $url");
            exit(); // Quit the script.

        } else { // No match was made.
            echo '<p class="error">Either the email address and password entered do not match those on file or you have not yet activated your account.</p>';
        }

    } else { // If everything wasn't OK.
        echo '<p class="error">Please try again.</p>';
    }

    mysqli_close($dbc);

} // End of SUBMIT conditional.
?>

2 个答案:

答案 0 :(得分:1)

也许有点猜测,但这看起来很奇怪:

$q = "UPDATE users SET last_login = NOW() WHERE (email='$e' AND pass=SHA1('$p')) AND active IS NULL"; 
// save the info to the database
$r= mysqli_query( $dbc );

更新查询存储在$q中,但$q变量未作为参数传递给mysqli_query

不应该作为第二个参数传递吗?
即:

$r= mysqli_query( $dbc, $q );

(这是select查询的内容 - 但不是update查询

答案 1 :(得分:1)

您可能甚至没有运行查询,

$q = "UPDATE users SET last_login = NOW() WHERE (email='$e' AND pass=SHA1('$p')) AND active IS NULL"; 
                        // save the info to the database
                        $r= mysqli_query( $dbc );

应该是

$q = "UPDATE users SET last_login = NOW() WHERE (email='$e' AND pass=SHA1('$p')) AND active IS NULL"; 
                        // save the info to the database
                        $r= mysqli_query( $dbc ,$q);

我建议您检查是否使用affected_rows

进行了更新