Hello
I have a problem with a shopping cart . The following script receives data from the product page and adds it to the basket by putting it in an array.
<?
session_start();
if(isset($_POST['add']))
{
$_SESSION['prod_id'][] =$_POST['prod_id'];
$_SESSION['author'][] =$_POST['autor'];
$_SESSION['title'][] =$_POST['title'];
$_SESSION['price'][] =$_POST['price'];
}
// each time a product is added , the values for the product are stored in an array and printed with a for loop
for($i=0; $i<count($_SESSION['prod_id']); $i++)
{
echo $_SESSION['author'][$i];
echo $_SESSION['title'][$i];
echo $_SESSION['price'][$i];
}
?>
This works just fine on localhost (PHP Version 5.1.2) on my comp but when i upload it in an outside server
like tripod (PHP Version 4.3.2) I get the following error : Fatal error: [] operator not supported for strings
reffering to the line where i store the first variable into the array - $SESSION['prod_id'][] =$POST['prod_id'];
This error occurs when i add a second product to the basket .
I've search on google and I understand that the problem is that when i load the page again the variable has some string value instead of being empty. I've put this code at the beggining of the script
if ( !is_array($_SESSION['prod_id'] ) ) { unset($_SESSION['prod_id']); }
if ( !is_array($_SESSION['author'] ) ) { unset($_SESSION['autor'] ); }
if ( !is_array($_SESSION['title'] ) ) { unset($_SESSION['title']); }
if ( !is_array($_SESSION['price'] ) ) { unset($_SESSION['price'] ); }
and some strange things happend. The script seems to run corectly for some time (while I add products , delete them , destroy the session etc) and then somehow it crashes - some of the variables are not printed anymore in the for loop.
I have done some testing and the results are the following :
- author and title crash after a while
- strangely price and prod_id seem to work well
I hope I was explicit enough and I really hope you could help me cause this is dirving me crazy.
Thank you.