私はHTML、CSS、PHP、MySQLでプロジェクトを行っています。このプロジェクトは、ほとんどの電子商取引サイトであるオンライン野菜工場です。 PHPを使用してHTMLテーブル(私のカート)からMySQLデータベースにデータを格納する必要があります。表は次のようになります。 http://i.imgur.com/OngFBuP.pngPHPを使用してHTMLテーブルからMySQLデータベースにデータを格納する必要があります
「カート」のためのコードは次のとおりです。
'<?php
include("session.php");
include_once("config.php");
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>View shopping cart</title>
<link href="style/style.css" rel="stylesheet" type="text/css"></head>
<body>
<h1 align="center">View Cart</h1>
<div class="cart-view-table-back">
<form method="post" action="buy.php">
<table id="myTable" width="100%" cellpadding="6" cellspacing="0"> <thead><tr><th>Sl_no</th><th>Username</th><th>Quantity</th><th>Name</th> <th>Price</th><th>Total</th><th>Remove</th></tr></thead>
<tbody>
<?php
if(isset($_SESSION["cart_products"]))
{
$total = 0;
$fl=1;
$b = 0;
foreach ($_SESSION["cart_products"] as $cart_itm)
{
$product_name = $cart_itm["product_name"];
$product_qty = $cart_itm["product_qty"];
$product_price = $cart_itm["product_price"];
$product_code = $cart_itm["product_code"];
$subtotal = ($product_price * $product_qty);
$bg_color = ($b++%2==1) ? 'odd' : 'even';
echo '<tr class="'.$bg_color.'">';
echo '<td>'.$fl.'</td>';
echo '<td>'.$user.'</td>';
echo '<td><input type="text" size="2" maxlength="2"
name="product_qty['.$product_code.']" value="'.$product_qty.'" /></td>';
echo '<td>'.$product_name.'</td>';
echo '<td>'.$product_price.'</td>';
echo '<td>'.$subtotal.'</td>';
echo '<td><input type="checkbox" name="remove_code[]"
value="'.$product_code.'" /></td>';
echo '</tr>';
$fl++;
$total = ($total + $subtotal);
}
echo$fl;
$grand_total = $total;
}
?>
</tbody>
</table>
<button name="btn1" type="submit">buy</button>
<input type="hidden" name="return_url" value="<?php
$current_url =
urlencode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
echo $current_url; ?>" />
</form>
</div>
</body>
</html>'
そして私は、データベース内のすべてのデータを保存するためにPHPのページ(buy.php
)を使用しています。 buy.php
ためのコードは次のとおりです。
'<?php
include('session.php');
$con=new mysqli("localhost","root","","test");
$total=0;
foreach ($_SESSION["cart_products"] as $cart_itm)
{
$product_name = $cart_itm["product_name"];
$product_qty = $cart_itm["product_qty"];
$product_price = $cart_itm["product_price"];
$product_code = $cart_itm["product_code"];
$subtotal = ($product_price * $product_qty);
$total = ($total + $subtotal);
}
$sql="INSERT INTO cart (Uname,TotalCost,veg_id,quantity)
VALUES('$user','$total','$product_code','$product_qty')";
if($con->query($sql)==true){
echo"data inserted successfully";
}
echo"<br><br><a href='index_2.php'> Return to Your Dashboard</a>
<a href='buy_veg.php'> Buy More Items</a>";
?>'
問題は、このbuy.php
ページが実際にデータベースへの最後のレコードを格納していることです。画像リンク: http://i.imgur.com/ZaCnvjI.png
すべてのカートアイテムをデータベースに保存します。これを解決するために何をする必要がありますか?
あなただけのforeach内にすべての人に感謝 – JYoThI