[英]how to get multiple data from two different table from one database in php
I am creating shopping website now i have one table call products in which all the products data is stored and i have different table cart in which all the product which are added to cart are stored so basically in cart I have stored productid, userid so now on page called checkout I want to retrieve data in which the data from cart table we will take productid of particular logged in user and from the table products we will show all the product added to cart so basically from cart table we will take the product added to cart and from table products we will show them . 我现在正在创建购物网站,我有一个表调用产品,其中存储了所有产品数据,并且我有不同的表购物车,其中所有添加到购物车的产品都存储在其中,因此基本上在购物车中,我已经存储了productid,userid在名为checkout的页面上,我要检索数据,其中购物车表中的数据将采用特定登录用户的productid,表产品中的数据将显示添加到购物车中的所有产品,因此基本上从购物车表中我们将采用添加的产品从餐桌产品到购物车,我们将向他们展示。 now the problem is that i have retrieved the data but it only showing one item and not multiple.
现在的问题是我已经检索了数据,但它仅显示一项而不是多项。 Thanks in advance
提前致谢
Here is my code: 这是我的代码:
<?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 } ?>
you could use a single query using a inner join 您可以使用内部联接使用单个查询
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
You should use another and only one query with JOIN statement. 您应该对JOIN语句使用另一个且只有一个查询。
SELECT * FROM cart c JOIN products p ON p.productid = c.productid WHERE c.userid = :uid
The data you will receive will contains products data from cart. 您将收到的数据将包含购物车中的产品数据。
I'll add this technique, it is closer to what I use and no one else did it quite the same. 我将添加这项技术,它与我使用的技术更接近,没有其他人做得完全相同。
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.