I'm trying to write a php query that orders trips based on what the user selects. I tried a bunch of things but nothing seems to be working.
<form action = "getbustripordered.php" method="post">
<input type="radio" name="orderBy" value="Country">Country
<input type="radio" name="orderBy" value="TripName">Name <br>
<input type="radio" name="orderType" value="ASC">Ascending
<input type="radio" name="orderType" value="DESC">Descending <br>
<input type="submit" value="Get Ordered List">
In the PHP I have
$orders=array("TripID", "TripName", "StartDate", "Enddate", "Country", "LicenseNum");
$key=array_search($_POST["orderBy"],$orders);
$order=$orders[$key];
$directions=array("ASC", "DESC");
$keys=array_search($_POST["orderType"],$directions);
$direction=$directions[$keys];
$query= 'SELECT * FROM BusTrips ORDER BY $order $direction';
$result=mysqli_query($connection,$query);
if (!$result) {
die("databases query failed.");
}
while ($row = mysqli_fetch_assoc($result)) {
echo '<ul>';
echo $row["TripID"],' ', $row["TripName"], ' ' , $row["StartDate"], ' ' , $row["EndDate"], ' ' , $row["Country"], ' ', $row["LicenseNum"] . "</ul>";
}
mysqli_free_result($result);
When I try to run this, it just says databases query failed, I am not sure what the problem is?