Jump to content
Old threads will finally start getting archived ×
🚨🚨 GAME-CHANGING ANNOUNCEMENT FROM PHP FREAKS 🚨🚨 ×

Recommended Posts

Ok here it goes..

 

I am creating a practice php shopping cart website and I am kinda stuck. I would like to make it so when the user has multiple products in the

shopping cart they can easily change the quantity and it will make the updates in the database. When you add only 1 product in the database

it has no problem editing the quantity and changing the prices accordingly but when you start having more than 1 item lets say 2 or more items

in the shopping cart I can not make it so it can change the quantities on all the products at once. Can anyone please help me on this, I

can not figure this out.

 

www.robslounge.com/robs_shoppingcart/

 

<?php require_once('Connections/shoppingcart.php'); ?>
<? session_start();
require("functions.php");

if(isset($_POST['Submit'])){
$id = $_POST['id'];
$quantity = $_POST['quantity'];

                
        mysql_query("UPDATE cart SET quantity = '$quantity' WHERE id = '$id'");

}



?>
<?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 (!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;
}
}

$maxRows_view_cart = 10;
$pageNum_view_cart = 0;
if (isset($_GET['pageNum_view_cart'])) {
  $pageNum_view_cart = $_GET['pageNum_view_cart'];
}
$startRow_view_cart = $pageNum_view_cart * $maxRows_view_cart;

mysql_select_db($database_shoppingcart, $shoppingcart);
$query_view_cart = "SELECT * FROM cart WHERE session_id = '$session_id'";
$query_limit_view_cart = sprintf("%s LIMIT %d, %d", $query_view_cart, $startRow_view_cart, $maxRows_view_cart);
$view_cart = mysql_query($query_limit_view_cart, $shoppingcart) or die(mysql_error());
$row_view_cart = mysql_fetch_assoc($view_cart);

if (isset($_GET['totalRows_view_cart'])) {
  $totalRows_view_cart = $_GET['totalRows_view_cart'];
} else {
  $all_view_cart = mysql_query($query_view_cart);
  $totalRows_view_cart = mysql_num_rows($all_view_cart);
}
$totalPages_view_cart = ceil($totalRows_view_cart/$maxRows_view_cart)-1;

mysql_select_db($database_shoppingcart, $shoppingcart);
$query_totalcost = "SELECT SUM(price) FROM cart WHERE session_id='$session_id'";
$totalcost = mysql_query($query_totalcost, $shoppingcart) or die(mysql_error());
$row_totalcost = mysql_fetch_assoc($totalcost);
$totalRows_totalcost = mysql_num_rows($totalcost);
?><!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>
</head>

<body>
<table width="990" height="654" border="1" align="center" cellspacing="3">
  <tr>
    <td width="135" valign="top"> </td>
    <td width="636" valign="top"><br />
      <form id="form1" name="form1" method="post" action="">
        <p> </p>        
<?
# If the shopping cart in the current session is not empty
if($row_view_cart['id']!=""){ 
?>
                <table width="631" border="1">
          <tr>
            <td width="120"> </td>
            <td width="324"><br />
            </td>
            <td width="95"><label>quantity</label></td>
            <td width="64">Price</td>
          </tr>
          <?php do { ?>
          <tr>
         
              <td><img name="" src="products/<?php echo $row_view_cart['image']; ?>" width="120" height="120" alt="" /></td>
              <td><a href="productdetails.php?productid=<?php echo $row_view_cart['product_id']; ?>"><?php echo $row_view_cart['product_name']; ?></a><br />
                Price: $<?php echo $row_view_cart['storeprice']; ?></td>
              <td><select name="quantity" id="quantity">
                  <option value="1" <?php if (!(strcmp(1, $row_view_cart['quantity']))) {echo "selected=\"selected\"";} ?>>1</option>
                  <option value="2" <?php if (!(strcmp(2, $row_view_cart['quantity']))) {echo "selected=\"selected\"";} ?>>2</option>
                  <option value="3" <?php if (!(strcmp(3, $row_view_cart['quantity']))) {echo "selected=\"selected\"";} ?>>3</option>
                  <option value="4" <?php if (!(strcmp(4, $row_view_cart['quantity']))) {echo "selected=\"selected\"";} ?>>4</option>
                  <option value="5" <?php if (!(strcmp(5, $row_view_cart['quantity']))) {echo "selected=\"selected\"";} ?>>5</option>
                  <option value="6" <?php if (!(strcmp(6, $row_view_cart['quantity']))) {echo "selected=\"selected\"";} ?>>6</option>
                                  </select>
                  <input name="id" type="hidden" id="id" value="<?php echo $row_view_cart['id']; ?>" /></td>
              <td>$<?php echo  sprintf("%01.2f", $row_view_cart['price']); ?></td>
          
</tr>
<? echo "$row_view_cart[id],";?>
    <?php } while ($row_view_cart = mysql_fetch_assoc($view_cart)); ?>
        </table>
        <br />
        <table width="100%" border="1">
                           <tr>
                             <td><div align="right">Total: $<?php 
                             
                              echo sprintf("%01.2f", $row_totalcost['SUM(price)']);
                            ?></div></td>
                           </tr>
                         </table>
                         <br />
          <br />
          <label>
          <div align="right">
            <input type="submit" name="Submit" id="Submit" value="Submit" />
          </div>
          </label>
          <?
          # If it's been found that the current session does not have any products in the shopping cart. 
          } else {
          
          echo "<p align=center>Shopping cart is currently empty!</p>";
          }
          
          ?>
       
          <p align="center"><a href="index.php">Main menu</a></p>
        <p> </p>
        <p> </p>
        <p></p>
        <p> </p>
        <p> </p>
        <p> </p>
      </form>
      <p align="center"> </p></td>
    <td width="197" valign="top"> </td>
  </tr>
</table>
</body>
</html>
<?php
mysql_free_result($view_cart);

mysql_free_result($totalcost);
?> 

 

 

It would be so great if someone could help me with this

Link to comment
https://forums.phpfreaks.com/topic/117107-changing-quantities-in-shopping-cart/
Share on other sites

use:

<input name="56" value="4" />

 

where 56 is the product_id and 4 the quantity

 

when the user then presses the update button, you take the items from the shopping cart and loop over them, when an items product_id is found in the POST variable with a different value for the quantity, you perform the update

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.