如何使用来自Ajax变量的PHP PDO处理许多WHERE子句

时间:2016-07-25 12:03:33

标签: php jquery mysql ajax pdo

我在使用PDO PHP处理多WHERE子句时遇到问题。我的Ajax代码发送两个变量,一个用于livesearch的输入文本,另一个用于下拉菜单。这是我的代码。

Ajax代码:index.php

<p style="text-align: center">Enter your search here: <input type="text" id="search" name="search" placeholder="Enter your search here">&nbsp;&nbsp;
Select education level: <select id="edulevel">
<option value="PHD">PHD</option>
<option value="MASTER">MASTER</option>
<option value="DEGREE">DEGREE</option></select></p>    
<div id="contentBox" class="login, reminder" style="margin:0px auto; width:95%; overflow-y: auto; height:304px;">


<div id="result" class="login"></div>
<script type="text/javascript">

    /*
    setInterval(function(){
        //alert('Refreshing database');
        $("#result").load("res.php", "update=true").fadeIn("slow").text("Refreshing Database");
    }, 10000);
    */


    function update() {
        $.ajax({
            url: 'userres.php',
            dataType: 'text',
            success: function(data) {
                if (parseInt(data) == 0) {
                    $("#result").css({ color: "red" }).text("offline");
                } else {
                    $("#result").css({ color: "green" }).text("online");
                }
            }
        });    // properly end the ajax() invocation
    }


    function ajaxSearchUpdater(){
        $("#result").show();
        var x = $("#search").val();
        var y = $("#edulevel").val();
        $.ajax({
            type:'POST',
            url:'userres.php',
            data:'q='+x+'&e='+y,
            cache:false,
            success:function(data){
                $("#result").html(data)
            }
        });
    }

    $(document).ready(function(e) {
        ajaxSearchUpdater();               // fires on document.ready
        $("#search").keyup(function() {
            ajaxSearchUpdater();           // your function call
        });
        $("#edulevel").click(function() {
            ajaxSearchUpdater();           // your function call
        });
    });
</script>

我的php和数据库代码:userres.php

//print_r($_GET);
$q=$_POST['q'];
if(isset($_POST['e'])){
    $e=$_POST['e'];
    echo $q;
    echo $e;
}


$query="SELECT *
            FROM viewlibrary
            WHERE 
                studentname LIKE :q OR
                matricno LIKE :q OR
                title LIKE :q OR
                education_level LIKE :q OR
                programme LIKE :q OR
                serialno LIKE :q OR
                education_level = :e
            ORDER BY studentname ASC";

$stmt = $db->prepare($query); 
$stmt->bindValue(':q','%'.$q.'%');
$stmt->bindValue(':e',$e, PDO::PARAM_STR);
$stmt->execute();

$a = 0; 

if($stmt->rowCount() > 0){
    $r=$stmt->fetchAll();
    echo "<table class='tablesorter' id='myTable' style='width:97%; table-border: 1'>";
        echo "<thead>";
        echo "<tr>";
        echo "<th>No.</th>";
        echo "<th>No.Matric</th>";
        echo "<th>Name</th>";
        echo "<th>Programme</th>";
        echo "<th>Title</th>";
        echo "<th>Education Level</th>";
        echo "<th>Serial Number</th>";
        echo "<th>Availability</th>";
        echo "</tr>";
        echo "</thead>";
        echo "<tbody>";

    foreach($r as $row){

            echo "<tr align='center'><td>". ($a+1) ."</td><td>". $row['matricno'] ."</td><td>". $row['studentname'] ."</td><td>". $row['programme'] ."</td><td>". $row['title'] ."</td><td>". $row['education_level'] ."</td><td>". $row['serialno'] ."</td><td>". $row['bavailable'] ."</td></tr>";
            $a++;
            //echo $row['education_level'];
    }
    echo "</tbody>";
    echo "</table>";
}
else{
    echo "<p align='center'>Nothing to show you :( I am really sorry for this T_T </p>";
}


?>

这里的问题是我想让我的表格显示只有具有适当教育水平的数据列表,即&#39; Degree&#39;,&#39; Master&#39;,#39; PHD&# 39;这取决于用户的下拉选择。我确实回应了来自ajax的变量,它显示了我点击的内容,但我似乎找不到如何处理这些查询的方法。大多数WHERE条件是livesearch,我想知道如何为我的下拉列表添加条件。

1 个答案:

答案 0 :(得分:0)

我认为您应该通过用户选择的education_level进行搜索。

from

另外,不要尝试在单个SQL语句中使用相同的命名参数两次,例如

SELECT *
FROM viewlibrary
WHERE 
    education_level = :e AND 
    (studentname LIKE :q1 OR
    matricno LIKE :q2 OR
    title LIKE :q3 OR
    education_level LIKE :q4 OR
    programme LIKE :q5 OR
    serialno LIKE :q6)
ORDER BY studentname ASC

这将不返回任何行且没有错误 - 您必须使用每个参数一次且仅使用一次。显然这是预期的行为(根据此错误报告:http://bugs.php.net/bug.php?id=33886),因为可移植性问题。