Php 在未定义的索引上找不到错误:id?

Php 在未定义的索引上找不到错误:id?,php,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 lette

我当前在登录脚本中遇到“未定义索引”错误。这是我的密码:

<?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();
    }
}
?>
,第461行是
$rahul=$row[“id”]

使用

mysqli_fetch_array($sql,MYSQLI_ASSOC)
因此,您不会得到索引为0、1、2等的数组,而是索引为“id”、“user\u login”等的关联数组

mysqli_fetch_array($sql,MYSQLI_ASSOC)