产品没有添加到我的购物车

时间:2012-04-23 11:18:57

标签: php html e-commerce shopping-cart

我想知道是否有人可以帮助我。我遇到的问题是产品没有添加到我的购物车中。我想知道是否有人可以帮助我或发现我的代码中的任何错误.. 感谢您提供任何帮助,您可以为大量的代码感到抱歉..

汽车价格(显示我数据库中的所有汽车)

<head>
<title>Car Prices</title>

<!--main.css stylesheet involves the positioning of the website and effects that have been applied to the website-->
<!--slider.css stylsheet involves the positioning the size and the time frame of the slideshow that have been created-->
<!--menu.css stylesheet involves the form of navigation and the styles that have been applied to that horizontal navigation bar-->

<link href="css/main2.css" rel="stylesheet" type="text/css" />
<link href="css/slider.css" rel="stylesheet" type="text/css" />
<link href="css/menu.css" rel="stylesheet" type="text/css" />


</head>

<body>

<span class = "title1">Prestige Supercars Car Price</span>

<div id = "header1">

<!-- as i wanted the button to be wrapped with the text on top i had to wrap the text over the button-->


<?php

require "dbc.php";

$query = "SELECT * from cars";

$result = mysql_query ($query);

$rCount = mysql_num_rows($result);

$aresult = array();

$aresult = mysql_fetch_array($result);

while($row = mysql_fetch_array($result)){

$CID = $row ["id"];

echo'<div id="content7">
     <div class="car-image"><a href="carinformation.php?id='.$row["id"]. '"><img width="500px" height="200px" src="data:image/jpeg;base64,'. base64_encode($row["picture"]). '" />';
echo '<div id="title2">'.$row ["Car_Name"];
echo '<br />Price: &pound'.$row ["Price"];
echo '</div>';
     '<input name="viewdetails" type="submit" value="More" onClick="">';
     }

?>  

汽车信息(指定一辆车)

<?php

$carid = $_GET['id'];
require_once "dbc.php";

$result = mysql_query('SELECT * FROM cars WHERE id = "'.$carid.'"');
$rCount = mysql_num_rows($result);
?>
<?php
if($row = mysql_fetch_array($result)){
do{
     $CID = $row ["id"];
    echo '<div id="content8"><form name="userForm" method="post" action="cart.php?cid='.$row['id'].'">';
    echo '<div class="car-image"><a href="carinformation.php?id='.$row["id"]. '"><img width="500px" height="200px" src="data:image/jpeg;base64,'. base64_encode($row["picture"]). '" />';
    echo '<div id="title2">'.$row ["Car_Name"];
    echo '<br />Quantity: '.$row ["Quantity"];
    echo '<br />Price: &pound'.$row ["Price"];
    echo '<br/><input name="addCart" type="submit" value="ADD TO CART" onClick="">';

    echo '</form></div>';
    echo '<br/><br/><br/><br/><br/>';

    }while($row = mysql_fetch_array($result));
    }else{print"Sorry, no records where found!";}
?>
</body>
</html>

<!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">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<!-- Presige Supercars -->

<head>
<title>Prestige Cars Cart</title>
</head>

<?php

//if product exists in command line, add it to cart
    @ $cid = $_GET['cid'];  
    //checks if command line has product added
    if($cid){
        //checks if cart is empty & creates it
        if(!isset($_SESSION['cart'])){
            $_SESSION['cart'] = array();
            $_SESSION['items'] = 0;
            $_SESSION['total_price'] = '0.00';
        }
        //cart array contains car id & quantity
        if (isset($_SESSION['cart'] [$cid] ))
            $_SESSION['cart'] [$cid]++;
        else 
            $_SESSION['cart'] [$cid] = 1;
    }

    //checks if command line has product updated
    if(isset($_POST['update'])){   
        foreach ($_SESSION['cart'] as $carid => $qty){
            //value from text box name
            if($_POST[$carid]<='0')
                unset($_SESSION['cart'] [$carid]);
            else 
                $_SESSION['cart'] [$carid] = $_POST[$carid];
        }
    }
?>

