Jump to content

$_SESSION field will not update


Recommended Posts

I have a problem where the session field quantity is not updating when the same entity key (id) is added to the session. Instead of quantity being updated, another identical entity is being added to the session.

I am TOTALLY out of clues on this one! Does anyone have any idea what is causing this to happen?

 

<?php
session_start();
if ($_SESSION['cart']['content']['id'] == $_POST['id']) {
	$_SESSION['cart']['content']['$_POST[id]']['quantity'] = $_SESSION['cart']['content']['$_POST[id]']['quantity'] + $_POST['quantity'];
} else {
	$_SESSION['cart']['content'][] = array ('id' => $_POST['id'], 'size' => $_POST['size'], 'quantity' => $_POST['quantity']);
}
echo '<DIV class="result">Added to cart.</DIV>';
?>

Link to comment
Share on other sites

http://php.net/string

 

More specifically this piece right here: '$_POST[id]'.  That will always return the same value.  if you echo it you will see that it is saying $_POST[id] exactly how you have it.

 

<?php
session_start();
if ($_SESSION['cart']['content']['id'] == $_POST['id']) {
	$_SESSION['cart']['content'][$_POST['id']]['quantity'] = $_SESSION['cart']['content'][$_POST['id']]['quantity'] + $_POST['quantity'];
} else {
                            /* You were just appending it to the array but you are doing a search for it above so you need the actual id as the index */
	$_SESSION['cart']['content'][$_POST['id']] = array ('id' => $_POST['id'], 'size' => $_POST['size'], 'quantity' => $_POST['quantity']);
}
echo '<DIV class="result">Added to cart.</DIV>';
?>

 

~juddster

Link to comment
Share on other sites

$_POST[id] will give you a warning (if you have error reporting displaying them) because it has to automagically convert id to 'id'.  If you have a $_POST'd value that has a space or anything in it (i.e. $_POST [ 'foo bar' ]) then it won't work.

 

You should always use the quotes rather than not because it is incredibly slower (about 4 times).

 

~juddster

Link to comment
Share on other sites

Unfortunately, I spoke too soon. Now another problem has developed.

 

If I add an entity with a quantity of 1, and then add the same entity with a quantity of 1, I get a single entity with a quantity of 2, however,

If I add an entity with a quantity of 1, and then add the same entity with a quantity of 3, I get two entities - entity A/quantity-1 & entity A/quantity-3.

 

Why is this happening?

 

http://i52.tinypic.com/30bgtww.png

 

<?php
session_start();
if (isset($_SESSION['cart']['content'][$_POST['id']])) {
	$_SESSION['cart']['content'][$_POST[id]]['quantity'] += $_POST['quantity'];
} else {
	$_SESSION['cart']['content'][] = array ('id' => $_POST['id'], 'size' => $_POST['size'], 'quantity' => $_POST['quantity']);
}
echo '<DIV class="result">Added to cart.</DIV>';
echo $_SESSION['cart']['content'][$_POST['id']]['quantity'];
?>

Link to comment
Share on other sites

Never mind. I have re-wrote the way entities are stored in the session. They are now stored as:

$_SESSION['cart']['content'][$_POST[id]][$_POST[size]] = array ('quantity' => $_POST['quantity'])

 

opposed to:

$_SESSION['cart']['content'][] = array ('id' => $_POST['id'], 'size' => $_POST['size'], 'quantity' => $_POST['quantity'])

 

EDIT: Well, this is flawed. But I will figure it out.

Link to comment
Share on other sites

Because you still just appending to the end of the array when you should be doing as I said and:

 

$_SESSION['cart']['content'][$_POST['id']] = array ('id' => $_POST['id'], 'size' => $_POST['size'], 'quantity' => $_POST['quantity']);

 

~juddster

Link to comment
Share on other sites

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.