01. januar 2007 - 16:24
Der er
7 kommentarer og
1 løsning
Shopsystem siger fejl og tilføjer 21 varer i stedet for 1
Jeg har ud fra PlainCart-systemet lavet et shopsystem. Men nu opfører den sig pludseligt underligt.
Når jeg går ind på en vare og trykker på "Læg i kurv" og så kommer den her fejlmeddelse op med Firefox:
"Denne side viderefører ikke forespørgslen korrekt
Firefox har opdaget at serveren henviser forespørgelsen for denne adresse på en måde så den aldrig vil blive færdig.
* Dette problem kan nogle gange skyldes, at cookies er slået fra, eller modtagelse af cookies er blevet nægtet."
Og når jeg så går over på siden igen har den tilføjet 21 af den valgte vare.
Hvad kan være problemet?
P.S. Jeg har både prøvet i Firefox og Internet Explorer og problemet er der begge steder. Jeg har også prøvet at fjerne cookies, men det hjælper ikke.
01. januar 2007 - 16:25
#1
#### cart.php ####
<?php
require_once 'library/config.php';
require_once 'library/category-functions.php';
require_once 'library/product-functions.php';
require_once 'library/cart-functions.php';
$_SESSION['shop_return_url'] = $_SERVER['REQUEST_URI'];
$catId = (isset($_GET['c']) && $_GET['c'] != '1') ? $_GET['c'] : 0;
$pdId = (isset($_GET['p']) && $_GET['p'] != '') ? $_GET['p'] : 0;
$action = (isset($_GET['action']) && $_GET['action'] != '') ? $_GET['action'] : 'view';
switch ($action) {
case 'add' :
addToCart();
break;
case 'update' :
updateCart();
break;
case 'delete' :
deleteFromCart();
break;
case 'view' :
}
$cartContent = getCartContent();
$numItem = count($cartContent);
$pageTitle = 'Shopping Cart';
require_once 'include/header.php';
// show the error message ( if we have any )
displayError();
if ($numItem > 0 ) {
?>
<table style="border-collapse: collapse; width: 712px;">
<tr>
<td style="padding: 0px; width: 181px; vertical-align: top; text-align: left;">
<img src="images/tilfaeldigtprodukt.jpg" alt="">
<?php
require_once 'include/random.php';
?>
<br>
<img src="images/kategorier.jpg" alt="">
<?php
require_once 'include/leftNav.php';
?>
<br>
<img src="images/sidekurv.jpg" alt="">
<?php
require_once 'include/miniCart.php';
?>
</td>
<td style="padding: 0px; width: 531px; vertical-align: top; text-align: center;">
<img src="images/contenttop.jpg" alt=""><br>
<form action="<?php echo $_SERVER['PHP_SELF'] . "?action=update"; ?>" method="post" name="frmCart" id="frmCart">
<table style="width: 525px; text-align: center; border: 0px; border-collapse: collapse;">
<tr>
<td style="padding: 0px; width: 75px; text-align: center;" colspan="2">Produkt</td>
<td style="padding: 0px; text-align: center;">Pris</td>
<td style="padding: 0px; width: 75px; text-align: center;">Antal</td>
<td style="padding: 0px; text-align: center;">Total</td>
<td style="padding: 0px; width: 75px; text-align: center;"> </td>
</tr>
<?php
$subTotal = 0;
for ($i = 0; $i < $numItem; $i++) {
extract($cartContent[$i]);
$productUrl = "index.php?c=$cat_id&p=$pd_id";
$subTotal += $pd_price * $ct_qty;
?>
<tr>
<td style="padding: 0px; width: 80px; text-align: center;"><a href="<?php echo $productUrl; ?>"><img src="<?php echo $pd_thumbnail; ?>" border="0"></a></td>
<td style="padding: 0px;"><a href="<?php echo $productUrl; ?>"><?php echo $pd_name; ?></a></td>
<td style="padding: 0px; text-align: right;"><?php echo displayAmount($pd_price); ?></td>
<td style="padding: 0px; width: 75px; text-align: center;"><input name="txtQty[]" type="text" id="txtQty[]" size="5" value="<?php echo $ct_qty; ?>" class="box" onKeyUp="checkNumber(this);">
<input name="hidCartId[]" type="hidden" value="<?php echo $ct_id; ?>">
<input name="hidProductId[]" type="hidden" value="<?php echo $pd_id; ?>">
</td>
<td style="padding: 0px; text-align: right;"><?php echo displayAmount($pd_price * $ct_qty); ?></td>
<td style="padding: 0px; width: 75px; text-align: center;"> <input name="btnDelete" type="button" id="btnDelete" value="Fjern" onClick="window.location.href='<?php echo $_SERVER['PHP_SELF'] . "?action=delete&cid=$ct_id"; ?>';" class="box"></td>
</tr>
<?php
}
?>
<tr>
<td style="padding: 0px; text-align: right;" colspan="4">Sub-total</td>
<td style="padding: 0px; text-align: right;"><?php echo displayAmount($subTotal); ?></td>
<td style="padding: 0px; width: 75px; text-align: center;"> </td>
</tr>
<tr>
<td style="padding: 0px; text-align: right;" colspan="4">Fragt </td>
<td style="padding: 0px; text-align: right;"><?php echo displayAmount($shopConfig['shippingCost']); ?></td>
<td style="padding: 0px; width: 75px; text-align: center;"> </td>
</tr>
<tr>
<td style="padding: 0px; text-align: right;" colspan="4">Total </td>
<td style="padding: 0px; text-align: right;"><?php echo displayAmount($subTotal + $shopConfig['shippingCost']); ?></td>
<td style="padding: 0px; width: 75px; text-align: center;"> </td>
</tr>
<tr class="content">
<td style="padding: 0px; text-align: right;" colspan="5"> </td>
<td style="padding: 0px; width: 75px; text-align: center;"><input name="btnUpdate" type="submit" id="btnUpdate" value="Opdater kurv" class="box"></td>
</tr>
</table>
</form>
<?php
} else {
?>
<table style="border-collapse: collapse; width: 712px;">
<tr>
<td style="padding: 0px; width: 181px; vertical-align: top; text-align: left;">
<img src="images/tilfaeldigtprodukt.jpg" alt="">
<?php
require_once 'include/random.php';
?>
<br>
<img src="images/kategorier.jpg" alt="">
<?php
require_once 'include/leftNav.php';
?>
<br>
<img src="images/sidekurv.jpg" alt="">
<?php
require_once 'include/miniCart.php';
?>
</td>
<td style="padding: 0px; width: 531px; vertical-align: top; text-align: center;">
<img src="images/contenttop.jpg" alt=""><br>
<p> </p>
<table style="width: 525px; text-align: center; border: 0px; border-collapse: collapse;">
<tr>
<td style="padding: 0px; text-align: center;">Din kurv er tom<p>Hvis du ikke kan tilføje nogen varer til din kurv, tjek venligst om din internetbrowser har cookies slået til og om der ikke er andet software, som blokerer dem.</p></td>
</tr>
</table>
<?php
}
$shoppingReturnUrl = isset($_SESSION['shop_return_url']) ? $_SESSION['shop_return_url'] : 'index.php';
?>
<table style="width: 525px; text-align: center; border: 0px; border-collapse: collapse;">
<tr>
<td style="padding: 0px; text-align: center;"><input name="btnContinue" type="button" id="btnContinue" value="<< Gå til butikken" onClick="window.location.href='index.php';" class="box"></td>
<?php
if ($numItem > 0) {
?>
<td style="padding: 0px; text-align: center;"><input name="btnCheckout" type="button" id="btnCheckout" value="Gå til kassen >>" onClick="window.location.href='checkout.php?step=1';" class="box"></td>
<?php
}
?>
</tr>
</table>
<br><img src="images/bottom.jpg" alt="">
<?php
require_once 'include/footer.php';
?>
#### cart-functions.php ####
<?php
require_once 'config.php';
/*********************************************************
* SHOPPING CART FUNCTIONS
*********************************************************/
function addToCart()
{
// make sure the product id exist
if (isset($_GET['p']) && (int)$_GET['p'] > 0) {
$productId = (int)$_GET['p'];
} else {
header('Location: index.php');
}
// does the product exist ?
$sql = "SELECT pd_id, pd_qty
FROM tbl_product
WHERE pd_id = $productId";
$result = dbQuery($sql);
if (dbNumRows($result) != 1) {
// the product doesn't exist
header('Location: cart.php');
} else {
// how many of this product we
// have in stock
$row = dbFetchAssoc($result);
$currentStock = $row['pd_qty'];
if ($currentStock == 0) {
// we no longer have this product in stock
// show the error message
setError('The product you requested is no longer in stock');
header('Location: cart.php');
exit;
}
}
// current session id
$sid = session_id();
// check if the product is already
// in cart table for this session
$sql = "SELECT pd_id
FROM tbl_cart
WHERE pd_id = $productId AND ct_session_id = '$sid'";
$result = dbQuery($sql);
if (dbNumRows($result) == 0) {
// put the product in cart table
$sql = "INSERT INTO tbl_cart (pd_id, ct_qty, ct_session_id, ct_date)
VALUES ($productId, 1, '$sid', NOW())";
$result = dbQuery($sql);
} else {
// update product quantity in cart table
$sql = "UPDATE tbl_cart
SET ct_qty = ct_qty + 1
WHERE ct_session_id = '$sid' AND pd_id = $productId";
$result = dbQuery($sql);
}
// an extra job for us here is to remove abandoned carts.
// right now the best option is to call this function here
deleteAbandonedCart();
header('Location: ' . $_SESSION['shop_return_url']);
}
/*
Get all item in current session
from shopping cart table
*/
function getCartContent()
{
$cartContent = array();
$sid = session_id();
$sql = "SELECT ct_id, ct.pd_id, ct_qty, pd_name, pd_price, pd_thumbnail, pd.cat_id
FROM tbl_cart ct, tbl_product pd, tbl_category cat
WHERE ct_session_id = '$sid' AND ct.pd_id = pd.pd_id AND cat.cat_id = pd.cat_id";
$result = dbQuery($sql);
while ($row = dbFetchAssoc($result)) {
if ($row['pd_thumbnail']) {
$row['pd_thumbnail'] = 'images/product/' . $row['pd_thumbnail'];
} else {
$row['pd_thumbnail'] = 'images/no-image-small.png';
}
$cartContent[] = $row;
}
return $cartContent;
}
/*
Remove an item from the cart
*/
function deleteFromCart($cartId = 0)
{
if (!$cartId && isset($_GET['cid']) && (int)$_GET['cid'] > 0) {
$cartId = (int)$_GET['cid'];
}
if ($cartId) {
$sql = "DELETE FROM tbl_cart
WHERE ct_id = $cartId";
$result = dbQuery($sql);
}
header('Location: cart.php');
}
/*
Update item quantity in shopping cart
*/
function updateCart()
{
$cartId = $_POST['hidCartId'];
$productId = $_POST['hidProductId'];
$itemQty = $_POST['txtQty'];
$numItem = count($itemQty);
$numDeleted = 0;
$notice = '';
for ($i = 0; $i < $numItem; $i++) {
$newQty = (int)$itemQty[$i];
if ($newQty < 1) {
// remove this item from shopping cart
deleteFromCart($cartId[$i]);
$numDeleted += 1;
} else {
// check current stock
$sql = "SELECT pd_name, pd_qty
FROM tbl_product
WHERE pd_id = {$productId[$i]}";
$result = dbQuery($sql);
$row = dbFetchAssoc($result);
if ($newQty > $row['pd_qty']) {
// we only have this much in stock
$newQty = $row['pd_qty'];
// if the customer put more than
// we have in stock, give a notice
if ($row['pd_qty'] > 0) {
setError('The quantity you have requested is more than we currently have in stock. The number available is indicated in the "Quantity" box. ');
} else {
// the product is no longer in stock
setError('Sorry, but the product you want (' . $row['pd_name'] . ') is no longer in stock');
// remove this item from shopping cart
deleteFromCart($cartId[$i]);
$numDeleted += 1;
}
}
// update product quantity
$sql = "UPDATE tbl_cart
SET ct_qty = $newQty
WHERE ct_id = {$cartId[$i]}";
dbQuery($sql);
}
}
if ($numDeleted == $numItem) {
// if all item deleted return to the last page that
// the customer visited before going to shopping cart
header("Location: $returnUrl" . $_SESSION['shop_return_url']);
} else {
header('Location: cart.php');
}
exit;
}
function isCartEmpty()
{
$isEmpty = false;
$sid = session_id();
$sql = "SELECT ct_id
FROM tbl_cart ct
WHERE ct_session_id = '$sid'";
$result = dbQuery($sql);
if (dbNumRows($result) == 0) {
$isEmpty = true;
}
return $isEmpty;
}
/*
Delete all cart entries older than one day
*/
function deleteAbandonedCart()
{
$yesterday = date('Y-m-d H:i:s', mktime(0,0,0, date('m'), date('d') - 1, date('Y')));
$sql = "DELETE FROM tbl_cart
WHERE ct_date < '$yesterday'";
dbQuery($sql);
}
?>
02. januar 2007 - 06:41
#7
En god ide er lige at skrive løsningen her, bare for god ordens skyld. - og af bitter erfaring kan jeg sige, at hvis du selv sørger for at ligge løsningen her vil du takke dig selv når du "måske" står med problemet igen. :)
Og så kan andre jo også få glæde af dit spørgsmål.
02. januar 2007 - 12:39
#8
Problemet var egentlig at jeg var kommet til at inkludere de forkerte filer i cart.php. Dvs. jeg har rettet starten til det her:
#### cart.php ####
<?php
require_once 'library/config.php';
require_once 'library/category-functions.php';
require_once 'library/cart-functions.php';
$catId = (isset($_GET['c']) && $_GET['c'] != '1') ? $_GET['c'] : 0;
$action = (isset($_GET['action']) && $_GET['action'] != '') ? $_GET['action'] : 'view';