Page 1 of 1

need help to correct code for remove quantity of product

Posted: Sat Apr 16, 2011 4:40 am
by liyun88
hi,i need help to check and correct the code to remove quantity of the products..failed to remove quantity of products..
can anyone help me to check and correct the code??thanks in advance..

remove.php

Code: Select all

<?php
session_start();
require_once 'config.php' ;
 require_once 'application.php' ;
 
 $cartid = $_SESSION['cartid'];
 $id = $_SESSION['id'];
 $username = $_SESSION['username'];
 $pid=$_REQUEST['pid'];
 $pname=$_REQUEST['pname'];
 $uprice=$_REQUEST['uprice'];
 $exactQuantity=$_REQUEST['quantity'];
 
 $removeQuantity = 0;
 
        
     $query = 'SELECT quantity FROM cart WHERE id = "'. mysql_real_escape_string($id) . '" 
	 AND pid = "'. mysql_real_escape_string($pid) . '"';
  $result = mysql_query($query);   
       
        
        if ($data = mysql_fetch_object($result)){
          $exactQuantity = $data->quantity;
            if($removeQuantity > $exactQuantity){
			  echo"<script>alert(\"You only have" . $data->quantity . " quantity for this item.\")</script><script>window.location='viewCart.php?id=$id'</script>";
            }
            else if($removeQuantity < $exactQuantity){
              $newQuantity = 0;
              if($data1 = mysql_fetch_object($result)){
              $newQuantity = $data1->quantity;
              $newQuantity -= $removeQuantity;
              $query1 = 'UPDATE cart set quantity ="'.$newQuantity.'" WHERE id = "'. mysql_real_escape_string($id) . '" 
	 AND pid = "'. mysql_real_escape_string($pid) . '"';
	           $result1 = mysql_query($query1);   
             echo"<script>alert(\"Updated Successfully!\")</script><script>window.location='viewCart.php?id=$id'</script>";
              }
            }
          else{
            $query2 = 'DELETE FROM cart WHERE pid = "'. mysql_real_escape_string($pid) . '"';
			$result2 = mysql_query($query2);   
            echo"<script>alert(\"Deleted Successfully!\")</script><script>window.location='viewCart.php?id=$id'</script>";
                     
          }
        }
       ?>

Re: need help to correct code for remove quantity of product

Posted: Sat Apr 16, 2011 9:46 pm
by josh
Your code is poorly formatted & you fail to mention what its doing instead (syntax error, unexpected result, random behavior? blank screen? etc.) I want to help you but don't even know where to start honestly. Your code has dependencies, I can't execute it if I wanted to, so I rely on you to provide info (which you have not). What SESSION/GET/POST parameters are you passing? What observations can you make?

Re: need help to correct code for remove quantity of product

Posted: Sat Apr 16, 2011 10:10 pm
by liyun88
josh wrote:Your code is poorly formatted & you fail to mention what its doing instead (syntax error, unexpected result, random behavior? blank screen? etc.) I want to help you but don't even know where to start honestly. Your code has dependencies, I can't execute it if I wanted to, so I rely on you to provide info (which you have not). What SESSION/GET/POST parameters are you passing? What observations can you make?

sorry..i am beginner and not very good in php..it display blank screen..
actually i want remove quantity of the products in the shopping cart when the user view their cart and the user check buy the product too much,want to deduct the quantity..so i need to do remove function for them..$_session['id'] and other S_REQUEST['pid'] i need to take the user id and retrieve data from database..
so i try to write these code according my logic..
if the user insert quantity more than exact quantity in database,it will pop out you have only how many quantity in cart..
if the user insert quantity less than exact quantity in database,it can update their quantity by using remove function..
if the user insert quantity equal to exact quantity in database,it will delete the item in database..
i know these code is poorly formatted,so i need people help me to check and correct these code..
can you help me to correct them??thanks in advance..

Re: need help to correct code for remove quantity of product

Posted: Sun Apr 17, 2011 10:43 am
by josh
A blank screen usually means error reporting / display_errors settings are turned off, turn them on or check the error log....