SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist Alexandra's Avatar
    Join Date
    Aug 2002
    Location
    Perth, Australia
    Posts
    414
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Update code suddenly stopped working, help

    I have a script that updates the quantity of a product in a database table. It was working fine until this morning when I made a change to the table structure (which should not effect this) and it has now stopped working.

    I can't see why the change I made would cause it to stop working, as all I did was add another field to be used by a different page.

    The actual code for updating the table is:
    PHP Code:
    if ($_REQUEST['req']) {
              
    //Change the quantity of products ordered
             
    $updateitem mysql_query("UPDATE cart SET quantity = '" $_GET['qty'] . "' WHERE cartitem_id = '" $_GET['id'] . "' AND session_id = '" session_ID() . "'");
              
              if (
    $_GET['qty'] == "0") {
                 
    $deleteitem mysql_query("DELETE FROM cart WHERE cartitem_id = '" $_GET['id'] . "' AND session_id = '" session_ID() . "'");
              }
          } 
    The code that this relates to on the page is:
    PHP Code:
    echo "
                          <p>You have the following items in your shopping cart:</p>
                             
                          <form action='cart.php' method='get' class='clearback'>
                           <input type='hidden' name='req' value='update'>
                           <table class='data'>
                           <tr>
                               <th>Item</th>
                               <th width='10%' class='digits'>Price</th>
                               <th width='15%' class='digits'>Quantity</th>
                               <th width='15%' class='digits'>Sub-Total</th>
                           </tr>
                          "
    ;
                      while(
    $row mysql_fetch_assoc($displaycart)) {
                             echo 
    "
                           <tr>    
                               <td>
                                   " 
    $row['product_name'] . "
                                   <input type='hidden' name='id' value='" 
    $row['cartitem_id'] . "'>    
                               </td>
                               <td class='digits'>$" 
    $row['product_cost'] . "</td>
                               <td class='digits'><input class='box2' type='text' name='qty' id='qty" 
    $row['cartitem_id'] . "' size='1' value='" $row['quantity'] . "'></td>
                               <td class='digits'>
                             "
    ;
                          
                           
    //Work out sub-total
                           
    $subtotal = ($row['product_cost'] * $row['quantity']);
                           echo 
    "$" number_format($subtotal,2);
                             echo 
    "
                               </td>
                           </tr>
                             "
    ;
                          }
                          echo 
    "
                             <tr>
                           <td colspan='3' class='empty'>Total:</td>
                           <td class='total'>
                          "
    ;
                          
                      
    $total mysql_result(mysql_query("SELECT sum(quantity * product_cost) AS subtotal FROM cart WHERE session_id='" session_ID() . "'"),0);
                          
                      echo 
    "$" number_format($total,2) . "</td>
                             </tr>
                             </table>
                             
                          <p>If you are happy with your selection, please proceed to the
                          payment page by clicking on the checkout button.</p>
                             
                          <div class='submitrow'>
                           <a href='cart.php?action=empty'><input type='image' src='images/button-emptycart.gif' alt='Empty Cart'></a> <input type='image' src='images/button-updatecart.gif' alt='Update Cart'> <a href='checkout.php?session_id=" 
    session_ID() . "''><input type='image' src='images/button-checkout.gif' alt='Checkout'></a>
                             </div>
                         "

    Does anybody know what's going wrong? Why has it suddenly stopped working?
    Alex Graham
    Cadmium Design & Development
    www.cadmium.com.au
    alexandra@cadmium.com.au

  2. #2
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I can't see why the change I made would cause it to stop working
    That's because you have no debugging in there
    Try:
    PHP Code:
    if (!empty($_REQUEST['req'])) {
      
    //Change the quantity of products ordered
      
    $sql "UPDATE cart SET quantity = '" $_GET['qty'] . "' 
        WHERE cartitem_id = '" 
    $_GET['id'] . "' AND session_id = '" session_ID() . "'";
      echo 
    'DEBUG: '.$sql.'<br />';
      
    $updateitem mysql_query($sql) or die(mysql_error());

      if (isset(
    $_GET['qty']) && $_GET['qty'] == "0") {
        
    $sql "DELETE FROM cart WHERE cartitem_id = '" $_GET['id'] . "' AND session_id = '" session_ID() . "'";
        echo 
    'DEBUG: '.$sql.'<br />';
        
    $deleteitem mysql_query($sql) or die(mysql_error());
      }

    ..it should highlight what the problem is.

  3. #3
    SitePoint Evangelist Alexandra's Avatar
    Join Date
    Aug 2002
    Location
    Perth, Australia
    Posts
    414
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It comes back with:
    DEBUG: UPDATE cart SET quantity = '4' WHERE cartitem_id = '2' AND session_id = 'f5f8d3f85f31c37f44294a1aad9da0d2'
    It is using the wrong data. But why would it do that when I haven't changed anything in relation to that aspect of the program?
    Alex Graham
    Cadmium Design & Development
    www.cadmium.com.au
    alexandra@cadmium.com.au

  4. #4
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do a 'view source' on the form, if the values are incorrect in there then the problem must be in the form generation code, add some debugging to it to see.

  5. #5
    SitePoint Evangelist Alexandra's Avatar
    Join Date
    Aug 2002
    Location
    Perth, Australia
    Posts
    414
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, it seems it only works when there is one type of product in the cart. If you have two separate items, and you try to increase the quantity of one, it doesn't work. Why would that be?
    Alex Graham
    Cadmium Design & Development
    www.cadmium.com.au
    alexandra@cadmium.com.au


Bookmarks

Posting Permissions

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