mardi 5 mai 2015

Select statement vs Dropdown list variable in SQL

I'm currently havin an issue with the following code:

<form action="" method="post">
    <input type="text" name="term" placeholder="Search Terms"/>
    <select id=""drop"" name="drop"">
        <option value="CODE">Code</option>
        <option value="SCINAME">Scientific Name</option>
        <option value="COLLECTOR">Holder</option>
        <option value="DATA">Data</option>
    </select>
    <br/>                   
    <button type="submit" class="button primary">Search</button>
</form>

<?php
if (!empty($_REQUEST['term'])) {
    $term = mysql_real_escape_string($_REQUEST['term']);  
    $drop = ($_REQUEST['drop']);  
    $sql = "SELECT * FROM ANIMAIS WHERE '%".$drop."%' LIKE '%".$term."%'"; 
    $r_query = mysql_query($sql); 

    while ($row = mysql_fetch_array($r_query)){  
    echo '<br />code: ' .$row['CODE'];
    echo '<br />Name: ' .$row['SCINAME'];  
    echo '<br /> Colector: '.$row['COLLECTOR'];  
    echo '<br /> Local: '.$row['LOCAL'];  
    echo '<br /> Data: '.$row['DATA'];  
    echo '<br /> Descr: '.$row['DESCRIPTION'];  
    echo '<br />';
    }  
}
?>

The problem is that the code does not "run" the $drop variable, but if i remove the dropdown list from the form and change the following line:

    $sql = "SELECT * FROM ANIMAIS WHERE '%".$drop."%' LIKE '%".$term."%'"; 

to

    $sql = "SELECT * FROM ANIMAIS WHERE SCINAME LIKE '%".$term."%'"; 

It does work, but searching on the SCINAME column, what i wan't do is allowing the dropdown list to select wich SQL column to query, but the actual solution isn't working so well.

Thanks in advance for all help.

Aucun commentaire:

Enregistrer un commentaire