在SQL中选择语句与下拉列表变量

时间:2015-05-06 04:30:16

标签: php html sql select drop-down-menu

我目前对以下代码存在疑问:

<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 />';
    }  
}
?>

问题是代码没有“运行”$ drop变量,但是如果我从表单中删除下拉列表并更改以下行:

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

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

它确实有效,但是在SCINAME列上搜索,我不想做的是允许下拉列表选择要查询的SQL列,但实际的解决方案效果不好。

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

列名不需要'%%'。它应该是 -

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

答案 1 :(得分:0)

您的陈述和查询中的错别字

for var i = 0; i < downloads_requests.count; i++ {

            var r :AnyObject!
            r = downloads_requests.objectAtIndex(i) as! Request

            if r != nil{

                (r as! Request).cancel()
            }
        }

应该是

<select id=""drop"" name="drop"">
           ^^    ^^            ^^

您在属性值上添加了额外的<select id="drop" name="drop"> 逗号

,您的查询必须

""