Jump to content

array_key_exists() error


ragrim

Recommended Posts

Hi,

 

Im encountering this error when i add items to my cart, im using sessions to store my cart items in an array. this error only happens the first time i add something, then if i go clear my session it works and no errors.

 

Warning: array_key_exists() [function.array-key-exists]: The second argument should be either an array or an object in /home/byronhea/public_html/oos/mini-cart.php on line 18

 

Here is the line its refering to

 

 

	if(array_key_exists($pid, $_SESSION['cart']))

 

and here is the block of code it belongs to

 

	if(!isset($_SESSION)) 
{ 
session_start('cart');
}
// Process actions

$pid = $_POST['prodid'];
$q = $_POST['qty'];

if(array_key_exists($pid, $_SESSION['cart']))
{
	$_SESSION['cart'][$pid]=$_SESSION['cart'][$pid]+$q;	
}
ELSE
{
	$_SESSION['cart'][$pid]=$q;
}

 

Any help would be appreciated.

Link to comment
Share on other sites

i made a change to my code and have not recieved any errors as yet, but its hard to tell if i fixed it as this error is random and doesnt happen all the time.

 

i changed it to

 

if(isset($_SESSION['cart'][$pid]))
{
$_SESSION['cart'][$pid]=$_SESSION['cart'][$pid]+$q;	
}
else
{
$_SESSION['cart'][$pid]=$q;
}

 

I was reading in the php manual that using isset may not be the right way to go about this but its the only way i could avoid the error as any other way seems to produce random errors.

 

Any help would be appreciated.

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.