The object stored into a PHP session gets the wrong value stored -


i have created shopping cart object works. when try save object session wrong content of object stored. value being saved value after object cleared shopping cart, empty one.

    <!doctype html> <html lang="en"> <head>  <meta charset="utf-8">  <title>testing shopping cart</title> </head> <body> <?php # cart.php // script uses shoppingcart , item classes. //error_reporting(0); // create cart: session_start(); try {  require('shoppingcart.php'); require('usermenu.php'); $cart = new shoppingcart();  // create items: require('item.php'); require ('connect.php');  $conn=connect::doconnect();  $query = "select product_id, product_name, product_price product"; $result = mysqli_query($conn, $query); $i=0; $w = array(); $new_cart; if ($result->num_rows > 0) {     // output data of each row     echo '<table border='."1".'><form action="cart.php" method="post"><tr><td>';     echo '<b>'."id produs".'</td><td><b>'."denumire".'</td><td><b>'."pret".'</td><td>'."numar de bucati solicitate".'</td></tr><tr>';     while($row = $result->fetch_assoc()) {         echo '<td>'.$row["product_id"].'</td><td>'. $row["product_name"].'</td><td>'. $row["product_price"]. '</td><td>             <input type="input" value="0" name="quantity[]"><input type="hidden" value="'.$row["product_id"].'" name="item_adjust[]"></td>';         echo '</tr>';         $i++;         $w[$i]=new item($row["product_id"], $row["product_name"],$row["product_price"]);         $cart->additem($w[$i]);         //$cart->deleteitem($w[$i]);          }     echo '</td></tr><tr><td colspan="3"><input type="submit" value="adauga in cosul de cumparaturi" name="adjq"></td></tr></table>';     //foreach ()  } else {     echo "0 results"; } $conn->close();  if($_post["adjq"]){  echo "in stoc avem ".$i." tipuri de produse";  // update quantities: $cart_items_new = array_combine($_post['item_adjust'],$_post['quantity']); foreach ($cart_items_new $product_id=>$quantity){     //$item=new item($product_id,item->);     //item $it;     //->updateitem($item->getid($product_id), $qty);     //$cart->updateitem(getid($product_id), $quantity);      $conn=connect::doconnect();      $query1 = "select product_id, product_name, product_price product      product_id='$product_id'";     $result1 = mysqli_query($conn, $query1);     $row1=mysqli_fetch_array($result1);      if($quantity>0){      $cart->updateitem($w[$product_id], $quantity);         echo $product_id.$quantity."+".$row1["product_name"];     }     else{         $cart->deleteitem($w[$product_id]);     }   }   // show cart contents: echo '<h2>continutul cosului de cumparaturi (' . count($cart) . ' tipuri de produse)</h2>'; echo "the user " . $_session["user"] . ".<br>"; echo "user type " . $_session["user_type"] . "."; $new_cart = unserialize(serialize($cart)); if (!$cart->isempty()) {      foreach ($cart $arr) {          // item object:         $item = $arr['item'];          // print item:         printf('<p><strong>%s</strong>: %d @ $%0.2f bucata.<p>', $item->getname(), $arr['qty'], $item->getprice());      } // end of foreach loop!  } // end of if. }  echo '</td></tr><tr><td colspan="2"><input type="submit" value="salveaza" name="session"></td><td></td></tr></form></table>'; if ($_post['session']) {     echo "you entered number ";     $serialize_cart=serialize($new_cart);     $_session["cart"]=$serialize_cart;  }  }   catch (exception $e) {  } ?> </body> </html> 

what doing wrong when push second submit button.

hey i've had go @ can't test dont have additional files , cart object should close error free

i've got session variable 'cart' if present grab unserialize , done can edit values , save out on

if not present i.e. first hit or cart deleted build new cart database (this isnt ideal testing presently adding every item database cart?)

if post or value of adjq present modify of values of cart object , save out session variable

if post or value of showcart present output current cart make work might have tweak shopping_cart object support variables being called , getcount function , getallrows function

i've removed additional storage of array of items cart (w) not sure thats since have data stored in object dont need replicate it

all request variables should sanitized prevent injection attacks n on

i've added hidden field trigger showcart request

anyway hope helps

<?php     session_start(); ?> <!doctype html> <html lang="en">     <head>         <meta charset="utf-8">         <title>testing shopping cart</title>     </head>     <body> <?php # cart.php // script uses shoppingcart , item classes. //error_reporting(0);      // create cart:     require('shoppingcart.php');     require('usermenu.php');      $rowcount = 0;      if(isset($_session['cart']))     {         echo "we have stored cart in session variable, retrieving data ...";          $cart = unserialize($_session["cart"]);          $rowcount = $cart->getcount();     }     else     {         $cart = new shoppingcart();          // create items:         require('item.php');         require ('connect.php');          $conn=connect::doconnect();          $query = "select product_id, product_name, product_price product";         $result = mysqli_query($conn, $query);          $rowcount = $result->num_rows;          if ($result->num_rows > 0) {             // output data of each row             while($row = $result->fetch_assoc()) {                 $cart->additem(new item($row["product_id"], $row["product_name"],$row["product_price"]));             }         }         $conn->close();     }      if(isset($_request['adjq']))     {         echo "in stoc avem ".$rowcount." tipuri de produse";          // update quantities:         $cart_items_new = array_combine($_post['item_adjust'], $_post['quantity']);         foreach ($cart_items_new $product_id=>$quantity) {             if($quantity > 0) {                 $cart->updateitem($product_id, $quantity);                  $conn=connect::doconnect();                  $query1 = "select product_id, product_name, product_price product product_id='$product_id'";                 $result1 = mysqli_query($conn, $query1);                  $row1 = mysqli_fetch_array($result1);                 echo $product_id." ".$quantity." + ".$row1["product_name"];             }             else {                 $cart->deleteitem($product_id);             }         }          // show cart contents:         echo '<h2>continutul cosului de cumparaturi (' . $rowcount . ' tipuri de produse)</h2>         user ' . $_session["user"] . '.<br>         user type ' . $_session["user_type"] . '.';          if (!$cart->isempty()) {             foreach ($cart $arr) {                 // item object:                 $item = $arr['item'];                 // print item:                 printf('<p><strong>%s</strong>: %d @ $%0.2f bucata.<p>', $arr['item']->getname(), $arr['item']->getquantity(), $arr['item']->getprice());             } // end of foreach loop!              echo "saving card session variable";             //new_cart set in adjq request prehaps code should there?             $_session["cart"] = serialize($cart);         } // end of if.     }      if(isset($_request['showcart']))     {         if ($cart->getcount() > 0) {             // output data of each row             echo '<table border='."1".'><form action="cart.php" method="post">';             echo '<tr><td><b>'."id produs".'</td><td><b>'."denumire".'</td><td><b>'."pret".'</td><td>'."numar de bucati solicitate".'</td></tr>';             foreach ($cart->getallrows() $row) {                 echo '                     <tr>                         <td>'. $row->getproductid() . '</td>                         <td>'. $row->getname() . '</td>                         <td>'. $row->getprice() . '</td>                         <td><input type="input" value="0" name="quantity[]"><input type="hidden" value="' . $row->getproductid() . '" name="item_adjust[]"/><input type="hidden" value="showcart" name="showcart"/></td>                     </tr>';             }             echo '<tr><td colspan="3"><input type="submit" value="adauga in cosul de cumparaturi" name="adjq"></td></tr></table>';         } else {             echo "cart empty";         }      } ?> </body> </html> 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -