I received this error :
Notice: Trying to access array offset on the value of type null
when I want to update. I put the codes bellow, pls help me how fix this problem and where are the faults and problems? (In normal situation there is not an error, but when I use ctrl+U and see the source codes, all fields show that error.) I searched a lot on the website, but I didn't find the proper answer or result.
in 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>
in 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");
}
}
}
and in InsertDataDatabases.php
public static function SelectDateId($id)
{
self::ConnectDataBase();
return mysqli_query(self::$con, "SELECT * FROM user WHERE id = '{$id}'");
}
public static function UpdateData($id, $name, $email, $image)
{
self::ConnectDataBase();
$path = "images/slider";
$image_new = self::ImageUpload($image, $path);
if (empty($image_new)) {
$query = self::EditDataId($id);
$item = [];
$item = mysqli_fetch_assoc($query);
$image_new = $item['image'];
}
mysqli_query(self::$con, "update user set name = '$name', email = '$email', image = '$image_new' where id = '$id'");
}