gpt4 book ai didi

PHP传递mysql查询结果显示在另一个页面

转载 作者:行者123 更新时间:2023-11-29 02:49:53 25 4
gpt4 key购买 nike

抱歉,如果这是一个业余问题,我的 PHP 技能仍在发展中。我目前已经使用当前代码创建了一个搜索框。

<?php    
// Tyre search setup
$profileSql = "SELECT DISTINCT `profile` FROM `tyres` WHERE `profile` > 0 ORDER BY `profile`";
$profileResult = $db->query($profileSql);

$widthSql = "SELECT DISTINCT `width` FROM `tyres` WHERE `width` > 0 ORDER BY `width`";
$widthResult = $db->query($widthSql);

$diamSql = "SELECT DISTINCT `diam` FROM `tyres` WHERE `diam` > 0 ORDER BY `diam`";
$diamResult = $db->query($diamSql);

if(isset($_GET['profile']) && isset($_GET['width']) && isset($_GET['diam'])) {
$profile = $_GET['profile'];
$width = $_GET['width'];
$diam = $_GET['diam'];

$tyreSql = "SELECT * FROM `tyres` WHERE `profile` = " . $profile . " AND `width` = " . $width . " AND `diam` = " . $diam . " AND rrp > 0 ORDER BY `profile`";
$tyreResult = $db->query($tyreSql);
}
?>

这是此逻辑关联的形式。

<form id="formTyreSearch" name="tyreSearch" method="GET">
<h2>Tyre Search</h2>
<p>Use our search below to find the tyre for your car.</p>
<div class="form-group">Tyre Profile:
<select name = "diamSearch">
<?php while($row=mysqli_fetch_assoc($profileResult)) : ?>
<option value="<?php echo $row['profile']; ?>"><?php echo $row['profile']; ?></option>
<?php endwhile; ?>
</select>
</div>

<div class="form-group">Tyre Width:
<select name = "pcdSearch">
<?php while($row=mysqli_fetch_assoc($widthResult)) : ?>
<option value="<?php echo $row['width']; ?>"><?php echo $row['width']; ?></option>
<?php endwhile; ?>
</select>
</div>

<div class="form-group">Tyre Diam:
<select name = "pcdSearch">
<?php while($row=mysqli_fetch_assoc($diamResult)) : ?>
<option value="<?php echo $row['diam']; ?>"><?php echo $row['diam']; ?></option>
<?php endwhile; ?>
</select>
</div>

<button type="submit" name="filterOptions" value="displayManu" class="btn btn-default btn-sm btn-primary"><i class="fa fa-pencil"></i> Search Now</button>
</form>

现在,我假设在表单中选择的值已设置在 $tyreResult 变量中。

我想将此结果传递到 tyres.php 页面并在那里显示搜索结果,执行此操作的最佳做​​法是什么?如果用户没有选择所有三个值我应该如何处理,因为网站上会有其他内容我真的不想重新加载页面并在顶部显示错误...

最佳答案

只需添加一个 action属性到您的表单并将结果逻辑移动到 tyres.php .如果他们没有选择所有值,请将他们重定向回上一页。但这不应该发生,除非用户恶作剧地更改了您的 HTML。默认情况下,您的第一个选项 <select>标签将被选中。

search.php

<?php    
// Tyre search setup
$profileSql = "SELECT DISTINCT `profile` FROM `tyres` WHERE `profile` > 0 ORDER BY `profile`";
$profileResult = $db->query($profileSql);

$widthSql = "SELECT DISTINCT `width` FROM `tyres` WHERE `width` > 0 ORDER BY `width`";
$widthResult = $db->query($widthSql);

$diamSql = "SELECT DISTINCT `diam` FROM `tyres` WHERE `diam` > 0 ORDER BY `diam`";
$diamResult = $db->query($diamSql);
?>

<form id="formTyreSearch" method="get" action="tyres.php">
<h2>Tyre Search</h2>
<p>Use our search below to find the tyre for your car.</p>
<div class="form-group">
Tyre Profile:
<select name="profile">
<?php while ($row = mysqli_fetch_assoc($profileResult)) : ?>
<option value="<?php echo $row['profile'] ?>"><?php echo $row['profile'] ?></option>
<?php endwhile ?>
</select>
</div>
<div class="form-group">
Tyre Width:
<select name="width">
<?php while ($row = mysqli_fetch_assoc($widthResult)) : ?>
<option value="<?php echo $row['width'] ?>"><?php echo $row['width'] ?></option>
<?php endwhile ?>
</select>
</div>
<div class="form-group">
Tyre Diam:
<select name="diam">
<?php while ($row = mysqli_fetch_assoc($diamResult)) : ?>
<option value="<?php echo $row['diam'] ?>"><?php echo $row['diam'] ?></option>
<?php endwhile ?>
</select>
</div>
<button type="submit" name="filterOptions" value="displayManu" class="btn btn-default btn-sm btn-primary">
<i class="fa fa-pencil"></i> Search Now
</button>
</form>

轮胎.php

<?php
if(isset($_GET['profile'], $_GET['width'], $_GET['diam'])) {
$profile = $_GET['profile'];
$width = $_GET['width'];
$diam = $_GET['diam'];

$tyreSql = "SELECT * FROM `tyres` WHERE `profile` = " . $profile . " AND `width` = " . $width . " AND `diam` = " . $diam . " AND rrp > 0 ORDER BY `profile`";
$tyreResult = $db->query($tyreSql);
} else {
header('Location: search.php');
exit;
}
?>

<!-- show the results -->

关于PHP传递mysql查询结果显示在另一个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37199148/

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