时间:2018-04-26 21:54:52

标签: php mysql indexing error-handling undefined

我有一个网站,用户可以输入用户名,按提交按钮,查看数据库中是否存在该用户名。我在第22行的C:\ xampp \ htdocs \ xxx \ xxx \ xxx中收到错误未定义索引:user_id,这是我编写代码的行:$ user_id = $ _ SESSION [& #39; USER_ID'];

我想知道导致这个问题的原因以及如何解决这个问题。我已经发布了以下完整代码,以备不时之需。非常感谢任何帮助

   <?php
include('conn.php');

session_start();

 ?>

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Username</title>
    </head>
<body>

    <form method="post" action=""> 
        Username:<input type="text" name="username"/></br>
                 <input type="submit" name="submit" value="Login"/>
    </form>

    <?php 
    $user_id=$_SESSION['user_id'];

    $result=mysqli_query($con,"SELECT * FROM account WHERE usr_id='$user_id'");
    $data=mysqli_fetch_assoc($result);

    $firstname=$data['usr_password'];



        if (isset($_POST['submit'])) {
        $username=$_POST['username'];

        $result=mysqli_query($con,"SELECT * FROM account WHERE usr_username='$username'")or die (mysqli_error());

         if(!$result){
                header('location:index.php');
            echo 'No user with the name '. $username. " ";
         }

         {
         }

         if (mysqli_num_rows($result) != 1)
        {
            echo 'No user with the name '. $username. " ";
        }

         else{
            $data=mysqli_fetch_assoc($result);              
                $_SESSION['user_id']=$data['usr_id'];
                    echo 'The username ' . $firstname." is correct!";
                    mysqli_query($con,"INSERT INTO account (usr_scan) VALUES ('$username')")or die (mysqli_error());
         }      

        }
?>
</body>
</html>

0 个答案:

没有答案