我的简单登录系统出错。试图从数据库中读取

时间:2015-11-13 19:30:00

标签: php mysql

我有这个错误“警告:mysql_result():在第14行的C:\ xampp \ htdocs \ core \ functions \ users.php中的MySQL结果索引13中找不到user_id”

我想我不知道如何从我的数据库中读取,看起来像是

this

这是我的用户代码

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

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

    function user_id_from_username($username){
            $username = sanitize($username);
            return mysql_result(mysql_query("SELECT `uID` FROM `user` WHERE `uUserName` = '$username'"), 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(`uID`) FROM `user` WHERE `uUserName` = '$username' AND `uPassword` = '$password'"), 0) == 1) ? $user_id : false;
    }

&GT;

这是我的登录系统代码

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 cannot find that Username, have you registered?';

        } else if(user_active($username) === false){
                $errors[] = 'You have not activated your account.';
        } 
        else {
                $login = login($username,$password);
                if($login === false){
                        $errors[] = 'The username or password is incorrect';
                } else {
                        echo 'ok';
                }
        }

        print_r($errors);

}

2 个答案:

答案 0 :(得分:1)

第13行有一个错误,您指定'user_id'作为偏移量。

如果该行存在,您只需获取uID列并存储该值。

@IBOutlet weak var dispTable: UITableView!
override func viewDidLoad() {
    super.viewDidLoad()

    dispTable.dataSource = self
    dispTable.delegate = self
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    if let cell = tableView.dequeueReusableCellWithIdentifier("DispCell") as? DispTableViewCell {
        let flight1: Flight = Flight()
        let flight2: Flight = Flight()
        curFlightsIda = [Flight]()
        Flight1.Num = "1221"
        Flight2.Num = "1331"

        curFlightsIda.append(Flight1)
        curFlightsIda.append(Flight2)

        for voo in curFlightsIda {
            let lbl = UILabel(frame: CGRectMake(0, 0, 100, 50))
            lbl.textAlignment = NSTextAlignment.Center
            lbl.text = voo.Num
            cell.contentView.addSubview(lbl)
        }

        return cell
    } else {
        return DispTableViewCell()
    }
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}

func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 100
}

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

然后你可以修改你的登录功能来检查user_id&gt; 0或不。如果它大于0,那么你就得到了该用户的id。

答案 1 :(得分:0)

目前,我看到的唯一问题是您的user_id_from_username功能。

您正在尝试为不存在的字段设置偏移量,而mysql找不到它。所以它引发了一个错误:

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

尝试上述操作或取消uID,因为它不是必需参数,而是可选参数。

  

插入必填项,如果您的PHP版本支持,则此时您应该使用mysqli而不是mysql