如何使链接显示数据库中的特定数据?

时间:2018-03-27 09:48:40

标签: php mysql

我正在为我的最终项目制作一个产品网站,我想制作一个链接,例如(如果我点击网页上的处理器,它将只在另一个页面上显示我的数据库中的处理器列表。当我点击主板,显卡等其他类别时,也应该发生同样的事情。例如,我创建了一个带有方法帖子的按钮,然后在另一页面上我编写了以下代码:

parts.php

    <form action="display.php" method="post">


        <div class="partbox">
            <a href="#">
            <img src="./img/processor.jpg" alt="Go to Processor">
            </a>
            <br>
            <input type="submit" class="btnlink" value="Processor" name="processor"</input>
        </div>

        <div class="partbox">
            <a href="#">
            <img src="./img/motherboard.jpg" alt="Go to Motherboard">
            </a>
            <br>
            <input type="submit" class="btnlink" value="Motherboard" name="motherboard"</input>
        </div>

等...

Display.php的

//When Clicking on Processor

if(isset($_POST['processor'])){

$sql = "SELECT * FROM products WHERE category = 'Processor' ";

$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);

echo "<h1>" . $row["category"] . "</h1>";

while($row = mysqli_fetch_assoc($result)) {
    include('info.php');
    echo $info;
}
}

//When clicking on Motherboard



if(isset($_POST['motherboard'])){

$sql = "SELECT * FROM products WHERE category = 'Motherboard ' ";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);

echo "<h1>" . $row["category"] . "</h1>";

while($row = mysqli_fetch_assoc($result)) {
    include('info.php');
    echo $info;
}
}

等...

好吧,我不知道链接是否可行。或者可以保持这样吗?我担心我会花太多时间认为可能有最简单的方法。

1 个答案:

答案 0 :(得分:-1)

parts.php

    <div class="partbox">
        <a href="display.php?category_name=Processor">
        <img src="./img/processor.jpg" alt="Go to Processor">
        </a>
        <br>
    </div>

    <div class="partbox">
        <a href="display.php?category_name=Motherboard">
        <img src="./img/motherboard.jpg" alt="Go to Motherboard">
        </a>
        <br>
    </div>

Display.php的

// based on the link clicked, $_GET['category_name'] will have the 
//values Processor or Motherboard
if(isset($_GET['category_name'])){

    $sql = "SELECT * FROM products WHERE category = $_GET['category_name'] ";

    $result = mysqli_query($conn, $sql);
    $row = mysqli_fetch_assoc($result);

    echo "<h1>" . $row["category"] . "</h1>";

    while($row = mysqli_fetch_assoc($result)) {
        include('info.php');
        echo $info;
    }
}