貨品不能加入購物車

加入購物車PHP
<?php

if(!isset($_SESSION)){
$_SESSION = array();
}

session_start();

$id = isset($_POST) ? $_POST : "1";
$name = isset($_POST) ? $_POST : "";

$host = "***";
$uname = "***";
$pass = "***";
$database = "***";
$tablename = "***";

if(!$link = mysql_connect ($host, $uname, $pass))
exit("***");

// Select a database from MySQL
if(!$result = mysql_select_db($database, $link))
exit("***");

if(array_key_exists($pid, $_SESSION)){
// redirect to product list and tell the user it was added to cart
header('Location: products.php?action=exists&id' . $id . '&name=' . $name);
}

// else, add the item to the array
else{
$_SESSION[$pid]=$name;
// redirect to product list and tell the user it was added to cart
header('Location: products.php?action=added&id' . $id . '&name=' . $name);
}
?>

會是上面的If else出了問題嗎?


購物車PHP
<?php

session_start();

$action = isset($_POST) ? $_POST : "";
$id = isset($_POST) ? $_POST : "1";
$name = isset($_POST) ? $_POST : "";
$rice = isset($_POST) ? $_POST : "";

$host = "***";
$uname = "***";
$pass = "***";
$database = "***";
$tablename = "***";

if(!$link = mysql_connect ($host, $uname, $pass))
exit("***");

// Select a database from MySQL
if(!$result = mysql_select_db($database, $link))
exit("***");

$query = "SELECT id, name, price FROM *** ORDER BY name";
if(!$result = mysql_query ($query, $link))
exit("***");

if(!$rec = mysql_fetch_array ($result)){
echo ("***");
}

else {

echo "<br />";
echo "<table border width=100%>";

echo "<tr>";
echo "<th class='textAlignLeft'><br /><h2>Product Name</h2><br /></th>";
echo "<th><br /><h2>Price</h2><br /></th>";
echo "<th><br /><h2>Action</h2><br /></th>";
echo "</tr>";

$total_price=0;

echo "<tr>";
echo "<td>{$name}</td>";
echo "<td>${$price}</td>";
echo "<td><a href='removetocart.php?id={$id}&name={$name}'>";
echo "<span class='glyphicon glyphicon-shopping-cart'></span> Remove from cart</td>";
echo "</a>";
echo "</tr>";

$total_price+=$price;
}

echo "<tr>";
echo "<td><b>Total</b></td>";
echo "<td>${$total_price}</td>";
echo "<td>";
echo "<a href='#' class='btn btn-success'>";
echo "<span class='glyphicon glyphicon-shopping-cart'></span> Checkout";
echo "</a>";
echo "</td>";
echo "</tr>";

while ($rec = mysql_fetch_array ($result));
echo "</table>";

// Release the Result Set
mysql_free_result($result);

// Close the connection
mysql_close($link);
?>

評論

Sign In or Register to comment.