在MySql数据库中仅显示一种产品后,PHP While循环停止

时间:2019-07-04 09:16:50

标签: php html mysql twitter-bootstrap

此php while循环旨在显示有关存储在MySql数据库中的产品的信息。 while循环应该在引导卡中显示数据库中的整个产品。每个产品的“二手面料”和“裁缝”都存储在数据库的单独表中。这些表用外键连接。为什么仅显示一种产品后循环停止,我该如何解决?

<div class="row" id="result">
<?php 
$query ="SELECT * FROM products, tailor_details";
$result=$dbcon->query($query);
while($row=$result->fetch_assoc()){
?>
    <div class="col-md-3 mb-2">         
        <div class="card-deck">      
            <div class="card border-secondary">     
                <img src="<?= $row['Picture'] ?>" class="card-img-top">
                <div class="card-body">
                    <h6 class="text light bg-info text-center rounded p-1"><?= $row['Name']; ?></h6>
                    <h4 class="card-title text-danger">Price: <?= number_format($row['Price']); ?>/-
                    </h4>
                    <p>Weight: <?= $row['Weight']; ?><br>Tailor: 

<?php 
    $tailor=$row['Tailor'];
    $getTailor="SELECT biz_name 
                FROM tailor_details 
                WHERE user_id=$tailor";
    $res=$dbcon->query($getTailor);
    while($rw = $res->fetch_assoc()){
        echo $row['biz_name'];
     } 
?>  
    <br>
     Fabric Used: 
<?php 
    $fabric=$row['Fabric'];
    $getfabric="SELECT FabricName FROM fabrics WHERE FabricsId=$fabric";
    $result=$dbcon->query($getfabric);
    while($row = $result->fetch_assoc()){
        echo $row['FabricName'];
    } 
?>
   </p>

                    <a href="#" class="btn btn-success btn-block">Customize</a>
                </div>
            </div>
        </div>
    </div>
<?php  
} // endwhile
?>
</div>

“我希望while循环可以在自助卡中显示每个产品及其详细信息,就像在电子商务网站中看到的那样;但是我得到的实际输出只是一种产品,循环就停止了!”

请参见下面的数据库架构:

产品表

enter image description here

面料表

enter image description here

tailor_detals表

enter image description here

Screenshot of output

1 个答案:

答案 0 :(得分:0)

正如许多peolpe在他们的评论中告诉您的那样,您不需要三个查询,只需执行一个查询即可。我已将您的代码更改为仅使用一个查询:

<div class="row" id="result">
<?php 
$query ="SELECT picture,`Name`,price, biz_name,FabricName FROM products a join tailor_details b on a.Tailor=b.user_id join fabrics c on c.FabricsId=a.Fabric";
$result=$dbcon->query($query);
while($row=$result->fetch_assoc()){
    ?>
        <div class="col-md-3 mb-2">         
            <div class="card-deck">      
                <div class="card border-secondary">     
                    <img src="<?= $row['Picture'] ?>" class="card-img-top">
                    <div class="card-body">
                        <h6 class="text light bg-info text-center rounded p-1"><?= $row['Name']; ?></h6>
                        <h4 class="card-title text-danger">Price: <?= number_format($row['Price']); ?>/-
                        </h4>
                        <p>Weight: <?= $row['Weight']; ?><br>Tailor: 

    <?php 
        echo $row['biz_name'];
    ?>  
        <br>
        Fabric Used: 
    <?php 
        echo $row['FabricName'];
    ?>
       </p>

                        <a href="#" class="btn btn-success btn-block">Customize</a>
                    </div>
                </div>
            </div>
        </div>
    <?php  
} // endwhile
?>
</div>