注意:尝试访问行中 null 类型值的数组偏移量(静态)

我收到这个错误:


注意:尝试访问 null 类型值的数组偏移量


当我想更新的时候。我把代码放在下面,请帮助我如何解决这个问题以及故障和问题在哪里?(正常情况下没有错误,但是当我使用ctrl+U并查看源代码时,所有字段都显示该错误。)我在网站上搜索了很多,但没有找到正确的答案或结果。


在editform.php中


<?php

include_once "InsertDataDatabases.php";

$id = (isset($_POST['id']) ? $_POST['id'] : '');

$query = InsertDataDatabases::SelectDateId($id);

$item = [];

$item = mysqli_fetch_assoc($query);

?>

<form action="updatedata.php" method="post" enctype="multipart/form-data">

    <?php CSRF::CreateToken(); ?>

    <input type="text" name="id" value="<?php echo $item['id']; ?>"><br>

    <label for="name">Name:</label>

    <input type="text" name="name" value="<?php echo $item['name']; ?>"><br><br>

    <label for="email">Email:</label>

    <input type="email" name="email" value="<?php echo $item['email']; ?>"><br><br>

    <input type="file" name="image" value="images/slider/<?php echo $item['image']; ?>"><br>

    <img src="images/slider/<?php echo $item['image']; ?> " width="50px" height="50px" alt=''><br>

    <input type="submit" name="updatebtn" value="UPDATE">


</form>

在 updatedata.php 中


<?php

include_once "InsertDataDatabases.php";

include_once "DataBase.php";

include_once "CSRF.php";

$con = DataBase::ConnectOpen();

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    if (isset($_POST['updatebtn'])) {

        if (CSRF::ValidateToken($_POST["token"])) {

            $id = htmlentities(mysqli_real_escape_string($con, $_POST['id']));

            $name = htmlentities(mysqli_real_escape_string($con, $_POST['name']));

            $email = htmlspecialchars(mysqli_real_escape_string($con, filter_var(trim($_POST['email']), FILTER_SANITIZE_EMAIL)));

            $image = $_FILES['image'];

            InsertDataDatabases::UpdateData($id, $name, $email, $image);

            mysqli_close($con);

            header("Location: index.php");

        }

    }

}

并在 InsertDataDatabases.php 中


public static function SelectDateId($id)

    {

        self::ConnectDataBase();

        return mysqli_query(self::$con, "SELECT * FROM user WHERE id = '{$id}'");

        

    }


慕侠2389804
浏览 89回答 1
1回答

侃侃尔雅

当您查看源代码时,您没有发布表单,因此$_POST未设置该变量。$_POST在尝试使用该变量之前,您应该检查该变量是否已设置。<?phpinclude_once "InsertDataDatabases.php";if (isset($_POST['id'])) {&nbsp; &nbsp; $id = $_POST['id'];&nbsp; &nbsp; $query = InsertDataDatabases::SelectDateId($id);&nbsp; &nbsp; $item = [];&nbsp; &nbsp; $item = mysqli_fetch_assoc($query);&nbsp; &nbsp; ?>&nbsp; &nbsp; <form action="updatedata.php" method="post" enctype="multipart/form-data">&nbsp; &nbsp; &nbsp; &nbsp; <?php CSRF::CreateToken(); ?>&nbsp; &nbsp; &nbsp; &nbsp; <input type="text" name="id" value="<?php echo $item['id']; ?>"><br>&nbsp; &nbsp; &nbsp; &nbsp; <label for="name">Name:</label>&nbsp; &nbsp; &nbsp; &nbsp; <input type="text" name="name" value="<?php echo $item['name']; ?>"><br><br>&nbsp; &nbsp; &nbsp; &nbsp; <label for="email">Email:</label>&nbsp; &nbsp; &nbsp; &nbsp; <input type="email" name="email" value="<?php echo $item['email']; ?>"><br><br>&nbsp; &nbsp; &nbsp; &nbsp; <input type="file" name="image" value="images/slider/<?php echo $item['image']; ?>"><br>&nbsp; &nbsp; &nbsp; &nbsp; <img src="images/slider/<?php echo $item['image']; ?> " width="50px" height="50px" alt=''><br>&nbsp; &nbsp; &nbsp; &nbsp; <input type="submit" name="updatebtn" value="UPDATE">&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; </form><?php }
打开App,查看更多内容
随时随地看视频慕课网APP