PHP高级搜索 - 多个参数

时间:2014-08-03 19:10:13

标签: php html mysql sql

我正在开发高级搜索功能:

$colname_SokvansterImp = "-1"; 

mysql_select_db($database_Audiologiska, $Audiologiska);
if (isset($_POST['Personnummer_search'])) 
{   
    //Visa bara det som söks
     $searchword = $_POST['Personnummer_search'];
    $query_SokvansterImp = "SELECT * FROM patient left join person on person.Personnummer = patient.Patient left join vanster_implantat on vanster_implantat.Patv = patient.Patient WHERE vanster_implantat.patv LIKE '%".$searchword."%'";

}
else//Visa all data
{

    $query_SokvansterImp = "select * from patient left join person on person.Personnummer = patient.Patient left join vanster_implantat on vanster_implantat.patv = patient.Patient";

}
$SokvansterImp = mysql_query($query_SokvansterImp, $Audiologiska) or die(mysql_error());
$row_SokvansterImp = mysql_fetch_assoc($SokvansterImp);
$totalRows_SokvansterImp = mysql_num_rows($SokvansterImp);

这适用于使用一个参数进行搜索,但是,我想知道如何让它接受多个参数? " Personnummer_search"是我的领域的名称。我该怎么做才能搜索名称和/或姓氏?我正在使用Dreamweaver

3 个答案:

答案 0 :(得分:1)

添加新的条件块?

if (isset($_POST['Personnummer_search'])) 
{   
    //Visa bara det som söks
     $searchword = mysqli_real_escape_string($_POST['Personnummer_search']);
    $query_SokvansterImp = "SELECT * FROM patient left join person on person.Personnummer = patient.Patient left join vanster_implantat on vanster_implantat.Patv = patient.Patient WHERE vanster_implantat.patv LIKE '%".$searchword."%'";

}
else if(isset($_POST['Name_search']) && isset($_POST['Surname_search']))
{
    $name = mysqli_real_escape_string($_POST['Name_search']);
    $surname = mysqli_real_escape_string(($_POST['Surname_search']);
    $query_SokvansterImp = "YOUR SQL QUERY...";

}
else//Visa all data
{
    $query_SokvansterImp = "select * from patient left join person on person.Personnummer = patient.Patient left join vanster_implantat on vanster_implantat.patv = patient.Patient";
}

答案 1 :(得分:0)

好的,要搜索所有可能的过滤器,您可以执行以下操作:

$where = array();

if (isset($_POST['Personnummer_search'])) 
{   
    $searchword = mysqli_real_escape_string($_POST['Personnummer_search']);
    $where[] =  "vanster_implantat.patv LIKE '%".$searchword."%'";
}

if(isset($_POST['Name_search']))
{
    $name = mysqli_real_escape_string($_POST['Name_search']);
    $where[] =  "nameField LIKE '%".$name."%'";

}

if(isset($_POST['Surname_search']))
{
    $surname = mysqli_real_escape_string($_POST['Name_search']);
    $where[] =  "surnameField LIKE '%".$surname."%'";

}

if(count($where))
{
   $query_SokvansterImp = "SELECT * FROM patient left join person on person.Personnummer = patient.Patient
    left join vanster_implantat on vanster_implantat.Patv = patient.Patient
     WHERE ".implode(" AND ",$where);
}
else//Visa all data
{
    $query_SokvansterImp = "select * from patient left join person on person.Personnummer = patient.Patient left join vanster_implantat on vanster_implantat.patv = patient.Patient";
}

答案 2 :(得分:0)

查看PHP站点,有一个名为" func_get_args"

的函数

http://php.net/manual/en/function.func-get-args.php