[英]how to get multiple data from two different table from one database in php
我現在正在創建購物網站,我有一個表調用產品,其中存儲了所有產品數據,並且我有不同的表購物車,其中所有添加到購物車的產品都存儲在其中,因此基本上在購物車中,我已經存儲了productid,userid在名為checkout的頁面上,我要檢索數據,其中購物車表中的數據將采用特定登錄用戶的productid,表產品中的數據將顯示添加到購物車中的所有產品,因此基本上從購物車表中我們將采用添加的產品從餐桌產品到購物車,我們將向他們展示。 現在的問題是我已經檢索了數據,但它僅顯示一項而不是多項。 提前致謝
這是我的代碼:
<?php $userid = $_SESSION['userSession']; require_once 'dbconfig.php'; $stmt = $DB_con->prepare('SELECT cartid,userid,productid FROM cart WHERE userid =:uid'); $stmt->execute(array(':uid'=>$userid)); if($stmt->rowCount() > 0) { while($rows=$stmt->fetch(PDO::FETCH_ASSOC)) { extract($rows); { } ?> <?php $productid = $rows['productid']; require_once 'dbconfig.php'; $stmt = $DB_con->prepare('SELECT productid,productname,productcategory,producttype,productprice,productimage1 FROM products WHERE productid = :uid'); $stmt->execute(array(':uid'=>$productid)); if($stmt->rowCount() > 0) { while($row=$stmt->fetch(PDO::FETCH_ASSOC)) { extract($row); { } ?> <div class="bs-example4" data-example-id="simple-responsive-table"> <div class="table-responsive"> <table class="table-heading simpleCart_shelfItem"> <tr> <th class="table-grid">Item</th> <th>Prices<h3></h3></th> <th >Delivery </th> <th>Subtotal</th> </tr> <tr class="cart-header1"> <td class="ring-in"><a href="single.html" class="at-in"><img src="thumb/<?php echo $row['productimage1']; ?>" class="img-responsive" alt=""></a> <div class="sed"> <h5><a href="single.html"><?php echo $row['productname']; ?></a></h5> </div> <div class="clearfix"> </div> <div class="close2"> </div></td> <td>₹ <?php echo $row['productprice']; ?></td> <td>FREE SHIPPING</td> <td class="item_price">$100.00</td> <td class="add-check"><a class="item_add hvr-skew-backward" href="#">Add To Cart</a></td> </tr> </table> </div> </div> <div class="produced"> <a href="single.html" class="hvr-skew-backward">Produced To Buy</a> </div> </div> </div> <?php } } else { ?> <div class="col-xs-12"> <div class="alert alert-warning"> <span class="glyphicon glyphicon-info-sign"></span> No Data Found ... </div> </div> <?php } ?> <?php } } else { ?> <div class="col-xs-12"> <div class="alert alert-warning"> <span class="glyphicon glyphicon-info-sign"></span> No Data Found ... </div> </div> <?php } ?>
您可以使用內部聯接使用單個查詢
SELECT a.cartid
, a.userid
, a.productid
, b.productname
, b.productcategory
, b.producttype
, b.productprice
, b.productimage1
FROM cart
INNER JOIN products ON a.productid = b.productid
where a.productid= :uid
您應該對JOIN語句使用另一個且只有一個查詢。
SELECT * FROM cart c JOIN products p ON p.productid = c.productid WHERE c.userid = :uid
您將收到的數據將包含購物車中的產品數據。
我將添加這項技術,它與我使用的技術更接近,沒有其他人做得完全相同。
select *
from cart c,
products p
where p.product_id = c.product_id
and c.userid = :uid
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.