存储到PHP会话中的对象获取了错误的存储值 [英] The object stored into a PHP session gets the wrong value stored
问题描述
我创建了一个有效的购物车对象.但是,当我尝试将对象保存到会话中时,该对象的内容存储错误.保存的值是从购物车中清除对象后的值,即空的.
I have created a shopping cart object that works. But when I try to save the object into the session the wrong content of that object is stored. The value that is being saved is the value after the object is cleared from the shopping cart, the empty one.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Testing the Shopping Cart</title>
</head>
<body>
<?php # cart.php
// This script uses the ShoppingCart and Item classes.
//error_reporting(0);
// Create the cart:
session_start();
try {
require('ShoppingCart.php');
require('userMenu.php');
$cart = new ShoppingCart();
// Create some items:
require('Item.php');
require ('Connect.php');
$conn=Connect::doConnect();
$query = "SELECT product_id, product_name, product_price from 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 some quantities:
$cart_items_new = array_combine($_POST['item_adjust'],$_POST['quantity']);
foreach ($cart_items_new as $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 from product where
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 the cart contents:
echo '<h2>Continutul cosului de cumparaturi (' . count($cart) . ' tipuri de produse)</h2>';
echo "The user is " . $_SESSION["user"] . ".<br>";
echo "User type is " . $_SESSION["user_type"] . ".";
$new_cart = unserialize(serialize($cart));
if (!$cart->isEmpty()) {
foreach ($cart as $arr) {
// Get the item object:
$item = $arr['item'];
// Print the 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 the number ";
$serialize_cart=serialize($new_cart);
$_SESSION["cart"]=$serialize_cart;
}
}
catch (Exception $e) {
}
?>
</body>
</html>
当我按下第二个提交按钮时,我做错了.
What I am doing wrong when I push the second submit button.
推荐答案
嘿,我已经尝试过了 我无法测试它,因为我没有其他文件和购物车对象,但它应该几乎没有错误
Hey I've had a go at this I can't test it as I dont have the additional files and cart object but it should be close to error free
我有一个Session变量"cart"(如果存在),我们将其反序列化,然后完成操作,然后可以编辑值并将其保存回去,如此
I've got a Session variable 'cart' if present we grab it unserialize and were done then can edit the values and save it back out so on
如果不存在,即删除了第一个匹配项或购物车,我们会从数据库中构建一个新的购物车(这不是理想的测试方法,因为目前您将数据库中的每个商品都添加到了购物车中?)
If not present i.e. first hit or cart was deleted we build a new cart from the database (This isnt ideal just for testing as presently your adding every item from the database to the cart?)
如果存在adjQ的post或get值,我们将修改cart对象的某些值并将其保存回会话变量中
If the post or get value of adjQ is present we modify some of the values of the cart object and save it back out to the session variable
如果显示showCart的帖子或获取值,我们将输出当前购物车 为此,您可能需要调整Shopping_Cart对象以支持所调用的变量以及getCount函数和getAllRows函数
If the post or get value of showCart is present we output the current cart To make this work you might have to tweak your Shopping_Cart Object to support the variables being called and the getCount function and the getAllRows function
我已从购物车(w)中删除了一系列物品的额外存储空间,不知道那是为了什么,因为您已将数据存储在对象中,不需要复制它
I've removed the additional storage of an array of the items from the cart (w) not sure what thats for since you have the data stored in the object dont need to replicate it
应对所有请求变量进行清理,以防止注入攻击等
All the request variables should be sanitized to prevent injection attacks n so on
我添加了一个隐藏字段来触发showCart请求
I've added a hidden field to trigger the showCart request
无论如何希望这对您有帮助
Anyway hope this helps
<?php
session_start();
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Testing the Shopping Cart</title>
</head>
<body>
<?php # cart.php
// This script uses the ShoppingCart and Item classes.
//error_reporting(0);
// Create the cart:
require('ShoppingCart.php');
require('userMenu.php');
$rowCount = 0;
if(isset($_SESSION['cart']))
{
echo "We have a stored cart in a Session variable, retrieving data ...";
$cart = unserialize($_SESSION["cart"]);
$rowCount = $cart->getCount();
}
else
{
$cart = new ShoppingCart();
// Create some items:
require('Item.php');
require ('Connect.php');
$conn=Connect::doConnect();
$query = "SELECT product_id, product_name, product_price from 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 some quantities:
$cart_items_new = array_combine($_POST['item_adjust'], $_POST['quantity']);
foreach ($cart_items_new as $product_id=>$quantity) {
if($quantity > 0) {
$cart->updateItem($product_id, $quantity);
$conn=Connect::doConnect();
$query1 = "SELECT product_id, product_name, product_price from product where 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 the cart contents:
echo '<h2>Continutul cosului de cumparaturi (' . $rowCount . ' tipuri de produse)</h2>
The user is ' . $_SESSION["user"] . '.<br>
User type is ' . $_SESSION["user_type"] . '.';
if (!$cart->isEmpty()) {
foreach ($cart as $arr) {
// Get the item object:
$item = $arr['item'];
// Print the 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 to Session variable";
//New_cart is only set in adjQ request prehaps this code should be 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() as $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 is empty";
}
}
?>
</body>
</html>
这篇关于存储到PHP会话中的对象获取了错误的存储值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!