currently I have a simple search query which works as follows:
$username = $_SESSION['username'];
$chosencategory = $_GET['category'];
$price = $_GET['price'];
$search = $_GET['search'];
$terms = explode(" ", $search);
if ($price && $chosencategory){
$sql = "SELECT * FROM people WHERE MATCH (lname,fname) AGAINST (:search IN BOOLEAN MODE) AND category='$chosencategory' ORDER BY price $price";
$q = $conn->prepare($sql) or die("failed!");
$q->bindValue(':search',"%".$search."%",PDO::PARAM_STR);
$q->execute();
}
When a user chooses, for example, “display price lowest to highest” the value sent through to $_GET['price'] = ASC, however i am not sure if this is a safe way to sort the results, does anyone have a better way?
also this method is not the best as when the user choses a sort option such as “display price lowest to highest” the dropdown box echoes the value which has been sent to the $_GET[‘price’] which is “ASC” so in the dropdown box it reads ASC after the form as been sent!
Sorry if this is confusing please comment if you would like me to re-explain this, any help or advice is much appreciated!!
Related to your value binding and sql injection, you should also check that values are set before using. if you enabled
error_reporting(E_ALL)you would see lots of Undefined warnings. Here are some tips/changes: