PHP单击链接时将数据库中的数据显示到表中

时间:2017-05-07 15:12:00

标签: php html mysql database html-table

刚开始,我是php的新手,所以我可能错过了一些明显的东西,所以请耐心等待。

我有超链接(飞机,轮船等),当我点击超链接"飞机"我希望所有飞机记录都显示在表格中。当我点击不同的车辆时,我希望它用新数据刷新表格。

问题是,当我点击链接" train"它不刷新表并显示相关数据,它保留相同的数据。当我点击链接"飞机"我想显示该productLine的所有记录。

感谢您的帮助

As you can see in the pic, i clicked train but it still displays vintage cars

这是我的代码:

<?php
require_once('dbconfig.php');

//get productLine
if (!isset($productLine)) {
    $productLine = filter_input(INPUT_GET, 'productLine', FILTER_VALIDATE_INT);
    if ($productLine == null || $productLine == FALSE) {
        $productLine = 'Trains';
    }
}

//get all product lines
$query = 'SELECT * FROM productlines';
$statement = $db->prepare($query);
$statement->execute();
$productLines = $statement->fetchAll();
$statement->closeCursor();

//Get products for product line
$queryProducts = 'SELECT * FROM products WHERE productLine = :productLine ORDER BY productCode';
$statement1 = $db->prepare($queryProducts);
$statement1->bindValue(':productLine', $productLine);
$statement1->execute();
$products = $statement1->fetchAll();
$statement1->closeCursor();

?>

<!DOCTYPE html>
<html>
<head>
    <title>Classic Models Online</title>
    <link rel="stylesheet" type="text/css" href="main.css" />
</head>
<body>
<header><h1>ClassicModels Online</h1>
<p>Classic models for all automobile enthusiasts</p>
</header>
<main>
    <h1>Classic Models Product List</h1>

    <aside>
        <!--Display list of product lines-->
        <h2>Product Lines</h2>
        <nav>
        <ul>
            <?php foreach ($productLines as $productLine) : ?>
            <li>
                <a href=".?productLine=<?php echo $productLine['productLine']; ?>">
                    <?php echo $productLine['productLine']; ?>
                </a>
            </li>
            <?php endforeach; ?>
        </ul>
        </nav>
    </aside>

    <br>
    <section>
        <!--Display a table of products for product line-->
        <h2><?php echo $productLine['productLine']; ?></h2>
        <table>
            <tr>
                <th>Code</th>
                <th>Name</th>
                <th>Scale</th>
                <th>Price</th>
                <th>Total Sold</th>
                <th>&nbsp;</th>
            </tr>

            <?php foreach ($products as $product) :?>
            <tr>
                <td> <?php echo $product['productCode']; ?> </td>
                <td> <?php echo $product['productName']; ?> </td>
                <td> <?php echo $product['productScale']; ?> </td>
                <td> <?php echo $product['MSRP']; ?> </td>
                <td> <?php echo $product['quantityInStock']; ?> </td> 
                <td> <form action="update_product.php" method="post">
                    <input type="hidden" name="productCode" value="<?php echo $product['productCode']; ?>">
                    <input type ="hidden" name="productLine" value="<?php echo $product['productLine']; ?>">
                    <input type="submit" value="Update">
                </form> </td>
            </tr>
            <?php endforeach; ?>
        </table>
        <p><a href="add_product_form.php">Add Product</a></p>
    </section>  
</main>
    <footer>
        <p>&copy; <?php echo date("Y"); ?> Classic Models Online.</p>
    </footer>
    </body>
    </html>

1 个答案:

答案 0 :(得分:0)

您需要从网址中的查询字符串中获取productLine变量。你需要添加像

这样的东西
if(isset($_GET['productLine'])){
    $productLine = $_GET['productLine'];
}