gpt4 book ai didi

php - 按多个字段搜索。有时按一个字段,有时按多个字段

转载 作者:可可西里 更新时间:2023-11-01 08:10:14 26 4
gpt4 key购买 nike

我有搜索表单。在这里多个领域。有时我会提交一个字段,有时提交两个字段值,有时提交多个字段值。

     if (isset($_POST['search'])) {
$projectName = $_POST['pName'];
$clientId = $_POST['s_by_clientName'];
$departmentId = $_POST['s_by_department'];
$statusName = $_POST['s_by_status'];
if (!empty($projectName))
{
$searchSql = mysql_query("select * from project_list where projectName='$projectName'");
}

if (!empty($clientId))
{
$searchSql = mysql_query("select * from project_list where client_id='$clientId'");
}

if (!empty($departmentId))
{
$searchSql = mysql_query("select * from project_list where department_id='$departmentId'");
}

if (!empty($statusName))
{
$searchSql = mysql_query("select * from project_list where status='$statusName'");
}

}

这些查询仅用于按单个字段搜索。 如何进行按一个或多个字段值执行搜索的查询 可能吗??

最佳答案

在查询变量中使用串联

 $searchSql ="select * from project_list where 1=1 ";
if (isset($_POST['search'])) {
$projectName = $_POST['pName'];
$clientId = $_POST['s_by_clientName'];
$departmentId = $_POST['s_by_department'];
$statusName = $_POST['s_by_status'];
if (!empty($projectName))
{
$searchSql. = " AND projectName='$projectName'";
}
if (!empty($clientId))
{
$searchSql. = " AND client_id='$clientId'";
}
if (!empty($departmentId))
{
$searchSql. = " AND department_id='$departmentId'";
}
if (!empty($statusName))
{
$searchSql. = " AND status='$statusName'";
}
}
$result=mysql_query($searchSql);

注意:mysql_query() 已在 PHP 5.5 中弃用并在 PHP 7 中删除。请更新以使用 PDO 的 mysqli 库。

关于php - 按多个字段搜索。有时按一个字段,有时按多个字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36952539/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com