Im using dreamweaver with PHP.
I have items in my shopping cart which is then passed on to the transaction page.

How do i add the amount of items in the shopping cart?

Any help is appreciated!
Thanks!

Dani AI

Generated

Summary: the page shows a per-row text box (NoOfItems) but there’s no clear server-side handling that saves or uses those quantities. Decide whether your cart is stored in the database (persisted) or in the session (temporary). If you’re using the DB you need a numeric quantity column and an update flow; if you prefer sessions you must keep a quantity value per product keyed to the session or user.

Practical, minimal steps you can apply now:

  • Add a quantity column to the cart table (or a session_id/user_id column if you don’t already filter rows by user). Example SQL:

    ALTER TABLE cart ADD COLUMN quantity INT NOT NULL DEFAULT 1;
  • Change each quantity input to submit as an array keyed by cart id (so you can update all rows in one request), e.g. name="quantity[123]" where 123 is the cartid. On submit, validate and update each row server-side. Example processing (use PDO or mysqli; do not use deprecated mysql_* functions):

    if (isset($_POST['Updatebtn']) && !empty($_POST['quantity'])) {
        foreach ($_POST['quantity'] as $cartid => $qty) {
            $qty = filter_var($qty, FILTER_VALIDATE_INT, ['options'=>['min_range'=>1]]) ?: 1;
            $stmt = $pdo->prepare("UPDATE cart SET quantity = :q WHERE cartid = :id AND session_id = :sid");
            $stmt->execute([':q'=>$qty, ':id'=>$cartid, ':sid'=>$sessionId]);
        }
    }

While rendering the cart, compute line totals and grand total: line = price * quantity; total += line; Escape displayed values with htmlspecialchars() to prevent XSS. Use <input type="number" min="1"> on the client side but always validate server-side.

Notes and troubleshooting: ’s suggestion to track per-item quantities is on target; , rename your NoOfItems inputs to a keyed array and ensure your update code actually writes the new value back to storage. Keep session_start() before any output, switch to PDO/mysqli+prepared statements, and add a CSRF token if this is a production checkout page.

Recommended Answers

All 3 Replies

Are you using a session to store the stuff in the cart or what? I'd suggest creating a session for the user with the following variables:

array $quantity
array $product

each element of those arrays will contain the info for an item. ($quantity[0] has the quantity of the first item and $product[0] has the name or item number or id of the product).

Are you using a session to store the stuff in the cart or what? I'd suggest creating a session for the user with the following variables:

array $quantity
array $product

each element of those arrays will contain the info for an item. ($quantity[0] has the quantity of the first item and $product[0] has the name or item number or id of the product).

Hey there.
Here's a code to my shopping cart

<?php require_once('../Connections/myDatabase.php'); ?>
<?php session_start(); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}


if ((isset($_POST['cartid'])) && ($_POST['cartid'] != "")) {
  $deleteSQL = sprintf("DELETE FROM cart WHERE cartid=%s",
                       GetSQLValueString($_POST['cartid'], "int"));

  mysql_select_db($database_myDatabase, $myDatabase);
  $Result1 = mysql_query($deleteSQL, $myDatabase) or die(mysql_error());

  $deleteGoTo = "DeleteSuccessfully.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?";
    $deleteGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $deleteGoTo));
}

mysql_select_db($database_myDatabase, $myDatabase);
$query_Cart = "SELECT * FROM cart";
$Cart = mysql_query($query_Cart, $myDatabase) or die(mysql_error());
$row_Cart = mysql_fetch_assoc($Cart);
$totalRows_Cart = mysql_num_rows($Cart);
?>

<!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>Untitled Document</title>
<link href="../eb-main.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
.style1 {color: #FFFFFF}
.style2 {font-size: small
}
body {
	background-color: #FFFFFF;
}
.style3 {color: #000000}
.style6 {font-size: 18px}
.style7 {color: #FFFFFF; font-size: medium; }
-->
</style>
</head>

<body>
<div id="wrapper">
  <div class="style1" id="banner">
    <div align="center">
      <p><img src="../Images/BooksInc Banner.jpg" width="751" height="300" /></p>
      <p>
        <map name="Map" id="Map">
          <area shape="rect" coords="2,0,145,63" href="../Pages/EventsMgt.html" />
          <area shape="rect" coords="154,0,283,63" href="../Pages/PA System.html" />
          <area shape="rect" coords="299,1,449,63" href="../Pages/RehearsalSuiteRental.html" />
          <area shape="rect" coords="651,-4,762,62" href="../Pages/BandTshirtDesign.html" />
        </map>
      <img src="../Images/BookHavenMenu.jpg" width="751" height="50" border="0" usemap="#Map2" />
<map name="Map2" id="Map2"><area shape="rect" coords="3,3,104,47" href="Home.php" />
<area shape="rect" coords="124,3,277,47" href="SelectUrBooks.php" />
<area shape="rect" coords="301,3,410,49" href="Transaction.php" />
</map></p>
    </div>
                                                                                 
    
                                                                                                                                                                             
    
                                                                                                                                                                                 
    
                                                                                                                                                                             
    
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Welcome, {stranger!}</div>
               
        

  <p><span class="style2"><span class="style1"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></p>
  

    <p align="right">    
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    
    <a href="SelectUrBooks.php" class="style7">Continue Shopping</a>
<table width="83%" border="1">
      <?php do { ?>
      <form id="form1" name="form1" method="POST" action="">

        <tr>
          <td width="23%" scope="col"><span class="style6">
            <input name="cartid" type="hidden" id="cartid" value="<?php echo $row_Cart['cartid']; ?>" />
          <?php echo $row_Cart['title']; ?></span></td>
          <td width="23%" scope="col"><span class="style6"><?php echo $row_Cart['author']; ?></span></td>
          <td width="16%" scope="col"><span class="style6">$<?php echo $row_Cart['price']; ?></span></td>
          <td width="18%" scope="col"><span class="style6"><?php echo $row_Cart['category']; ?></span></td>
          <td width="5%" scope="col"><label>
            <input name="NoOfItems" type="text" id="NoOfItems" size="5" />
          </label></td>
          <td width="8%" scope="col"><input type="submit" name="Updatebtn" id="Updatebtn" value="Update" /></td>
          <td width="7%" scope="col"><label>
          <input type="submit" name="DeleteBtn" id="DeleteBtn" value="Delete" />
          </label></td>
        </tr>  </form>
        <?php } while ($row_Cart = mysql_fetch_assoc($Cart)); ?>
    </table>
  <p align="right">&nbsp;&nbsp;<span class="style6">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="ConfirmDelete.php"></a>      </span>
    &nbsp;&nbsp;&nbsp;
  
  <p> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>

  <div class="style1" id="footer">
    <div align="center">
      <p class="style2">&nbsp;</p>
      <p class="style2">&nbsp;</p>
      <p class="style2">&nbsp;</p>
      <p class="style2">&nbsp;</p>
      <p class="style2">&nbsp;</p>
      <p class="style2">&nbsp;</p>
      <p class="style2"><span class="style3"><a href="Home.php">Home</a><span class="style1"> | </span><a href="SelectUrBooks.php">Select ur Books!</a> <span class="style1">| Transaction | Checkout | Transaction Summary</span></span></p>
      <p>&copy; Book Haven productions</p>
    </div>
  </div>
  </div>
</body>
</html>
<?php
mysql_free_result($Cart);
?>

If possible you could guide my through the steps cos im a beginner in PHP.

Hmm, you've got an input for the quantity. Did you add that in the hope of getting it to work? Does the cart work how it is without quantity? What do you actually mean by "add the amount of items in the shopping cart"?.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.