Jump to content

Error when doing SHOPPING CART = php


thientanchuong

Recommended Posts

I have 3 pages which are test.php, testdetails.php and testupdateCart.php in my project http://yoongda.com/test.php

 

the layout of test.php looks like:

 

cart.jpg

 

and testdetails.php

 

cart1.jpg

 

and its coding:

 

<?php
   include'connection/connection.php';
   $pID = $_REQUEST["pID"];
   $pID = isset($_GET['pID']) ? $_GET['pID']:'';
      
      $query = 'SELECT * FROM product
            WHERE pID="'.mysql_real_escape_string($pID, $conn).'"';
      
      $result = mysql_query($query,$conn) or die (mysql_error($conn));
      
      if(mysql_num_rows($result) != 1)
      {
         header('Location: testdetails.php');
         mysql_free_result($result);
         mysql_close($conn);
         exit();
      }
      $row = mysql_fetch_assoc($result);
      extract($row);
   ?>
<!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>
<?php
echo'      <table width="100%" border="0">';
echo '    <tr>';
echo'       <td width="25%" rowspan="5"><img src="images/product/'.$pImage.'" alt="'.$pImage.'" height="200" width="150"/></a> </td>';
echo '       <td width="25%">Product ID:</td>';
echo '      <td width="50%"> <font face="Verdana" size="3">'.$pID.'</font></td>';
echo '     <tr>';
echo '      <td colspan="2"> <font face="Verdana" size="3"><b>'.$pName.'</b></font></td>';
echo '      </tr>';
echo '    <tr>';
echo '       <td colspan="2"> <font face="Verdana" size="2">'.$pDes.'</font></td>';
echo '      </tr>';
echo '    <tr>';
echo '       <td>Brand:</td>';
echo  '     <td> <font face="Verdana" size="3"><a href="brand.php?bID='.$bID.'">'.$bID.'</a></font></td>';
echo '    </tr>';
echo '    <tr>';
echo '      <td>Price:</td>';
echo '      <td> <font face="Verdana" size="3">£ '.$pPrice.'</font></td>';
echo '     </tr>';
echo '     </table>';
   ?>
<?php
echo '<form action="testupdateCart.php" method="post">';
echo '<table width="100%" border="0" align="center">';
echo   '<tr>';
echo  '   <td width="25%"> <input type="hidden" name="pID" value="'.$pID.'" /><label for="qty">Quantity: </label>';
echo '<input type="hidden" name="redirect" value="testdetails.php?'.'pID='.$pID.'" /> ';
$session = session_id;
$query = 'SELECT qty FROM ecomm_temp_cart
      WHERE session="'.$session.'" AND pID="'.$pID.'"';
$result = mysql_query($query,$conn) or die(mysql_error($conn));
if (mysql_num_rows($result) >0)
{
   $row = mysql_fetch_assoc($result);
   extract($row);
}else{
   $qty = 0;   
}
mysql_free_result($result);
echo '<input type="text" name="qty" id="qty" size="2" maxlength="2" value="'.$qty.'" />';
if ($qty > 0)
{
   echo ' <td style="padding-right:1px"><input type="image" id="cbi_cbmenu_5" src="menu_files/ebbtcbmenu5_0.gif" name="ebbcbmenu_5" width="103" height="26" border="0" alt="Update Quantity" value="Update Quantity" /></td>';
//   echo'<td><input type="submit" name="submit" value="Update Quantity" /></td>';
}else{
   echo '<td style="padding-right:1px"><input type="image" id="cbi_cbmenu_1" src="menu_files/ebbtcbmenu1_0.gif" name="ebbcbmenu_1" width="93" height="26" border="0" alt="Add To Basket" value="Add to Cart"/></td>';   
//   echo'<td><input type="submit" name="submit" value="Add to Cart" /></td>';
}
echo'</td>';
echo'<td><a href="product.php"><img id="cbi_cbmenu_2" src="menu_files/ebbtcbmenu2_0.gif" name="ebbcbmenu_2" width="94" height="26" border="0" alt="More Shopping" /></a> </td>';
echo  '    </tr>';
echo '   </table>';
echo '</form>';

?>
</body>
</html>

 

My ideal is user can update their wanted amount in the textfield then click on "Add to basket" button, the testupdateCart.php will contain "add to cart" function which redirect user back to testdetails.php page with user wanted quantity.

 

However, after clicking on "Add to basket" button,  it direct user to testupdateCart.php with nothing displaying

 

I have put some code to test all variable that are called.

 

cart2.jpg

 

and the coding in testupdateCart.php

 

<?php
   include'connection/connection.php';
   session_start();
   $session = session_id();
   
   $qty = (isset($_POST['qty']) && ctype_digit($_POST['qty'])) ? $_POST['qty']:0;
   $pID = (isset($_POST['pID'])) ? $_POST['pID']:'';
   $action= (isset($_POST['submit'])) ? $_POST['submit']:'';
   $redirect= (isset($_POST['redirect'])) ? $_POST['redirect']:'testdetails.php';
   
   switch ($action)
   {
      case'Add to Cart':
         if (!empty($pID) && $qty > 0)
            {
               $query = 'INSERT INTO ecomm_temp_cart
                        (session, pID, qty)
                        VALUES
                        ("'.$session.'","'.mysql_real_escape_string($pID, $conn).'",'.$qty.')';
               mysql_query($query, $conn) or die(mysql_error($conn));
            }
         header('Location:'.$redirect);
         exit();
         break;
         
      case 'Update Quantity':
         if (!empty($pID))
            {
               if ($qty >0)
                  {
                     $query ='UPDATE ecomm_temp_cart
                           SET qty='.$qty.'
                           WHERE session="'.$session.'" AND
                              pID="'.mysql_real_escape_string($pID, $conn).'"';
                  }else{
                     $query ='DELETE FROM comm_temp_cart
                           WHERE session="'.$session.'" AND
                              pID="'.mysql_real_escape_string($pID, $conn).'"';
                  }
                  mysql_query($query, $conn) or die(mysql_error($conn));
            }
            header('Location:'.$redirect);
            exit();
            break;
            
      case 'Empty Cart':
         $query = 'DELETE FROM ecomm_temp_cart
                  WHERE session="'.$session.'" AND
                     pID="'.mysql_real_escape_string($pID, $conn).'"';
         mysql_query($query, $conn) or die(mysql_error($conn));
         header('Location:'.$redirect);
         exit();
         break;
   }
?>
<?php
   echo'$qty is "'.$qty.'" and $pID is "'.$pID.'" and $action is "'.$action.'" and $redirect is "'.$redirect.'"';
?>

 

Please, help me figure out the problem

thanks

 

 

Link to comment
https://forums.phpfreaks.com/topic/184833-error-when-doing-shopping-cart-php/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.