Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't increment product's quantity in session object
    primarykey
    data
    text
    <p>I created an object, it's named carwash. I created a session and assigned that object for session, if I enter quantity then and press buy button, I have result (example 6):</p> <blockquote> <blockquote> <p>Your shopping cart contains 6 items.</p> </blockquote> </blockquote> <p>but when I enter nothing, I get:</p> <blockquote> <blockquote> <p>Your shopping cart contains items.</p> </blockquote> </blockquote> <p>How should I do? Thank you! Here is all my code:</p> <p>index.php <br /> PHP Code:</p> <pre><code>&lt;?php session_start(); require_once 'carwash.php'; if (isset($_SESSION['encoded_cartopass'])) { // first let's get the variable from the session $encoded_cartopass = $_SESSION['encoded_cartopass']; // now let's unpack it $cartopass = unserialize($encoded_cartopass); // echo quantity echo "Your shopping cart contains {$cartopass-&gt;getQuantity()} items. &lt;br /&gt;&lt;br /&gt;"; } else { echo "Your shopping cart contains 0 items. &lt;br /&gt;&lt;br /&gt;"; } ?&gt; &lt;form action="process.php" method="post"&gt; Quantity: &lt;input type="text" name="quantity" id="quantity"/&gt; &lt;input type="submit" value="Buy" name="submit" /&gt; &lt;/form&gt; </code></pre> <p>process.php <br /> PHP Code:</p> <pre><code>&lt;?php require_once 'carwash.php'; if (isset($_POST['submit'])) { if (!isset($_SESSION['encoded_cartopass'])) { // construct and set quantity $cartopass = new carwash(); $cartopass-&gt;setQuantity($_POST['quantity']); // construct and encode session session_register('encoded_cartopass'); $_SESSION['encoded_cartopass'] = serialize($cartopass); } else { // if session is existing, decode it and // increment quantity $encoded_cartopass = $_SESSION['encoded_cartopass']; $cartopass = unserialize($encoded_cartopass); $cartopass-&gt;incrementQuantity($_POST['quantity']); // encode class and assign to session and // session is used pass to index.php $_SESSION['encode_cartopass'] = serialize($cartopass); } } echo "&lt;script&gt;window.location.href='index.php'&lt;/script&gt;"; ?&gt; </code></pre> <p>carwash.php <br /> PHP Code:</p> <pre><code>&lt;?php class carwash { private $carmake; private $caryear; private $quantity; public function getCarmake() { return $this-&gt;carmake; } public function setCarmake($carmake) { $this-&gt;carmake = $carmake; } public function getCaryear() { return $this-&gt;caryear; } public function setCaryear($caryear) { $this-&gt;caryear = $caryear; } public function getQuantity() { return $this-&gt;quantity; } public function setQuantity($quantity) { $this-&gt;quantity = $quantity; } public function incrementQuantity($quantity = '') { if (empty($quantity)) { $this-&gt;quantity++; } else { $this-&gt;quantity += $quantity; } } public function washcar() { echo "scruba, dub, dub, scruba, dub, dub &lt;br /&gt;"; echo "I'm feelling cleaner, Thank you!"; } } ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload