Jump to content

$_SESSION variables producing Warnings!


akshay

Recommended Posts

Hello.

 

My codes for these pages produce errors:

 

array.php errors:

 

Warning: array_merge() [function.array-merge]: Argument #1 is not an array in D:\wamp\www\test\session\array.php on line 12

 

Warning: array_unique() [function.array-unique]: The argument should be an array in D:\wamp\www\test\session\array.php on line 12

 

array_read.php errors:

 

Warning: Invalid argument supplied for foreach() in D:\wamp\www\test\session\array_read.php  on line 12

 

Basically, I'm trying to add products to cart and display them. Simple! Then, why so many errors?

 

Also, I didnt understand what serialize() and unserialize() are for, despite referring to php.net manual

 

My coding:

 

array.php:

<?php
session_start();


//Turn off Notices
$ebits = ini_get('error_reporting');
error_reporting($ebits ^ E_NOTICE);
//Notices Turned OFf

if(isset($_POST['form_products'])) {
if(!empty($_POST['form_products']))  {
$products=array_unique(array_merge(unserialize($_SESSION['products']),$_POST['form_products']));
}
$_SESSION['products']=serialize($products);
print "Your products have been saved!";
}
?>

<html>
<body>
<form method="post" action="<?php print $_SERVER[php_SELF] ?>">;
<p>Select some products:</p>

<select name="form_products[]" multiple size=3>
<option>iBall Li'l Book 1710</option>
<option>Lenovo S10 Netbook</option>
<option>ASUS Netbook</option>
</select><br><br>
<input type="submit" value="Add to Cart"></form><br>
<a href="array_read.php">Array Read</a>
</body></html>

 

array_read.php :

 

<?php

//Turn off Notices
$ebits = ini_get('error_reporting');
error_reporting($ebits ^ E_NOTICE);
//Notices Turned OFf

session_start();

if(isset($_SESSION['products']) && !empty($_SESSION['products'])) {
echo "Your cart:<ol>";
foreach($_SESSION['products'] as $p) {
print "<li>$p</li>";
}
print "</ol>";
}
echo '<br><br><a href="/test/session/array.php">Back to Product Choices</a>';


?>

 

Thanks a lot! :)

Akshay

Link to comment
https://forums.phpfreaks.com/topic/193669-_session-variables-producing-warnings/
Share on other sites

Try Changing this

if(!empty($_POST['form_products']))  {
$products=array_unique(array_merge(unserialize($_SESSION['products']),$_POST['form_products']));
}

 

to

if(!empty($_POST['form_products']))  {
$newnew= array_merge(unserialize($_SESSION['products']),$_POST['form_products']);
$newnew = array();
$products=array_unique($newnew);
}

 

Not sure but try :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.