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

时间:2016-05-12 23:43:32

标签: php mysql

道歉,如果这是一个业余问题,我的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页面并在那里显示搜索结果,最佳做法是什么?如果用户没有选择所有三个值,我应该如何处理,因为网站上将有其他内容我真的不想重新加载页面并在顶部显示错误...

1 个答案:

答案 0 :(得分:1)

只需在表单中添加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>

<强> tyres.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 -->