2017-05-02 31 views
-2

私は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

すべてのカートアイテムをデータベースに保存します。これを解決するために何をする必要がありますか?

+1

あなただけのforeach内にすべての人に感謝 – JYoThI

答えて

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')"; 

あなたの挿入クエリはforeach()の外にある、とループ内の変数は各反復に上書きされます。彼らはそれの最後の価値を保持し、あなたはそれだけを挿入していることを意味します。

+0

をクエリ実行部を移動させるように、各反復に値を挿入する必要があります。私の問題は解決されました。あなたの助けがなければ私は私のプロジェクトを完了できませんでした。私のコードを修復してくれてありがとう.. –

0

各繰り返しで値を挿入するだけで、クエリ実行部分をforeach内に移動する必要があります。

注:最後の値を格納するのはなぜですか。 foreachは各反復で変数を上書きするので、最後の反復値は変数値に設定され、最後の反復値のみを格納します。

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"; 
      } 
} 
関連する問題