php - 通过 php 显示数据时生成额外的空行

标签 php html css mysql

我不知道为什么这段代码在最后生成了一个额外的行,它只发生在我使用 PHP 显示我的数据时。表中没有多余的行。额外的行显示空数据。

 <div class="tbl-header">
<table cellpadding="0" cellspacing="0" border="0">
    <thead>
        <tr>
            <th>ID</th>
            <th>Product Name</th>
            <th>Brand</th>
            <th>Category Name</th>
            <th>Description</th>
            <th>Picture</th>
            <th>Quantity</th>
            <th>Price</th>
        </tr>
        <?php
            $data = mysqli_query($con, "select `shopifyv2`.`products`.`ID` AS `ID`,`shopifyv2`.`products`.`ProductName` AS `ProductName`,`shopifyv2`.`brands`.`BrandName` AS `BrandName`,`shopifyv2`.`category`.`Name` AS `CategoryName`,`shopifyv2`.`products`.`Description` AS `Description`,`shopifyv2`.`products`.`Picture` AS `Picture`,`shopifyv2`.`products`.`Quantity` AS `Quantity`,`shopifyv2`.`products`.`Price` AS `Price` from `products` join `shopifyv2`.`brands` on `products`.`BrandID` = `brands`.`ID` join `shopifyv2`.`category` on `products`.`CateID` = `category`.`ID`");
            $row = mysqli_num_rows($data);

            for ($i = 0; $i < $row; $i++) {
                $row = mysqli_fetch_array($data);
                echo "<tr>";
                echo "<td>$row[0]</td><td>$row[1]</td><td>$row[2]</td><td>$row[3]</td><td>$row[4]</td><td><img height='30' width='20' src='../AdminPanel/Pictures/$row[5]'/ /></td><td>$row[6]</td><td>$row[7]</td>";
                echo "<td><a href='ModifyProduct.php?ID=$row[0]' class='btn btn-info'>Modify</a></td>";
                echo "<td><a href='ShowProduct.php?id=$row[0]'>Delete</a></td>";
                echo "</tr>";
            }
            if (isset($_GET['id'])) {
                $delid = $_GET['id'];
                mysqli_query($con, "delete from products where id = $delid");
                header("location: ShowProduct.php");
            }
        ?>
    </thead>
</table>

最佳答案

您可以检查该行是否不为空:

$row = mysqli_num_rows($data);

for ($i = 0; $i < $row; $i++) {
   $row = mysqli_fetch_array($data);
   if(!$row[0]) continue; // <- HERE

关于php - 通过 php 显示数据时生成额外的空行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51340055/

相关文章:

javascript - HTML5 视频 : canPlayType() is not working in safari

javascript - 使用 javascript 检测 html5 验证

javascript - 输入类型 - 后退按钮?

html - 避免滚动到 Canvas 外的内容

html - 如何为移动设备和不同的媒体查询定位两个图像

javascript - 没有空格的CSS文本突出显示

PHP 无限 while 循环阻止对脚本的其他调用

php - 搜索 Ignited 数据表 CodeIgniter

PHP session 变量未传递到下一页

php - 是否有一本涉及真实世界编程的PHP和MySQL书籍?