Jump to content

Recommended Posts

I have transferred a cart script to another server and now it will only add the first item to the cart. When adding a second item to the cart it just changes the quantity of the first item to the new quantity that was added for the second item, and leaving only the first item in the cart. I suspect this might be due to depricated code, could you please take a look over the script and assist in updating it to non depricated code version.

 

the item add form

<tr><form method='post' action='<?php echo $PHP_SELF; ?>' target="_self" onsubmit="MM_validateForm('quantity','','NisNum');return document.MM_returnValue">
  <td colspan="2" align="center" class="presslink"><input name="id" type="hidden" value="<?php echo $selected; ?>"><input name="stockcode" type="hidden" value="<?php echo $itemcode; ?>"><input name="price" type="hidden" value="<?php echo $sellprice; ?>"><input name="basket" type="hidden" value="<?php echo $itemname; ?>">ORDER QUANTITY: <input name="quantity" style="vertical-align:middle;" type="text" id="quantity" value="1" size="5" maxlength="5"><br/><input style="vertical-align:middle;" type='submit' name='submit' id='submit' value='Order'></td></form>
  </tr>

 

the processing script

<?php session_start();
$basket = $_POST['basket'];
$itemprice = $_POST['price'];
$id = $_POST['id'];
$itemqty = $_POST['quantity'];
$code = $_POST['stockcode'];
$newprice = $itemprice*$itemqty;
if (isset($basket)){
   	if (isset($_SESSION["ses_basket_items"])) {
	// basket position
	$basket_position_counter=0;
	// double entry flag set to NO
	$double=0;
	// Check for existing basket name
	if ($ses_basket_items>0){
	   foreach ($ses_basket_name as $basket_item){
	      if ($basket_item==$basket){
	         // If exist flag for update
			 $double=1;
	         $basket_position=$basket_position_counter;
	      }
		  // Add new basket position
	      $basket_position_counter++;
	   }
	}
	// Update basket with new quantity and price
	if ($double==1){
	   $ses_basket_amount[$basket_position]=$itemqty;
	   $ses_basket_price[$basket_position]=$newprice;
	}else{
	   // If item not in basket ad new item
	   $ses_basket_name[]=$basket;
	   $ses_basket_amount[]=$itemqty;
	   $ses_basket_price[]=$newprice;
	   $ses_basket_stockcode[]=$code;
	   $ses_basket_id[]=$id;
	   $ses_basket_items++;
	}
}else{
	// Set Item Counter to 1 if no items
	$ses_basket_items=1;
	// Set variables for first session
	$ses_basket_name[0]=$basket;
	$ses_basket_amount[0]=$itemqty;
	$ses_basket_price[0]=$newprice;
	$ses_basket_stockcode[0]=$code;
	$ses_basket_id[0]=$id;
	// Registet the session variables
	session_register("ses_basket_items");
	session_register("ses_basket_name");
	session_register("ses_basket_amount");
	session_register("ses_basket_price");
	session_register("ses_basket_stockcode");
	session_register("ses_basket_id");
}
// Delete Item when set to 0
if ($itemqty == "0") {
	array_splice ($ses_basket_name, $basket_position, 1);
	array_splice ($ses_basket_amount, $basket_position, 1);
	array_splice ($ses_basket_price, $basket_position, 1);
	array_splice ($ses_basket_stockcode, $basket_position, 1);
	array_splice ($ses_basket_id, $basket_position, 1);
	$ses_basket_items--;
}
header("Location: ./prodshow.php?selected=$id");
}
?>

Link to comment
https://forums.phpfreaks.com/topic/148735-solved-rewrite-depricated-session-code/
Share on other sites

session_register() is depreciated.

Data added to a session should be assigned like:

 

$_SESSION['name'] = $x;

 

So in your case

session_register("ses_basket_items");

 

$_SESSION['ses_basket_items']

 

However you may be able to work around this by enabling global variables but this is not recomended. To do this add the following to an .htaccess file

php_flag register globals on

how do I incorporate the [0] part into the $_SESSION

 

like this

$_SESSION['ses_basket_items[0]']=$basket;

 

or like this

$_SESSION['ses_basket_items']=$basket[0];

 

where the previous code was

$ses_basket_name[0]=$basket;
session_register("ses_basket_name");

 

 

Mmm, I concur session_register() is deprecated.

You probably change the lines to

 

<?php
      // Register the session variables
      $_SESSION["ses_basket_items"] = $ses_basket_items;
      $_SESSION["ses_basket_name"] = $ses_basket_name;
      $_SESSION["ses_basket_amount"] = $ses_basket_amount;
      $_SESSION["ses_basket_price"] = $ses_basket_price;
      $_SESSION["ses_basket_stockcode"] = $ses_basket_stockcode;
      $_SESSION["ses_basket_id"] = $ses_basket_id;
?>

 

You will also need to intialise all the proper variables above.

Thank you for the help on the rewrite and all is done, but still was not working, I then realised that the person who entered the stock data had decided he did not need to name his items and just use stock codes and called everything -, the cart looks for a name then updates it, if it exists or adds it, if it doesnt. Since everything was called - it was only adding and updating the first thing put in the cart.

Fixed it by reffering to the unique product id stored in the db instead of the name.

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.