php如果$\u为空,则从数据库中选择所有内容

dy1byipe  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(208)

如果用户没有在下拉菜单中选择任何内容或选择他在那里选择的内容,我将尝试从我的数据库中选择所有内容。所以基本上我想做的是检查$filter是否为空,如果是,则选择所有内容,但不知道如何做到这一点。下面是我的select,如果我从下拉列表中选择某个内容,它会很好地工作,但是在我选择某个内容之前,它会给出一个未定义的索引警告。

<?php
$visibility = 1;
$filter = $_GET['slct'];
$stmt = $conn->prepare("SELECT id, name, description, address, phone, phone2, email, company, job, visibility, filter, confirmed, userid FROM cards WHERE visibility = ? AND filter = ?");
$stmt->bind_param("is", $visibility, $filter);
$stmt->execute();
$result = $stmt->get_result();
while ($record = mysqli_fetch_assoc($result)) {
?>

然后选择:

<select name="slct" id="slct" onchange="form.submit()">
<option value="One">One</option>
<option value="Two">Two</option>
</select>
dw1jzc5e

dw1jzc5e1#

首先获得全球 slct ```
$visibility = 1;
$filter = isset($_GET['slct']) ? $_GET['slct'] : "";

在尝试构造查询之后 `slct` 设置与否

$baseQuery = "SELECT id, name, description, address, phone, phone2, email, company, job, visibility, filter, confirmed, userid FROM cards WHERE visibility = ?" ;
$filterPart = isset($_GET['slct']) ? " AND filter = ?" : "";

准备语句并相应地绑定param并执行

$stmt = $conn->prepare( $baseQuery. $filterPart);
if(isset($_GET['slct'])){
$stmt->bind_param("is", $visibility, $filter);
}else{
$stmt->bind_param("is", $visibility);
}
$stmt->execute();

相关问题