I’m working on an assignment to create a management system for a high school bus transport system. Everything has been going swimmingly until now, for some reason, I cannot access the “learner_trips” (the table where trip information for every learner is stored) table.
I’m trying to access a learner’s trip information, display said information for an admin of the system, and allow the admin to email the learner’s parent with that trip information or allow said admin to remove the learner from the table.
But all I get is this error:
Warning: Trying to access array offset on value of type bool in C:xampphtdocsstorsviewadmin_lists.php on line 146
Now I understand that the error probably means the PDO function I’m using for this is returning false. But the same query in that function works and returns what it should when I put it into phpMyAdmin with an ID.
I haven’t encountered such an error anywhere else in my code or tables.
I’ve tried changing the query in the function to use implicit joins instead of INNER JOINs, I tried casting the ID gotten through POST to integers, but nothing works. I get the same error. I’ve looked at some other questions here on SO related to my problem, but none of them address my problem.
Here’s an example of what the query should return in PHP PDO, after running in phpMyAdmin:
My related code in index.php:
case "send_trip_info":
// Send email with trip info to parent
// get input
$l_id = filter_input(INPUT_POST, "l_id");
// Get trip info and parent
$info = getPassengerInfo($l_id);
$parent_id = get_parent_id($l_id);
$parent_info = get_parent_info($parent_id);
$to_name = $parent_info["name"];
$to = $parent_info["email"];
$name = $learners[$l_id]["name"];
$p1_name = $info["p1_name"];
$p1_time = $info["p1_time"];
$p2_name = $info["p2_name"];
$p2_time = $info["p2_time"];
// body
$body = "Dear $to_name, <br><br> Here is the trip information for $name: <br><br>
<b>Pickup Point and Time</b>: $p1_name at $p1_time<br>
<b>Dropoff Point and Time</b>: $p2_name at $p2_time<br>
<br>
Kind Regards<br>
Strive High
";
$alt = "Dear $to_name, nn Here is the trip information for $name: nn
PICKUP Point and Time: $p1_name at $p1_timen
DROPOFF Point and Time: $p2_name at $p2_timen
n
Kind Regardsn
Strive High
";
send_mail("STORS Trip Info For $to_name", $body, $alt, $to, $to_name);
break;
I’ve also received the same kind of error in the “get info and parent” section of the code above.
<!-- Passenger List (learner_trips table) -->
<div class="learner_section" id="passengerList">
<div class="mb-3 table-responsive">
<!-- Passenger List table here -->
<table class="table table-primary table-striped table-bordered">
<thead>
<tr>
<th>Learner Name</th>
<th>Pickup Point</th>
<th>Pickup Time</th>
<th>Dropoff Point</th>
<th>Dropoff Time</th>
<th colspan="2">Options</th>
</tr>
</thead>
<tbody id="pass_list">
<?php for ($i = 0; $i < count($learners); $i++): ?>
<?php if (checkLearnerPassengerStatus($learners[$i]['id'])) : ?>
<?php $info = getPassengerInfo($learners[$i]["id"]); ?>
<tr>
<td name="fullName"><?php echo $learners[$i]["name"] . " " . $learners[$i]["surname"] ?></td>
<td name="p1_point_name"><?php echo $info["p1_name"] ?></td>
<td name="p1_pickup_time"><?php echo $info["p1_time"] ?></td>
<td name="p2_point_name"><?php echo $info["p2_name"] ?></td>
<td name="p2_dropoff_time"><?php echo $info["p2_time"] ?></td>
<td>
<button class="btn btn-danger" value="<?php echo $info['id'] ?>" name="cancelPassBtn">Remove From List</button>
</td>
<td>
<form method="POST">
<input hidden name="action" value="send_trip_info"/>
<input hidden name="l_id" value="<?php echo $learners[$i]['id'] ?>"/>
<button class="btn btn-success" type="submit" name="emailBtn">Email Trip To Parent</button>
</form>
</td>
</tr>
<?php endif; ?>
<?php endfor; ?>
</tbody>
</table>
The code above is from admin_lists.php where the error appears.
Here’s my code from my PDO function:
// Get a learner's passenger info
function getPassengerInfo($id)
{
global $db;
$query = "SELECT id, t1.point_name AS p1_name, t1.pickup_time AS p1_time, t2.point_name AS p2_name, t2.dropoff_time AS p2_time FROM learner_trips
INNER JOIN route_points AS t1 ON t1.point_num = learner_trips.pickup_id
INNER JOIN route_points AS t2 on t2.point_num = learner_trips.dropoff_id WHERE learner_id = :id";
$statement = $db->prepare($query);
$statement->bindValue(":id", $id);
$result = $statement->fetch();
$statement->closeCursor();
return $result;
}
My full code is available in this git repo: https://github.com/moppdev/ICT3715_STORS_PROJECT
I’m not sure what I could be doing wrong? Theoretically, everything should be firing but for some reason it just refuses to? I’m not getting any other database related errors either… This error has been driving me crazy for the past two days…