如何显示已搜索和已过滤值的查询表?

时间:2018-06-12 15:02:58

标签: php search filter sql-like concat

我的搜索和过滤PHP代码不会显示我搜索和过滤的值的查询表。我该如何解决这个问题?

PHP

if(isset($_POST['search']))
{
    $valueToSearch = $_POST['valueToSearch'];
    // search in all table columns
    // using concat mysql function
    $query = "SELECT * FROM `details` WHERE CONCAT('id','date_of_entry','region','province_of_provider') LIKE '%".$valueToSearch."%'";
    $search_result = filterTable($query);
}
 else {
    $query = "SELECT * FROM `details`";
    $search_result = filterTable($query);
}

// function to connect and execute the query
function filterTable($query)
{

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";

    $connect = mysqli_connect($servername, $username, $password, $dbname);
    $filter_Result = mysqli_query($connect, $query);
    return $filter_Result;
}

while ($trainee = mysqli_fetch_array($search_result)){

    echo "<tr>";
    echo "<td>".$trainee['id']."</td>"; 
    echo "<td>".$trainee['date_of_entry']."</td>";
    echo "<td>".$trainee['region']."</td>";
    echo "<td>".$trainee['province_of_provider']."</td>";
    echo "</tr>";

}// End while

?>

当我从textbox搜索和过滤时,表行不包含任何值,包括其边框,但每列的表头仍然存在。此外,当我将搜索留空并单击过滤器按钮时,所有表行都包含数据库表中找到的所有数据的所有值。

第一编辑:

我改变了我的:

$query = "SELECT * FROM `details` WHERE CONCAT('id','date_of_entry','region','province_of_provider') LIKE '%".$valueToSearch."%'";

对此:

$query = "SELECT * FROM `details` WHERE CONCAT_WS(' ','id','date_of_entry','region','province_of_provider') LIKE '%".$valueToSearch."%'";

仍然相同,我的搜索结果是一个空表,但只有表列标题。

1 个答案:

答案 0 :(得分:1)

替换此部分:

$query = "SELECT * FROM `details` WHERE CONCAT('id','date_of_entry','region','province_of_provider') LIKE '%".$valueToSearch."%'";

与此:

$query = "SELECT * FROM `details` WHERE CONCAT(`id`, `date_of_entry`, `region`, `province_of_provider`) LIKE '%".$valueToSearch."%'";