[英]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.