trying to access array offset on value of null

Codes here !

Moderators: egami, macek, gesf

Post Reply
pinkstel
New php-forum User
New php-forum User
Posts: 2
Joined: Mon Mar 15, 2021 4:21 pm

Mon Mar 15, 2021 4:56 pm

Hello everyone! I need help with my code. I can't seem to find the error.

this is the line that gives the error:
$get_courses['courses_id']; ?>"><?php echo $get_courses['courses']; ?></option>
I am trying to get all the courses from tbl_courses . My tbl_courses has only 3 column, and those are course_id, courses, and number_of_enrolled_students .
pinkstel
New php-forum User
New php-forum User
Posts: 2
Joined: Mon Mar 15, 2021 4:21 pm

Mon Mar 15, 2021 5:02 pm

So this is where the code begins
session_start();

$teachers_id = $surname = $first_name = $middle_name = $work_status = $courses = $courses_id = $contact_number = $email_address = $alert_msg = '';
I've put this below the includes
//select all courses
$get_all_courses_sql = "SELECT * FROM tbl_courses ORDER BY courses Asc ";
$get_all_courses_data = $con->prepare($get_all_courses_sql);
$get_all_courses_data->execute();
I am using multiSelect on this.
<div class="control-group">
<label class="control-label" for="multiSelect">Department/s:</label>
<div class="controls">
<select multiple="multiple" id="multiSelect" class="chzn-select span5" name="department[]">
<option>
<?php while ($courses = $get_all_courses_data->fetch(PDO::FETCH_ASSOC)) { ?>
<option value="<?php echo
$get_courses['courses_id']; ?>"><?php echo $get_courses['courses']; ?></option>
<?php } ?>
</select>
<p class="help-block">Start typing to activate auto complete!</p>
</div>
Please help.Thanks!
simonbrahan
php-forum Fan User
php-forum Fan User
Posts: 140
Joined: Mon Jun 08, 2020 2:00 am
Contact:

Tue Mar 16, 2021 12:22 am

Code: Select all

<?php while ($courses = $get_all_courses_data->fetch(PDO::FETCH_ASSOC)) { ?>
<option value="<?php echo
$get_courses['courses_id']; ?>"><?php echo $get_courses['courses']; ?></option>
<?php } ?>
In this code you are assigning a value to $courses in your while loop, but you're looking at $get_courses in the loop body. $get_courses is probably null, and you're trying to treat it like an array; that's what your error means. I'm guessing this is a typo and you meant to use $courses rather than $get_courses inside the loop.
Post Reply