gpt4 book ai didi

php - 我正在尝试使用不同的输入从我的数据库中搜索数据并使所有内容保持在 session 中

转载 作者:行者123 更新时间:2023-11-29 16:01:36 24 4
gpt4 key购买 nike

我正在尝试使用不同的输入字段在 php mysql 中搜索数据,但我的代码似乎是正确的,但每当我尝试在页面内切换时,它不会停留在 session 中...

    <form method="POST" action="search_all_properties">
<div class="form-row align-items-center">
<div class="col-auto my-1">
<select class="custom-select mr-sm-2" id="inlineFormCustomSelect" name="category" value="category" >
<option selected>Category</option>
<option value="houses">Houses</option>
<option value="shops">Shops</option>
<option value="office_spaces">Office Spaces</option>
</select>
</div>
<div class="col-auto my-1">
<select class="custom-select mr-sm-2" id="inlineFormCustomSelect" name="bedroom">
<option selected>Bedrooms</option>
<option value="1_bedroom">1 Bedroom</option>
<option value="2_bedroom">2 Bedrooms</option>
<option value="3_bedroom">3 Bedrooms</option>
</select>
</div>
<div class="col-auto my-1">
<select class="custom-select mr-sm-2" id="inlineFormCustomSelect" name="price">
<option selected>Price</option>
<option value="200,000">200,000</option>
<option value="300,000">300,000</option>
<option value="500,000">500,000</option>
<option value="800,000">800,000</option>
<option value="1_million">1 Million</option>
</select>
</div>
<div class="col">
<input type="text" onchange="trim(this)" name="location" class="form-control" placeholder="Location">
</div>
<div class="col-auto my-1">
<button type="submit" name="submit" class="btn bg-info">Search</button>
</div>
</div>
</form>

我总是收到一些值未定义的错误...

$bedroom  = $database->escape_value($bedroom);
$price = $database->escape_value($price);
$location = $database->escape_value($location);

每次我尝试分页到下一页时,上述变量都不会被定义... session 是为了一旦我尝试移动到下一页,变量就会被重置。

<?php
if(isset($_POST['submit']) || isset($_SESSION['search'])){
// 1. the current page number ($current_page)
$page = !empty($_GET['page']) ? (int)$_GET['page'] : 1;
// 2. records per page ($per_page)
$per_page = 2;
// 3. total record count ($total_count)
if(isset($_POST['submit'])) {
$_SESSION['category'] = $_POST['category'];
$category = $_POST['category'];
$_SESSION['bedroom'] = $_POST['bedroom'];
$bedroom = $_POST['bedroom'];
$_SESSION['price'] = $_POST['price'];
$price = $_POST['price'];
$_SESSION['location'] = $_POST['location'];
$location = $_POST['location'];
} else { if(isset($_SESSION['category'])){
$category = $_SESSION['category'];
}else if(isset($_SESSION['bedroom'])){
$bedroom = $_SESSION['bedroom'];
}else if(isset($_SESSION['price'])){
$price = $_SESSION['price'];
}else if(isset($_SESSION['location'])){
$location = $_SESSION['location'];
}
}
$category = $database->escape_value($category);
$bedroom = $database->escape_value($bedroom);
$price = $database->escape_value($price);
$location = $database->escape_value($location);

$sql = "SELECT COUNT(*) FROM pictures WHERE title LIKE '%$category%'";
$sql .= " OR location LIKE '%$location%'";
$sql .= " OR price LIKE '%$price%'";
$sql .= " OR description LIKE '%$category%'";
$sql .= " OR bedroom LIKE '%$bedroom%'";
$sql = $database->query($sql);
$sql = $database->fetch_array($sql);
$total_count = array_shift($sql);

// Find all photos
// use pagination instead
$pagination = new Pagination($page, $per_page, $total_count);
// Instead of finding all records, just find the records

$sql = "SELECT * FROM pictures WHERE title LIKE '%$category%'";
$sql .= " OR location LIKE '%$location%'";
$sql .= " OR price LIKE '%$price%'";
$sql .= " OR description LIKE '%$category%'";
$sql .= " OR bedroom LIKE '%$bedroom%'";
$sql .= " ORDER by id DESC";
$sql .= " LIMIT {$per_page}";
$sql .= " OFFSET {$pagination->offset()}";
$photos = Picture::find_by_sql($sql);
$tcount = count($photos);
}
?>

最佳答案

检查 session 变量内容的条件不会按您的预期工作。

您使用的是 else if,这意味着 if(isset($_SESSION['category'])) 为 true,else if 的其余部分 不会被执行。

session 仍会保存,但仅到达category(因为else)

将代码更改为:

if(isset($_POST['submit'])) {
// your code
}
else
{
if(isset($_SESSION['category'])) {
$category = $_SESSION['category'];
}
if(isset($_SESSION['bedroom'])){
$bedroom = $_SESSION['bedroom'];
}
if(isset($_SESSION['price'])){
$price = $_SESSION['price'];
}
if(isset($_SESSION['location'])){
$location = $_SESSION['location'];
}
}

关于php - 我正在尝试使用不同的输入从我的数据库中搜索数据并使所有内容保持在 session 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56151224/

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