<?php
session_start();
?>
<?php
    //displays car
    if  ($_SESSION['cart']){
        //echo '<h3>Cart Contents</h3>';
        echo '<div id="content7">';
        echo '<table width="600" border="0" cellpadding=1 cellspacing=3>';
        echo '<form name="userForm" method="post" action="cart.php">';
        echo '<tr><th width="300" scope="col" align="center">Title</th>';
        echo '<th width="100" align="right">Quantity</th>';
        echo '<th width="150" align="right">Price</th></tr>';
        $_SESSION['total_price'] = '0.00';
        $_SESSION['items'] = 0;

        //place lines of products in page
            foreach ($_SESSION['cart'] as $carid => $qty){
            //Connect to your Database Server with your credentials
            require "dbc.php";

            $query = 'SELECT * FROM cars WHERE id = "'.$carid.'"';
            $result = mysql_query($query);
            $catNo = mysql_num_rows($result);
            $aresult = array();

            $product = mysql_fetch_array($result);
            $tprice = $qty*$product['Price'];
            echo '<tr><td align = "left"><span>';
            echo $product['Car_Name'];
            echo '</span></td>';
            echo '<td align = "right"><span>';
            //name of textbox=carid and value=qty
            echo '<input name="'.$carid.'" type="text" size="3" maxlength="3" value="'.$qty.'">';
            echo '</span></td>';
            echo '<td align = "right"><span>&pound;';
            echo $tprice;
            echo '</span></td></tr>';

            $_SESSION['items'] += $qty;
            $_SESSION['total_price'] += $tprice;

            //close the connection
            mysql_close($conn);
        }
        echo '<input type="hidden" name="update" value=true>';
        echo '<tr bgcolor="yellow"><td align="center">';
        echo '<input type="submit" name="Submit" value="Update Cart"></td>';
        echo '<td align = "right"><b>Total Qty: '.$_SESSION['items'].'</b></td>';
        echo '<td align="right"><b>Total: &pound;'.$_SESSION['total_price'].'</b></td>';
        echo '</tr>';
        echo '</table>';
        echo '</form>';
        echo '<br/><br/>';
        echo '<form action="pay.php" method="post">';
        echo '<input type="submit"  name="CHECKOUT" value="CHECKOUT"></form>';

        //echo '</div>';

    }else{  //if no product in cart
        echo '<div id="content7">';
        echo '<h3><span>You currently have no products in Shopping Cart.</span></h3>';
        echo '<p class="p1"><span></span></p>';
        //echo '</div>';
    }
    ?>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

在您的cart.php中,您询问$_SESSION['cart']是否存在,但稍后再拨打session_start()。在“添加到购物车”功能之前写下session_start();

<强> EDIT2:

//CHANGE action and method attribute
    echo '<div id="content8"><form name="userForm'.$row["id"].'" method="get" action="cart.php">';
    echo '<div class="car-image"><a href="carinformation.php?id='.$row["id"]. '"><img width="500px" height="200px" src="data:image/jpeg;base64,'. base64_encode($row["picture"]). '" />'; 
    echo '<div id="title2">'.$row ["Car_Name"]; 
    echo '<br />Quantity: '.$row ["Quantity"]; 
    echo '<br />Price: &pound'.$row ["Price"];
//CHANGE add this line --v
    echo '<input type="hidden" name="cid" value="'.$row['id'].'" />';
    echo '<br/><input name="addCart" type="submit" value="ADD TO CART"">'; 

修改

<head> 
<title>Prestige Cars Cart</title> 
</head> 

<?php 

//==========================ADD SESSION_START() HERE==========================
session_start();

//if product exists in command line, add it to cart 
    @ $cid = $_GET['cid'];   
    //checks if command line has product added 
    if($cid){ 
        //checks if cart is empty & creates it 
        if(!isset($_SESSION['cart'])){ 
            $_SESSION['cart'] = array(); 
            $_SESSION['items'] = 0; 
            $_SESSION['total_price'] = '0.00'; 
        } 
        //cart array contains car id & quantity 
        if (isset($_SESSION['cart'] [$cid] )) 
            $_SESSION['cart'] [$cid]++; 
        else  
            $_SESSION['cart'] [$cid] = 1; 
    } 

    //checks if command line has product updated 
    if(isset($_POST['update'])){    
        foreach ($_SESSION['cart'] as $carid => $qty){ 
            //value from text box name 
            if($_POST[$carid]<='0') 
                unset($_SESSION['cart'] [$carid]); 
            else  
                $_SESSION['cart'] [$carid] = $_POST[$carid]; 
        } 
    } 
?> 

<?php 
//==========================REMOVE SESSION_START() HERE==========================
?> 

<?php 
    //displays car 
    if  ($_SESSION['cart']){ 
.....