用户名/密码组合PHP MySQL登录/注册错误

时间:2013-08-23 12:04:50

标签: php mysql

我正在关注phpacademy的教程进行登录和注册。 我按照Youtube上的脚本。在第3部分,一切都很顺利,直到最后一部分功能。如果我输入也在te数据库中的用户名和密码,我会收到消息:Array([0] =>那个用户名/密码

有人可以告诉我,我做错了吗? 这是我的登录部分:

<?php 
include 'core/init.php';



if (empty($_POST) === false) { 
    $username = $_POST['username']; 
    $password = $_POST['password']; 

    if (empty($username) === true || empty($password) === true) {
        $errors[] = 'You need to enter a username and password'; 
    } else if (user_exists($username) === false) {
        $errors[] = 'We can\'t find that username. Have you registered?';
    } else if (user_active($username) === false) {
        $errors[] = 'You haven\'t activated your account!';
    } else {
        $login = login($username, $password);
        if ($login === false) {
        $errors[] = 'That username/password combination is incorrect';

    } else {
        echo 'ok!';

}
}
}
?>

这是我的用户部分(我认为)最后一部分出错了。

<?php
function user_exists($username) {
    $username = sanitize($username);
    return (mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username'"), 0) == 1) ? true : false;
}

function email_exists($email) {
    $email = sanitize($email);
    return (mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `email` = '$email'"), 0) == 1) ? true : false;
}

function user_active($username) {
    $username = sanitize($username);
    return (mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username' AND `active` = 1"), 0) == 1) ? true : false;
}

function user_id_from_username($username) {
    $username = sanitize($username);
    return mysql_result(mysql_query("SELECT `user_id` FROM `users` WHERE `username` = '$username'"), 0, 'user_id');
}

function user_id_from_email($email) {
    $email = sanitize($email);
    return mysql_result(mysql_query("SELECT `user_id` FROM `users` WHERE `email` = '$email'"), 0, 'user_id');
}

function login($username, $password) {
    $user_id = user_id_from_username($username);

    $username = sanitize($username);
    $password = md5($password);

    return (mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username' AND `password` = '$password'"), 0) == 1) ? $user_id : false;
}
?>

希望有人可以帮助我。

提前致谢

0 个答案:

没有答案