无法在未定义的索引上找到错误:id?

时间:2015-03-14 06:00:52

标签: php

我目前正在获得"未定义索引"我的登录脚本中出错。这是我的代码:

<?php
//Login Script
if (isset($_POST["user_login"]) && isset($_POST["password_login"])) {
    $user_login = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["user_login"]); // filter everything but numbers and letters
    $password_login = preg_replace('#[^A-Za-z0-9]#i', '', $_POST["password_login"]); // filter everything but numbers and letters
    $md5password_login = md5($password_login);

    $sql = mysqli_query($con, "SELECT id FROM users WHERE username='$user_login' AND password='$md5password_login' LIMIT 1"); // query the person

    //Check for their existance
    $userCount = mysqli_num_rows($sql); //Count the number of rows returned

    if ($userCount == 1) {
        while($row = mysqli_fetch_array($sql,MYSQLI_NUM)) {
             $rahul = $row["id"];
        }

        $_SESSION["id"] = $rahul;
        $_SESSION["user_login"] = $user_login;
        $_SESSION["password_login"] = $password_login;
        // exit("<meta http-equiv=\"refresh\" content=\"0\">");
    } else {
        echo 'That information is incorrect, try again';
        exit();
    }
}
?>

出现的具体错误是:

Notice: Undefined index: id in C:\wamp\www\hootpile\designer\index.php on line 461

,第461行是$rahul = $row["id"];

的第461行

1 个答案:

答案 0 :(得分:0)

使用

mysqli_fetch_array($sql,MYSQLI_ASSOC)

所以你没有得到索引为0,1,2等的数组,而是一个带有索引“id”,“user_login”等的关联数组。