PHP传递mysql查询结果以显示在另一个页面上
抱歉,如果这是一个业余问题,我的PHP技能仍在发展中。我已经用当前代码创建了一个搜索框PHP传递mysql查询结果以显示在另一个页面上,php,mysql,Php,Mysql,抱歉,如果这是一个业余问题,我的PHP技能仍在发展中。我已经用当前代码创建了一个搜索框 <?php // Tyre search setup $profileSql = "SELECT DISTINCT `profile` FROM `tyres` WHERE `profile` > 0 ORDER BY `profile`"; $profileResult = $db->query($profileSql);
<?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变量中设置的
我想将这个结果传递到一个tires.php页面,并在那里显示搜索结果,这样做的最佳实践是什么?如果用户没有选择所有三个值,我该如何处理,因为网站上还有其他内容,我真的不想重新加载页面并在顶部显示错误…只需在表单中添加一个
操作
属性,并将结果逻辑移到tires.php
。如果尚未选择所有值,请将其重定向回上一页。但这不应该发生,除非用户恶意修改你的HTML。默认情况下,将选择
标记的第一个选项
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
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 -->
轮胎搜查
使用下面的搜索查找您汽车的轮胎
轮胎外形:
轮胎宽度:
轮胎直径:
现在搜索
tires.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
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 -->
我建议将结果转换为JSON并使用POST将数据发送到其他页面。你不需要表格。只需获取JSON并在结果对象中再次转换即可。检查此项以了解如何在PHP中管理JSON数据。然后检查curl,将JSON数据发布到另一个PHP页面。嘿,Mikey,似乎没有传递值,只是在标题中有tires.PHP?tyreSearch=。也不能使用标题('Location:'),因为我收到了以下错误警告:无法修改标题信息-标题已经由第55行的\tyres.php中的\tyres.php发送(输出开始于includes\quickModalTyres.inc.php:127),55是标题,127行是奇怪的。我希望tires.php?pcdSearch=
基于您的原始代码。您需要为您的
标记指定唯一的名称,如我修订的HTML代码所示。就警告而言,这是因为您在代码中较早地开始了输出缓冲。查看此链接以了解有关如何修复此警告的更多信息。嘿,Mikey,谢谢我进行了另一次查看并对其进行了更改,我可能会在以后的某个日期将其编辑为用例,这样任何一个参数都可以单独使用,但这是另一天的事情。我会看看那篇文章,模态已经给了我一段时间的问题(ob_get_clean就是在这里被称为ob_clean的)。