如何从PHP中的一个数据库的两个不同表中获取多个数据

时间:2018-07-13 15:54:37

标签: php mysql database html5

我现在正在创建一个购物网站,我有一个表调用产品,其中存储了所有产品数据,我有一个不同的表购物车,其中存储了添加到购物车中的所有产品,因此基本上在购物车中我已经存储了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>&#x20b9; <?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> &nbsp; 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> &nbsp; No Data Found ...
            </div>
        </div>
        <?php
	}
	
?>		

3 个答案:

答案 0 :(得分:0)

您可以使用内部联接使用单个查询

//https://en.cppreference.com/w/cpp/string/basic_string
std::string txtvar {"This is for Testing Purpose line"};
//https://en.cppreference.com/w/cpp/string/basic_string/find
auto index {txtvar.find("Purpose")};
std::string t{"XXXXXXX"};
txtvar.replace(index, 7, t);
std::cout << txtvar << std::endl;

答案 1 :(得分:0)

您应该对JOIN语句使用另一个且只有一个查询。

SELECT * FROM cart c JOIN products p ON p.productid = c.productid WHERE c.userid = :uid

您将收到的数据将包含购物车中的产品数据。

答案 2 :(得分:0)

我将添加这项技术,它与我使用的技术更接近,没有其他人完全一样。

select  *
from    cart c,
        products p
where   p.product_id = c.product_id
and     c.userid = :uid