进行PHP MySql搜索

时间:2018-01-02 10:49:11

标签: php search mysqli

我正在我的应用中制作搜索功能, 我有用户名,名称和地区, 所以任何人都可以告诉我实现它的最佳方法。 它应该像用户在搜索中输入的内容一样,脚本应该搜索数据库并获取所需的详细信息。 //我的代码在这里

<?php
include_once("db.php");
if(isset($_POST['district'])){
    $district=$_POST['district'];
}
if(isset($_POST['designation'])){
    $design=$_POST['designation'];
}
if(isset($_POST['name'])){
    $name=$_POST['name'];
}
if(isset($_POST['department'])){
    $dept=$_POST['department'];
}
$result=array();
$response=array();
if((isset($design)) &&(!isset($district)) && (!isset($name)))
{
    $a=$design;
    $stmt=$db->prepare("SELECT * FROM search WHERE designation LIKE ?");
    $stmt->bind_param("s",$a);
    $stmt->execute();
    $d=$stmt->get_result();
    while($b=$d->fetch_array(MYSQLI_ASSOC)){
        $result[]=$b;
}
}
else if((isset($district)) &&(!isset($design)) && (!isset($name)))
{
    $a=$district;
    $stmt=$db->prepare("SELECT * FROM search WHERE district LIKE ?");
    $stmt->bind_param("s",$a);
    $stmt->execute();
    $d=$stmt->get_result();
    while($b=$d->fetch_array(MYSQLI_ASSOC)){
        $result[]=$b;

    }
}
else if((isset($name)) &&(!isset($district)) && (!isset($district))){
    $a=$name;
    $stmt=$db->prepare("SELECT * FROM search WHERE name LIKE ?");
    $stmt->bind_param("s",$a);
    $stmt->execute();
    $d=$stmt->get_result();
    while($b=$d->fetch_array(MYSQLI_ASSOC)){
        $result[]=$b;

    }
}
if((isset($name))&& (isset($design))){
    $stmt=$db->prepare("SELECT * FROM search WHERE name LIKE ? AND designation LIKE ?");
    $stmt->bind_param("ss",$name,$design);
    $stmt->execute();
    $d=$stmt->get_result();
    while($b=$d->fetch_array(MYSQLI_ASSOC)){
        $result[]=$b;
    }
}
if(isset($dept)){
    $stmt=$db->prepare("SELECT * FROM search WHERE department LIKE ?");
    $stmt->bind_param("s",$dept);
    $stmt->execute();
    $d=$stmt->get_result();
    while($b=$d->fetch_array(MYSQLI_ASSOC)){
        $result[]=$b;

    }
}
$response['result']=$result;
echo json_encode($response)
?>

以上是完整的代码。

3 个答案:

答案 0 :(得分:1)

使用ajax请求。 你的MySql查询应该是:

select * from user where uname like %username% or designation like %designation% or district like %district%

别忘了使用参数化查询。

希望有所帮助。

答案 1 :(得分:0)

试试这个..我执行它。

`$query='SELECT * FROM search WHERE ';
$stmt;
if(isset($_POST['district'])){
    $query+='district like @district '
    $stmt=$db->prepare($query);
    $stmt->bind_param(@district,$_POST['district']);

}
if(isset($_POST['designation'])){
    if(strlen($query>27))// district is set
    {
        $query+=' AND ';
    }
   $query+='district like @designation'
    $stmt=$db->prepare($query);
    $stmt->bind_param(@designation,$_POST['designation']);
}
if(isset($_POST['name'])){
    if(strlen($query>27))// district/design  is set
    {
        $query+=' AND ';
    }
    $query+='district like @name'
    $stmt=$db->prepare($query);
    $stmt->bind_param(@name,$_POST['name']);
}
if(isset($_POST['department'])){
  if(strlen($query>27))// district/design/dept  is set
    {
        $query+=' AND ';
    }
    $query+='district like @department'
    $stmt=$db->prepare($query);
    $stmt->bind_param(@department,$_POST['department']);
}
$result=array();
$d=$stmt->get_result();
    while($b=$d->fetch_array(MYSQLI_ASSOC)){
        $result[]=$b;
    }`

答案 2 :(得分:0)

首先,您需要更改li!empty而不是emptyisset,因为您要将变量设置在顶部,以便始终设置并且将在每个条件中,所以我将其更改为空,如下面的代码所示。

之后,你可以创建一个包含满足条件的参数的数组,最后我在SQL中实现了这个并使用join实现了绑定参数,希望它对你有用。

!isset