php - 购物车功能、更新和清除字段

标签 php javascript mysql html e-commerce

我创建了一个非常基本的电子商务应用程序,这是购物车的完整代码。我在获取更新购物车的功能时遇到问题,(例如,我将 1 个产品添加到购物车中,然后进入购物车并希望将订购数量更改为 10,代码目前不允许我)并且要清除字段,我有代码,但它什么也没做。谢谢

  <?
        include("includes/db.php");
        include("includes/functions.php");

        if($_REQUEST['command']=='delete' && $_REQUEST['pid']>0){
            remove_product($_REQUEST['pid']);
        }
        else if($_REQUEST['command']=='clear'){
            unset($_SESSION['cart']);
        }
        else if($_REQUEST['command']=='update'){
            $max=count($_SESSION['cart']);
            for($i=0;$i<$max;$i++){
            $pid=$_SESSION['cart'][$i]['productid'];
            $q=intval($_REQUEST['product'.$pid]);
            if($q>0 && $q<=999){
                $_SESSION['cart'][$i]['qty']=$q;
            }
            else{
                $msg='Some proudcts not updated!, quantity must be a number between 1 and 999';
            }
        }
    }

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Shopping Cart</title>

<script language="javascript">
    function del(pid){
        if(confirm('Do you really mean to delete this item?')){
            document.form1.pid.value=pid;
            document.form1.command.value='delete';
            document.form1.submit();
        }
    }
    function clear_cart(){
        if(confirm('This will empty your shopping cart, continue?')){
            document.form1.command.value='clear';
            document.form1.submit();
        }
    }
    function update_cart(){
        document.form1.command.value='update';
        document.form1.submit();
    }
<div id="login">
<?php
        session_start();
        // dBase file
        include "dbConfig.php";

        if ($_GET["op"] == "login")
  {
  if (!$_POST["username"] || !$_POST["password"])
        {
        die("You need to provide a username and password.");
        }

  // Create query
  $q = "SELECT * FROM `dbUsers` "
        ."WHERE `username`='".$_POST["username"]."' "
        ."AND `password`=PASSWORD('".$_POST["password"]."') "
        ."LIMIT 1";
  // Run mysql query
  $r = mysql_query($q);

  if ( $obj = @mysql_fetch_object($r) )
        {
        // Login good, create session variables
        $_SESSION["valid_id"] = $obj->id;
        $_SESSION["valid_user"] = $_POST["username"];
        $_SESSION["valid_time"] = time();

        // Redirect to member page
        Header("Location: index.php?p=members");
        }
  else
        {
        // Login not successful
        die("Sorry, could not log you in. Wrong login information.");
        }
  }
        else
  {
//If all went right the Web form appears and users can log in
  echo "<form action=\"?p=login&op=login\" method=\"POST\">";
  echo "Username: <input name=\"username\" size=\"20\"><br />";
  echo "Password: <input type=\"password\" name=\"password\" size=\"20\"><br />";
  echo "<input type=\"submit\" value=\"Login\">";
  echo "</form>";
  }

        ?>

        <li><a href="index.php?p=register">Register</a></li></div>

        </ul>
</div>

</script>
</head>

<body>
<form name="form1" method="post">
<input type="hidden" name="pid" />
<input type="hidden" name="command" />
    <div style="margin:0px auto; width:550px;" >
    <div style="padding-bottom:10px">
    <div style="padding-top:50px">

        <h1 align="center">Shopping Basket</h1>
    <input type="button" value="Continue Shopping" onclick="window.location='index.php?p=products'" />
    </div>
        <div style="color:#F00"><?=$msg?></div>
        <table border="0" cellpadding="5px" cellspacing="1px" style="font-family:Verdana, Geneva, sans-serif; font-size:12px; background-color:#E1E1E1" width="100%">
        <?
            if(is_array($_SESSION['cart'])){
                echo '<tr bgcolor="#FFFFFF" style="font-weight:bold"><td>Serial</td><td>Name</td><td>Price</td><td>Qty</td><td>Amount</td><td>Options</td></tr>';
                $max=count($_SESSION['cart']);
                for($i=0;$i<$max;$i++){
                    $pid=$_SESSION['cart'][$i]['productid'];
                    $q=$_SESSION['cart'][$i]['qty'];
                    $pname=get_product_name($pid);
                    if($q==0) continue;
            ?>
                    <tr bgcolor="#FFFFFF"><td><?=$i+1?></td><td><?=$pname?></td>
                    <td>&pound; <?=get_price($pid)?></td>
                    <td><input type="text" name="product<?=$pid?>" value="<?=$q?>" maxlength="3" size="2" /></td>                    
                    <td>&pound; <?=get_price($pid)*$q?></td>
                    <td><a href="javascript:del(<?=$pid?>)">Remove</a></td></tr>
            <?                  
                }
            ?>
            <tr><td><b>Order Total + Shipping: &pound<?=get_order_total() +5.00?></b></td><td colspan="5" align="right"><input type="button" value="Clear Cart" onclick="clear_cart()"><input type="button" value="Update Cart" onclick="update_cart()"><input type="button" value="Place Order" onclick="window.location='index.php?p=billing'"></td></tr>
            <?
            }
            else{
                echo "<tr bgColor='#FFFFFF'><td>There are no items in your shopping cart!</td>";
            }
        ?>
        </table>
    </div>
</form>
</body>
</html>

最佳答案

我非常确定,如果您想使用 $_SESSION 变量,则需要将 session_start() 移至代码顶部。

关于php - 购物车功能、更新和清除字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9889844/

相关文章:

java - FileUtils.write写入速度

javascript - 3 通过Ajax和PHP实现依赖下拉

javascript - 根据用户输入在 javascript 中搜索数组

php - 多语言网站问题

javascript - 选择超链接内的输入跨浏览器问题

javascript - 如何在 HTML 选项中换行文本

mysql - 选择值变化的行

PHP:每当调用子类中的函数时调用函数

PHP、MySQL : can't explain this undefined index error

php - 基于数量计算的产品类别的购物车折扣