就像库存系统一样,如果我输入我要购买的商品数量,必须在(products.xml)上扣除,同时将订购的产品保存在(orders.xml)上。它在orders.xml上保存但股票没有更新。
股票被扣除但只能在输入框中看到,但未在" products.xml" 图片:The Ordering Form 图片:Xml File is not updating
<?php
$products =simplexml_load_file("product.xml") or die("ERROR");
foreach ($products->product as $product){
if($product['id']==$_GET['id']){
$id = $product['id'];
$name =$product->name;
$price = $product->price;
$stocks = $product->stocks;
break;
}
}
if(isset($_POST['submitSave'])){
// $name = $_POST['name'];
// $id = $_POST['id'];
// $price = $_POST['price'];
$quan = $_POST['quan'];
$total = $price * $quan;
$stocks = $stocks - $quan; // on minusing the stocks :<
foreach ($products->product as $product){
if($product['id']==$_POST['id']){
// $product->name = $_POST['name'];
// $product->price = $_POST['price'];
$product->stocks = $_POST['stocks'];
break;
}
}
file_put_contents('product.xml',$products->asXML());
$xml = new DOMDocument("1.0","UTF-8");
$xml -> preserveWhiteSpace = TRUE;
$xml -> formatOutput = true;
if(file_exists("orders.xml")){
$xml = simplexml_load_file('orders.xml');
$order = $xml->addChild('order');
$order->addChild('id', $id);
$order->addChild('name', $name);
$order->addChild('price', $price);
$order->addChild('quan', $quan);
$order->addChild('total', $total);
$xml->saveXML("orders.xml");
$xml=simplexml_load_file("orders.xml") or die("ERROR");
$a=1;
foreach($xml->children() as $order)
{
echo"<div class = 'orderlist' ";
echo"<b>Order No.:". $a++."</b><br>";
echo"Product Id: ".$order->id."<br>";
echo"Product Name: ".$order->name."<br>";
echo"Price: ".$order->price."<br>";
echo"Quantity Ordered: ".$order->quan."<br>";
echo"Total Price: ".$order->total."<br>";
echo"</div><br>";
}
}
else{
$orders = $xml -> createElement("orders");
$xml -> appendChild($orders);
$order = $xml -> createElement("order");
$orders -> appendChild($order);
$id = $xml -> createElement("id", $id);
$order -> appendChild($id);
$name = $xml -> createElement("name", $name);
$order -> appendChild($name);
$price = $xml -> createElement("price", $price);
$order -> appendChild($price);
$quan = $xml -> createElement("quan", $quan);
$order -> appendChild($quan);
$total = $xml -> createElement("total", $total);
$order -> appendChild($total);
//echo "<xmp>" .$xml -> saveXML(). "</xmp>";
file_put_contents("orders.xml", $xml->saveXML());
//
$xml=simplexml_load_file("orders.xml") or die("ERROR");
$a=1;
foreach($xml->children() as $order)
{
echo"<div class = 'orderlist' ";
echo"<b>.Order No.:".$a++."</b><br>";
echo"Product Id: ".$order->id."<br>";
echo"Product Name: ".$order->name."<br>";
echo"Price: ".$order->price."<br>";
echo"Quantity Ordered: ".$order->quan."<br>";
echo"Total Price: ".$order->total."<br>";
echo"</div><br>";
}
}
// file_put_contents('product.xml',$products->asXML());
// header('location: index.php');
}
?>
<form method="post">
<br><center>
<table cellpadding="2" cellspacing="2">
<tr>
<td>Id</td>
<td><input type="text" name="id" value="<?php echo $id;?>" ></td>
</tr>
<tr>
<td>Name</td>
<td><input type="text" name="name" value="<?php echo $name;?>" ></td>
</tr>
<tr>
<td>Price</td>
<td><input type="number" name="price" value="<?php echo $price;?>" ></td>
</tr>
<tr>
<td>Stocks</td>
<td><input type="number" name="stocks" value="<?php echo $stocks;?>"></td>
</tr>
<tr>
<td>Quantity</td>
<td><input type="number" name="quan" value="" ></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="submitSave" value="Buy"></td>
</tr>
</table>