www.webdeveloper.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 35

Thread: Simple php update

  1. #1
    Join Date
    Dec 2006
    Posts
    63

    Simple php update

    I've used the built in update option in dreamweaver to allow users to change the quantity of an item in their shopping cart via php.

    the problem is it only seems to work for the first item entered into the cart and non of the others in there. any pointers on the reason why?

    Sean
    Last edited by sean1983uk; 02-07-2007 at 02:55 PM.

  2. #2
    Join Date
    Dec 2006
    Location
    Escaz˙ (Costa Rica) and Mallorca (Spain)
    Posts
    3,234
    It would depend on the generated code. If the update was only intended for a single item, that's what will happen.

    If you can post more code, the "lapse" would be easier to pinpoint...

  3. #3
    Join Date
    Dec 2006
    Posts
    63
    Quote Originally Posted by NightShift58
    It would depend on the generated code. If the update was only intended for a single item, that's what will happen.

    If you can post more code, the "lapse" would be easier to pinpoint...
    hey sorry would have made sense to post code lol

    heres all my code on the cart page:

    Code:
    <?php require_once('Connections/conn.php'); ?>
    <?php
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
    {
      $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $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;
    }
    ?>
    <?php require_once('Connections/conn.php'); ?>
    <?php
    $editFormAction = $_SERVER['PHP_SELF'];
    if (isset($_SERVER['QUERY_STRING'])) {
      $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
    }
    
    if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form2")) {
      $updateSQL = sprintf("UPDATE tblcart SET productQuantity=%s WHERE cartID=%s AND productID=%s" ,
                           GetSQLValueString($_POST['quantity'], "int"),
                           GetSQLValueString($_POST['cartID'], "int"),
    					   GetSQLValueString($_POST['productID'], "int"));
    
      mysql_select_db($database_conn, $conn);
      $Result1 = mysql_query($updateSQL, $conn) or die(mysql_error());
    
      $updateGoTo = "cart.php";
      if (isset($_SERVER['QUERY_STRING'])) {
        $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
        $updateGoTo .= $_SERVER['QUERY_STRING'];
      }
      header(sprintf("Location: %s", $updateGoTo));
    }
    
    if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
      $insertSQL = sprintf("INSERT INTO tblOrders (orderName, orderPhone, orderEmail, orderTotal) VALUES (%s, %s, %s, %s)",
                           GetSQLValueString($_POST['orderName'], "text"),
                           GetSQLValueString($_POST['orderPhone'], "text"),
                           GetSQLValueString($_POST['orderEmail'], "text"),
                           GetSQLValueString($_POST['orderTotal'], "double"));
    
      mysql_select_db($database_conn, $conn);
      $Result1 = mysql_query($insertSQL, $conn) or die(mysql_error());
    	
      $orderID = mysql_insert_id();
      
      //get the cart details
      if (isset($_SESSION['cartGuid'])) {
        $varCartGuid_rsCart = (get_magic_quotes_gpc()) ? $_SESSION['cartGuid'] : addslashes($_SESSION['cartGuid']);
      }
      $query_rsCart = sprintf("SELECT * FROM tblCart, tblProducts WHERE tblProducts.productID = tblCart.productID  AND cartGuid =  '%s'", $varCartGuid_rsCart);
      $rsCart = mysql_query($query_rsCart, $conn) or die(mysql_error());
      $row_rsCart = mysql_fetch_assoc($rsCart);
      $totalRows_rsCart = mysql_num_rows($rsCart);
      do {
      	$sql = "INSERT INTO tblOrderProducts (productName, productPrice, productCode, productQuantity, productSubtotal, orderID) VALUES ('" . $row_rsCart['productName'] ."','" . $row_rsCart['productPrice'] ."','" . $row_rsCart['productCode'] ."'," . $row_rsCart['productQuantity'] .",'" .$row_rsCart['productPrice']*$row_rsCart['productQuantity'] ."'," . $orderID .")";
    	
    	$Result2 = mysql_query($sql, $conn) or die(mysql_error());
    	
      } while ($row_rsCart = mysql_fetch_assoc($rsCart));
      
      //delete the session
      $deleteSQL = "DELETE FROM tblCart WHERE cartGuid = '" . $_SESSION['cartGuid'] . "'";
      $Result3 = mysql_query($deleteSQL, $conn) or die(mysql_error());
      $_SESSION['cartGuid'] = '';
      
      $insertGoTo = "thankyou.php";
      if (isset($_SERVER['QUERY_STRING'])) {
        $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
        $insertGoTo .= $_SERVER['QUERY_STRING'];
      }
      header(sprintf("Location: %s", $insertGoTo));
    }
    
    $varCartGuid_rsCart = "1";
    if (isset($_SESSION['cartGuid'])) {
      $varCartGuid_rsCart = (get_magic_quotes_gpc()) ? $_SESSION['cartGuid'] : addslashes($_SESSION['cartGuid']);
    }
    mysql_select_db($database_conn, $conn);
    $query_rsCart = sprintf("SELECT * FROM tblCart, tblProducts WHERE tblProducts.productID = tblCart.productID  AND cartGuid =  '%s'", $varCartGuid_rsCart);
    $rsCart = mysql_query($query_rsCart, $conn) or die(mysql_error());
    $row_rsCart = mysql_fetch_assoc($rsCart);
    $totalRows_rsCart = mysql_num_rows($rsCart);
    
    ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <title>Your shopping cart</title>
    </head>
    
    <body>
    <h1>Your Shopping Cart</h1>
    <form action="<?php echo $editFormAction; ?>" name="form2" id="form1" method="POST">
      <table>
        <tr>
          <th scope="col">Product name </th>
          <th width="5" scope="col">QUANTITY</th>
          <th scope="col">Price</th>
          <th scope="col">Subtotal</th>
          <th scope="col">&nbsp;</th>
        </tr>
        <?php
      $total = 0;
       do { ?>
        <tr>
          <td><input type="hidden" name="MM_update" value="form2" />
          <input name="productID" type="hidden" id="productID" value="<?php echo $row_rsCart['productID']; ?>" />
            <input name="cartID" type="hidden" id="cartID" value="<?php echo $row_rsCart['cartID']; ?>" />
              <?php echo $row_rsCart['productName']; ?></td>
          <td width="5"><input name="quantity" type="text" id="quantity" value="<?php echo $row_rsCart['productQuantity']; ?>" size="3" />
            <input type="submit" name="Submit" value="update" /></td>
          <td>&pound;<?php echo $row_rsCart['productPrice']; ?></td>
          <td>&pound;<?php echo $row_rsCart['productPrice']*$row_rsCart['productQuantity']; ?></td>
          <td><a href="delete.php?cartID=<?php echo $row_rsCart['cartID']; ?>">del</a></td>
        </tr>
        <?php $total = $total+($row_rsCart['productPrice']*$row_rsCart['productQuantity']); ?>
        <?php } while ($row_rsCart = mysql_fetch_assoc($rsCart)); ?>
      </table>
    </form>
    <p>&nbsp;</p>
    <p>Total price: <strong>&pound;<?php echo $total; ?></strong>   <a href="updateCart.php?<?php echo $row_rsCart['cartID']; ?>=<?php echo $row_rsCart['cartID']; ?>&amp;<?php echo $row_rsCart['productID']; ?>=<?php echo $row_rsCart['productID']; ?>&amp;<?php echo $row_rsCart['productQuantity']; ?>=<?php echo $row_rsCart['productQuantity']; ?>"></a> </p>
    <p>Return to the <a href="products.php">products listing</a> or complete your details below to check out. </p>
    <form name="checkout" id="checkout" method="POST" action="<?php echo $editFormAction; ?>">
      <div><label for="orderName">Name</label>
      <input type="text" name="orderName" id="orderName" /></div>
      <div><label for="orderPhone">Phone number</label>
      <input type="text" name="orderPhone" id="orderPhone" /></div>
      <div><label for="orderEmail">Email</label>
      <input type="text" name="orderEmail" id="orderEmail" /></div>
      <input type="hidden" name="orderTotal" id="orderTotal" value="<?php echo $total; ?>" />
      <div><input type="submit" name="btnSubmit" value="Checkout" /></div>
      <input type="hidden" name="MM_insert" value="form1">
    </form>
    
    </body>
    </html>
    <?php
    mysql_free_result($rsCart);
    ?>
    the update button is in a repeating area so each item has the update button with it...

    any pointers?
    Last edited by sean1983uk; 02-07-2007 at 03:08 PM.

  4. #4
    Join Date
    Dec 2006
    Location
    Escaz˙ (Costa Rica) and Mallorca (Spain)
    Posts
    3,234
    On the form, only 1 item is displayed and the update is coded to update that one item only, which is the way it's supposed to be.

    Are you saying that even when you display Item #3 in the form, it will not update the quantity fo Item #3?

  5. #5
    Join Date
    Dec 2006
    Posts
    63
    when i add items to the cart, so say there is then 5 items in the cart. it will only update the quantity of the first item that was added to the cart the others stay the same even when quantity changed. That form u see gets repeated for each item and each item in turn has its own update button.

    is it something to do with where it says:

    if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form2"))

    is it only looking at form2 and the repeated ones get called something else?
    Last edited by sean1983uk; 02-07-2007 at 03:20 PM.

  6. #6
    Join Date
    Dec 2006
    Posts
    63
    just tried this..

    added 5 items to the cart and then changed quantities of all of them in the form and pressed the submit button on the last item added to cart.

    it is only updating the first item in the cart. is there a way to make it update mulitpy records?

    Guessing i need some kind of loop?
    Last edited by sean1983uk; 02-07-2007 at 03:24 PM.

  7. #7
    Join Date
    Dec 2006
    Location
    Escaz˙ (Costa Rica) and Mallorca (Spain)
    Posts
    3,234
    No, all the forms are named the same.

    The problem is that all productID have the same name, and only the first one is passed.

    You'll need to change:
    PHP Code:
    <?php
    <input name="productID" type="hidden" id="productID" value="<?php echo $row_rsCart['productID']; ?>" />
    ?>
    to:
    PHP Code:
    <?php
    <input name="productID[]" type="hidden" id="productID" value="<?php echo $row_rsCart['productID']; ?>" />
    ?>
    which will then pass an array of fields instead of just one.

  8. #8
    Join Date
    Dec 2006
    Posts
    63
    each item i add to cart has a dif productID though.

    i tried what you said, do i need to change anything anywhere else?

    as i just changed this and it doesn't update either...
    Last edited by sean1983uk; 02-07-2007 at 04:12 PM.

  9. #9
    Join Date
    Dec 2006
    Location
    Escaz˙ (Costa Rica) and Mallorca (Spain)
    Posts
    3,234
    Yes, it could be modified to accept more than one change at a time but it would require more changes in the script.

  10. #10
    Join Date
    Dec 2006
    Posts
    63
    really confused now, that code you said to change what will that do?

  11. #11
    Join Date
    Dec 2006
    Location
    Escaz˙ (Costa Rica) and Mallorca (Spain)
    Posts
    3,234
    You're too fast...

    To complete the changes post in Post #7, this:
    PHP Code:
    <?php
          
    <td width="5"><input name="quantity" type="text" id="quantity" value="<?php echo $row_rsCart['productQuantity']; ?>" size="3" />
    ?>
    must be changed to:
    PHP Code:
          <td width="5"><input name="quantity[]" type="text" id="quantity" value="<?php echo $row_rsCart['productQuantity']; ?>" size="3" />
    This part as well, from:
    PHP Code:
    <?php
      $updateSQL 
    sprintf("UPDATE tblcart SET productQuantity=%s WHERE cartID=%s AND productID=%s" ,
                           
    GetSQLValueString($_POST['quantity'], "int"),
                           
    GetSQLValueString($_POST['cartID'], "int"),
                           
    GetSQLValueString($_POST['productID'], "int"));
      
    mysql_select_db($database_conn$conn);
      
    $Result1 mysql_query($updateSQL$conn) or die(mysql_error());
    ?>
    needs to be changed as well, to:
    PHP Code:
      FOREACH ($_POST['productID'] as $key => $thisPRODUCTID) :
        
    $updateSQL sprintf("UPDATE tblcart SET productQuantity=%s WHERE cartID=%s AND productID=%s" ,
                             
    GetSQLValueString($_POST['quantity'], "int"),
                             
    GetSQLValueString($_POST['cartID'], "int"),
                             
    GetSQLValueString($$thisPRODUCTID"int"));
        
    mysql_select_db($database_conn$conn);
        
    $Result1 mysql_query($updateSQL$conn) or die(mysql_error());
      ENDFOREACH;
    ?> 

  12. #12
    Join Date
    Dec 2006
    Location
    Escaz˙ (Costa Rica) and Mallorca (Spain)
    Posts
    3,234
    Last modification would be, from this:
    PHP Code:
    <?php
    <form action="<?php echo $editFormAction; ?>" name="form2" id="form1" method="POST">
      <
    table>
        <
    tr>
          <
    th scope="col">Product name </th>
          <
    th width="5" scope="col">QUANTITY</th>
          <
    th scope="col">Price</th>
          <
    th scope="col">Subtotal</th>
          <
    th scope="col">&nbsp;</th>
        </
    tr>
        <?
    php
      $total 
    0;
       do { 
    ?>
        <tr>
          <input name="productID" type="hidden" id="productID" value="<?php echo $row_rsCart['productID']; ?>" />
            <input name="cartID" type="hidden" id="cartID" value="<?php echo $row_rsCart['cartID']; ?>" />
              <?php echo $row_rsCart['productName']; ?></td>
          <td width="5"><input name="quantity[]" type="text" id="quantity" value="<?php echo $row_rsCart['productQuantity']; ?>" size="3" />
            <input type="submit" name="Submit" value="update" /></td>
          <td>&pound;<?php echo $row_rsCart['productPrice']; ?></td>
          <td>&pound;<?php echo $row_rsCart['productPrice']*$row_rsCart['productQuantity']; ?></td>
          <td><a href="delete.php?cartID=<?php echo $row_rsCart['cartID']; ?>">del</a></td>
        </tr>
        <?php $total $total+($row_rsCart['productPrice']*$row_rsCart['productQuantity']); ?>
        <?php } while ($row_rsCart mysql_fetch_assoc($rsCart)); ?>
      </table>
    </form>
    to:
    PHP Code:
    <?php
    <form action="<?php echo $editFormAction; ?>" name="form2" id="form1" method="POST">
      <
    input name="productID" type="hidden" id="productID" value="<?php echo $row_rsCart['productID']; ?>" />
      <
    input name="cartID" type="hidden" id="cartID" value="<?php echo $row_rsCart['cartID']; ?>" />
      <
    table>
        <
    tr>
          <
    th scope="col">Product name </th>
          <
    th width="5" scope="col">QUANTITY</th>
          <
    th scope="col">Price</th>
          <
    th scope="col">Subtotal</th>
          <
    th scope="col">&nbsp;</th>
        </
    tr>
        <?
    php
      $total 
    0;
       do { 
    ?>
        <tr>
              <?php echo $row_rsCart['productName']; ?></td>
          <td width="5"><input name="quantity[]" type="text" id="quantity" value="<?php echo $row_rsCart['productQuantity']; ?>" size="3" />
          <td>&pound;<?php echo $row_rsCart['productPrice']; ?></td>
          <td>&pound;<?php echo $row_rsCart['productPrice']*$row_rsCart['productQuantity']; ?></td>
          <td><a href="delete.php?cartID=<?php echo $row_rsCart['cartID']; ?>">del</a></td>
        </tr>
        <?php $total $total+($row_rsCart['productPrice']*$row_rsCart['productQuantity']); ?>
        <?php } while ($row_rsCart mysql_fetch_assoc($rsCart)); ?>
        <tr><td coslpan="4"><input type="submit" name="Submit" value="update" /></td></tr>
      </table>
    </form>
    ?>

  13. #13
    Join Date
    Dec 2006
    Posts
    63
    hi i added that last part (post 11) but it doesn't seem to go in properly, i mean the FOREACH just stays as normal text. do i need to delete the line before it?

  14. #14
    Join Date
    Dec 2006
    Location
    Escaz˙ (Costa Rica) and Mallorca (Spain)
    Posts
    3,234
    Take out the <?php and ?>.

    Copy only the code part...

  15. #15
    Join Date
    Dec 2006
    Posts
    63
    i did that but still didn't turn the FOREACH green

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles