I currently have a query that looks like this:
$sql = "SELECT * FROM Results ORDER BY '$SortBy' '$SortIn'";
$SortBy = $_POST['SortBy'];
$SortIn = $_POST['SortIn'];
When I run this query, I recieve no errors and it displays all the data in question, however, it does not display it corretly.
SortBy refers to the table fields (e.g. Reference Number)
SortIn refers to ascending or descending.
If I run the query without using variables, for example:
$sql = "SELECT * FROM Results ORDER BY RefNo asc"
It works fine and sorts the data as expected. Do you have any idea how I can get it to work using the variables? I have a form where the users can choose to sort the data how they please.
The code to the form:
<form action="#" method="post" id="displayTimeTable">
<table>
<tr>
<td>
Sort table on =
<select name="SortBy">
<option value="RefNo" selected="selected">Reference Number</option>
<option value="Date">Date</option>
<option value="Amount">Amount</option>
</select>
</td>
<td>
Sort in =
<select name="SortIn">
<option value="asc" selected="selected">Ascending order</option>
<option value="desc">Descending order</option>
</select>
</td>
</tr>
</table>
<p>
<input type="submit" name="submit" value="Submit"/>
</p>
</form>
After making the change suggested, I recieved the following error:
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given on line 37
Below is the code that it is referring to (Line 37 is the while statement):
$result = mysql_query($sql, $connection);
echo "<table>";
echo "<table border=1>";
echo "<TR><TH>Reference Number</TH><TH>Date</TH><TH>Amount</TH></TR>";
while ($row = mysql_fetch_array($result)) {
echo "<tr><td>";
echo $row['RefNo'];
echo "</td><td>";
echo $row['Date'];
echo "</td><td>";
echo $row['Amount'];
echo "</td></tr>";
}
echo "</table>";