2012-06-18 62 views
2

*解决*SimpleCart(JS)保存到数据库的登录会话

我实现SimpleCart(JS)我的一个项目。

一个人可以registerlogin。 一旦logged您可以使用购物车purchase items

我有一个问题,因为simplecart(JS)保存推车localStorage的,如果你login与其他一个帐户。您看到相同的购物车结果。

有没有人有这样的PHP SQL解决方案,到车结果存储到SQL每个individual session用户?

我解决了这个问题,这个, 这是一个基本的技术,但它完全适用于我需要什么,我还是会捏捏PHP来检查,如果在数据库中存在车项目,是否更新或创建。

的jQuery

$(".saveCart").click(function(){ 
     $(".itemRow").each(function(){ 
      var custId = "<?php echo $_SESSION['Username'] ?>"; 
      var itemName = $(this).find(".item-name").text(); 
      var itemPrice = $(this).find(".item-price").text(); 
      var itemQty = $(this).find(".item-quantity").text(); 
      var itemTotal = $(this).find(".item-total").text(); 

      $.ajax({ 
       // Enter below the full path to your "cart" php file 
       url: "cart.php", 
       type: "POST", 
       data: {custId: custId, itemName: itemName, itemPrice: itemPrice, itemQty: itemQty, itemTotal: itemTotal}, 
       cache: false, 
       success: function (html) { 
        // If form submission is successful 
        if (html == 1) { 
        } 
        // Double check if maths question is correct 
        else { 
        } 
       } 
      });    

     }); 
    }); 

PHP

<?php 

$con = mysql_connect("localhost","root","pass"); 
if (!$con) 
    { 
    die('Could not connect: ' . mysql_error()); 
    } 

mysql_select_db("test", $con); 

$sql="INSERT INTO cart (Id, itemName, itemPrice, itemQty, itemTotal) 
VALUES 
('$_POST[custId]','$_POST[itemName]','$_POST[itemPrice]','$_POST[itemQty]','$_POST[itemTotal]')"; 

if (!mysql_query($sql,$con)) 
    { 
    die('Error: ' . mysql_error()); 
    } 

if ($sql) { echo "1"; } 
else {echo "2"; } 

mysql_close($con); 
?> 

回答

1
mysql_query("UPDATE table SET sessid = ".session_id()." WHERE user = ".$user_id); 

类似的东西?

+0

类似的东西,是的,但我怎么得到购物车的内容? simpleCart会自动解析它的内容吗? – Christopher

+0

@Christopher我很抱歉,我没有simpleCart的经验。希望别人能回答。 – 2012-06-18 07:31:28

+0

酷,我现在使用一种很好的技术来实现这一点,但我不能张贴到我自己的答案。 – Christopher

相关问题