Search code examples
phphref

change query by using <href>


I have set a default query

$query = "SELECT Image, ISBN, Name, Vol, Release_date, publisher, price  FROM products p order by Name,vol";
echo "<a href = ???>Date</a>";

so that the query can be changed in sorting by date after clicking? Thank you!


Solution

  • The sorting/ordering parameter is order by Name,vol at the end of the query. So you might make it as a variable depending on whether the GET['date'] is present from href.

    <?php
    $sortvar = isset($_GET['date']) ? 'order by '. $_GET['date'] : 'order by Name,vol'; 
    if (isset($_GET['ndsn']) &&  isset($_GET['date']) ) 
        $sortvar =  'order by '. $_GET['date'] . ',' . $_GET['ndsn'];     
    $query = "SELECT Image, ISBN, Name, Vol, Release_date, publisher, price  FROM products p " . $sortvar;
    
    echo "<a href = '?date=Date'>Date</a><br>";
    echo "<a href = '?date=Date&ndsn=NDSN'>Date & NDSN</a><br>";
    echo $query;
    

    IMPORTANT

    you MUST escape GET parameters or parametrize your result query when adding GET parameters into it, cause of SQL-injections possibility.