在html表PHP中显示mysql数据 [英] Displaying mysql data in html table PHP

查看:89
本文介绍了在html表PHP中显示mysql数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在HTML表格中显示MySQL表格中的数据。
我已经将代码显示在它将显示MySQL表中七行中的第一行的位置,但它重复该行九次,而不是显示所有七行。
我做错了什么?

I have to make data from a MySQL table display in an HTML table. I've got the code to where it will display the first of seven rows from the MySQL table, but it is repeating the row nine times, instead of displaying all seven rows. What have I done wrong?

<?php
require 'database.php';

//get all product data
$query = 'SELECT * FROM products';
          $products = $db->query($query);
          $products = $products->fetch();
?>

    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>SportsPro</title>
    </head>

    <body>
    <form id="Header" name="Header" method="post">
    <?php include 'header.php'; ?>
    </form>
    <form id="Content" name="Content" method="post">
    <table width="500" border="1">
    <tr>
    <th scope="col">Code</th>
    <th scope="col">Name</th>
    <th scope="col">Version</th>
    <th scope="col">Release Date</th>
    <th scope="col">&nbsp;</th>
    </tr>
    <?php foreach ($products as $product) { ?>
    <tr>
    <td><?php echo $products['productCode']; ?></td>
    <td><?php echo $products['name']; ?></td>
    <td><?php echo $products['version']; ?></td>
    <td><?php echo $products['releaseDate']; ?></td>
    <td><input type = "submit" value = "Delete" align = "right" ></td>

    </tr> 
    <?php } ?>
    </table>

    <p><a href="add_product.php">Add Product</a></p>
    </form>
    <form id="Footer" name="Footer" method="post">
    <?php include 'footer.php'; ?>
    </form>
    </body>
    </html>


推荐答案

为什么使用$ products?

Why are you using $products?

FIX:

<?php 
foreach ($products as $product) {  //See *$products as $product*, so no need to use $products below instead use $product
    echo "<tr>";
    echo "<td>". $product['productCode']. "</td>";
    echo "<td>". $product['name']. "</td>";
    echo "<td>". $product['version']. "</td>";
    echo "<td>". $product['releaseDate']. "</td>";
    echo "<td><input type = \"submit\" value = \"Delete\" align = \"right\" ></td>";
    echo "</tr>" 
} 
?>

这篇关于在html表PHP中显示mysql数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