![](/img/trans.png)
[英]I am getting a - Warning: Trying to access array offset on value of type null in
[英]I'm trying to insert data into mysql database, but i kept getting: Warning: Trying to access array offset on value of type null
請將我的表單插入數據庫時遇到問題。 每次我運行代碼時,我得到的錯誤是
警告:第 154 行 C:\xampp\htdocs\netadem\admin\apps\insert.php 中未定義的數組鍵“course_image”
警告:嘗試訪問第 154 行 C:\xampp\htdocs\netadem\admin\apps\insert.php 中 null 類型值的數組偏移量
警告:第 155 行 C:\xampp\htdocs\netadem\admin\apps\insert.php 中未定義的數組鍵“course_video”
警告:嘗試訪問第 155 行 C:\xampp\htdocs\netadem\admin\apps\insert.php 中 null 類型值的數組偏移量
警告:第 159 行 C:\xampp\htdocs\netadem\admin\apps\insert.php 中未定義的數組鍵“course_image”
警告:嘗試訪問第 159 行 C:\xampp\htdocs\netadem\admin\apps\insert.php 中 null 類型值的數組偏移量
警告:第 163 行 C:\xampp\htdocs\netadem\admin\apps\insert.php 中未定義的數組鍵“course_video”
警告:嘗試訪問第 163 行 C:\xampp\htdocs\netadem\admin\apps\insert.php 中 null 類型值的數組偏移量
這是我的表格
<input type="hidden" name="instructor_id" value="<?=$id?>">
<div class="row mb-3">
<label for="inputNanme4" class="form-label"><strong>Course Title</strong></label>
<div class="col-sm-10">
<input type="text" name="course_title" class="form-control" id="inputNanme4">
</div>
</div>
<div class="row mb-3">
<label for="floatingTextarea" class="col-sm-2 col-form-label"><strong>Short Description</strong></label>
<div class="form-floating">
<textarea class="form-control" name="course_description" placeholder="Address" id="floatingTextarea" style="height: 100px; padding: 10px;"></textarea>
</div>
</div><br>
<div class="row mb-3">
<label class="form-label"><strong>Course Image</strong></label>
<div class="col-sm-10">
<input type="file" name="course_image" class="form-control" id="formFile">
</div>
</div><br>
<div class="row mb-3">
<label class="form-label"><strong>Course Video</strong></label>
<div class="col-sm-10">
<input type="file" name="course_video" class="form-control" id="formFile">
</div>
</div><br>
<!-- Quill Editor Full -->
<p><strong>Course Details </strong>(As much as possible, describe what this course is all about.)</p>
<div class="form-floating">
<textarea class="form-control" name="course_details" id="floatingTextarea" style="height: 250px; padding: 10px;"></textarea>
</div><br><br>
<!-- End Quill Editor Full -->
<a href="my-courses.php" class="btn btn-danger float-start m-2">Back</a>
<button type="submit" name="create-course" class="btn btn-primary float-start m-2">Submit</button>
這是我的 php 插入邏輯代碼
if (isset($_POST['create-course']))
{
$img_folder = "../uploads/images/";
$videos_folder = "../uploads/videos/";
if(!file_exists($img_folder))
{
mkdir($img_folder,0777,true);
file_put_contents($img_folder."index.php", "<?php //silence");
file_put_contents("../uploads/index.php", "<?php //silence");
}
elseif(!file_exists($videos_folder))
{
mkdir($videos_folder,0777,true);
file_put_contents($videos_folder."index.php", "<?php //silence");
}
$current_time = time();
$instructor_id = $_POST['instructor_id'];
$course_title = mysqli_real_escape_string($connect, htmlentities($_POST['course_title']));
$course_description = mysqli_real_escape_string($connect, htmlentities($_POST['course_description']));
$course_details = mysqli_real_escape_string($connect, htmlentities($_POST['course_details']));
$course_image = $current_time.$_FILES['course_image']['name']; (Line 154)
$course_video = $current_time.$_FILES['course_video']['name']; (line 155)
//validate image
$image_allowed = ['image/jpeg','image/jpg','image/png'];
$img_type = $_FILES['course_image']['type']; (Line 159)
//validate video
$video_allowed = ['video/mp4','video/avi','video/3gp','video/mov','video/mpeg'];
$video_type = $_FILES['course_video']['type']; (Line 163)
$check_image = in_array($img_type, $image_allowed);
$check_video = in_array($video_type, $video_allowed);
if($check_image) // check file extension
{
if ($check_video)
{
$query = "INSERT INTO courses (user_id,course_title,course_description,course_details,course_image,course_video) VALUES ('$instructor_id','$course_title','$course_description','$course_details','$course_image','$course_video')";
$query_run = mysqli_query($connect, $query);
if ($query_run)
{
$img_tmp = $_FILES['course_image']['tmp_name'];
$destination = $img_folder.$current_time.$_FILES['course_image']['name'];
move_uploaded_file($img_tmp, $destination);
$video_tmp = $_FILES['course_video']['tmp_name'];
$vid_dst = $videos_folder.$current_time.$_FILES['course_image']['name'];
move_uploaded_file($img_tmp, $vid_dst);
$_SESSION['success'] = "Data Inserted Successfully.";
redirect('../my-courses.php');
}
else
{
$_SESSION['error'] = "Operation failed!";
redirect('../courses-add.php');
}
}
elseif(!$check_video)
{
$_SESSION['vid_error'] = "Only videos of type .mp4, .avi, .3gp, .mov, .mpeg are allowed.";
redirect('../courses-add.php');
}
}
elseif(!$check_image)
{
$_SESSION['img_error'] = "Only images of type .jpg, .jpeg, .png are allowed.";
redirect('../courses-add.php');
}
}
如果有人能告訴我原因,我會很高興,我的代碼沒有運行。
感謝大家。 我實際上發現我忘記將 'enctype="multipart/form-data" 添加到表單標簽中。 感謝大家的幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.