dousu8767 2014-04-19 01:28
浏览 50

实时AJAX搜索同时具有多个输入PHP / MySQL

I'm working on a live search in AJAX (for a database of powder horns) with two inputs, the first for the year, and the second for the conflict. The problem I'm having is when one of the inputs is blank, it only returns elements in the table that have no date or conflict name listed. Instead I want blank inputs to behave as null, and show everything from the table. I can fix the date section using a conditional to check if (!$date), but I can't get the select input to behave the same way if the user chooses blank again.

Any ideas on how to fix this? Especially in one query without a ton of PHP if/else?

HTML

<h1> Powder Horn Search Engine </h1>




Date <input id="date" type="text" name="variable">

To <select id="conflict">
        <option>  </option>
        <option value="French & Indian War">French & Indian War</option>
        <option value="Revolutionary War">Revolutionary War</option>
        <option value="War of 1812">War of 1812</option>    
    </select>

<div id="result">
</div>

Javascript

        var date="";
var conflict=""

    $(document).ready( function () {    
//Send date to PHP
$("#date").keyup(function(){
    date = $("#date").val();
    conflict=$("#conflict").val();

    $.ajax({
          type: "POST",
          data: {date: date, conflict:conflict},
          url: "powderhornsearch.php",
            success: function(data){ //response param
            $("#result").html(data);
            }

        });
});

//Send conflict to PHP
$("#conflict").change(function(){
conflict=$("#conflict").val();
date = $("#date").val();

$.ajax({
          type: "POST",
          data: {conflict: conflict, date:date},
          url: "powderhornsearch.php",
            success: function(data){ //response param
            $("#result").html(data);
            }

        });


}); 
    });

PHP

    $date = $_POST['date'];
$conflict=$_POST['conflict'];
    $result = mysql_query ("SELECT * FROM powderhorns WHERE Date LIKE $date AND Conflict LIKE '$conflict'  ", $connection);
    if (!$result) {
            die("Database query failed:" . mysql_error());
        }
    echo "<table>";
    while ($row = mysql_fetch_array($result)) {
    echo "<tr>";
        echo "<td>".$row[0]."</td>".
            "<td>".$row[1]."</td>".
            "<td>".$row[2]."</td>".
            "<td>".$row[3]."</td>".
            "<td>".$row[4]."</td>";     
    echo "</tr>";   
    }
    echo "</table>";

EDIT

I changed the value of the blank select to "1" because it seemed like MySQL was having trouble with the empty string. I got everything to work by using the following code. As I am new to programming, if anyone has a suggestion on how to make it more condensed or elegant, I'd love your ideas. Thanks so much!

    if (!$date) {
$date="";
}

if ($conflict=="1" && !$date)
{
$result = mysql_query ("SELECT * FROM powderhorns", $connection);
}

else if ($conflict==1){
    $result = mysql_query ("SELECT * FROM powderhorns WHERE Date LIKE $date  ", $connection);
}

else if (!$date && $conflict){
    $result = mysql_query ("SELECT * FROM powderhorns WHERE Conflict LIKE '$conflict'  ", $connection);
}

else{
    $result = mysql_query ("SELECT * FROM powderhorns WHERE Conflict LIKE '$conflict' and Date LIKE $date  ", $connection);
}   
  • 写回答

1条回答 默认 最新

  • dtwzwmv87399 2014-04-19 01:40
    关注

    change it to:

    $condition=1;
    if(trim($date)) $condition .=" and Date LIKE '$date'";
    if(trim($conflict)) $condition .="  and Conflict LIKE '$conflict'";
    
    $result = mysql_query ("SELECT * FROM powderhorns WHERE $condition ", $connection);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器