2017-04-24 65 views
1

我一直在制作一个小型购物车。我试图显示购物车 价值,并有总计。我已成功显示购物车产品,但我无法显示总计。它一直在显示错误'资源ID#5'。请在下面查看我的代码。如何获得购物车中相同用户ID的两个值的总和

viewcart.php

<?php 

include('config.php'); 

session_start(); 

echo $session=$_SESSION['user_email']; 

echo $query=mysql_query("SELECT product_price, SUM(product_price) FROM cart where user_email='$session' GROUP BY user_email"); 


$total = mysql_fetch_array($query); 



$query1=mysql_query("select * from cart where user_email='$session'"); 
    $num_rows = mysql_num_rows($query1); 


    if($num_rows>0){ 

    echo "<center>"; 

    echo "<table style='width:80%' border=5px><tr><th>Product Name</th> 
<th>Product Details</th><th>Product Price</th></tr>"; 


    while($query2=mysql_fetch_array($query1)) 
    //if($query2>0){ 
    { 


    echo "<tr><td>".$query2['product_name']."</td>"; 

    echo "<td>".$query2['product_details']."</td>"; 

    echo "<td>".$query2['product_price']."</td>"; 

    echo "<td><a href='remove.php?id=".$query2['id']."'>Remove Product</a></td>"; 

    echo "</tr>"; 

    } 

    echo "<tr><td>Total:</td><td>".$total['product_price']."</td></tr>"; 


     ?> 

     </table> 


    </center><br/><br/><?php } else { echo "<center>No product available for display!!</center>"; }?> 
+1

1.摆脱的MySQL和使用mysqli的,它弃用 – clearshot66

回答

1
$query = mysql_query("SELECT SUM(product_price) as sum FROM cart where user_email='".mysql_real_escape_string($session)."'"); 

$total = 0; 
if ($result=mysql_fetch_assoc($query)) { 
    $total = $result['sum']; 
} 
+0

谢谢你这么much..this代码工作..thanks再次.. –

相关问题