2017-08-08 32 views
-1

我正在实现服装购物网站。当我仅从购物车下订单最后一行项目时,我遇到问题HTML表格插入数据库而不是所有行。我希望当我点击下单按钮时,所有行值都应该插入到数据库中。以便顾客在购物车中选择的所有物品都应该放置在订单上。 下面是代码,Html表只有最后一行而不是所有行都插入mysql数据库

<table align="center" > 
     <thead> 
      <tr > 
      <th>Product</th> 
      <th>Price</th> 
      <th>QTY</th> 
      <th>SubTotal</th> 

      </tr> 
     </thead> 

     <?php 
       if (!empty($_SESSION["shopping_cart"])) 
       { 
        $total=0; 
        foreach ($_SESSION["shopping_cart"] as $keys => $values) { 

     ?> 

     <tr> 
      <td> 
      <p>Product Code:<?php echo $values["item_id"]; ?> <br/> 
      <?php echo $values["item_gender"]; ?><br/> 
    <?php echo $values["item_description"]; ?> </p></td> 

      <td>PKR 
<input type="number" name="price1" id="price1" value="<?php echo $values["item_price"];?>" readonly ></td> 

      <td><input type="number" name="qty[<?php echo $values["item_id"]; ?>]" id="qty" value="<?php echo $values["item_quantity"];?>"></td> 

      <input type="hidden" name="qty" value="<?php echo $values["item_quantity"];?>" > 

      <td >PKR<input type="number" name="total" id="total" value="<?php echo ($values["item_quantity"] * $values["item_price"]) ?>"readonly></td> 


     </tr> 

     <?php 
         $total=$total+($values['item_quantity']*$values["item_price"]); 
        /* $total=$total+$values["item_price"]; */ 
     } 
      } ?> 
     </table> <br/><br/> 

<button type="submit" name="submit" class="btn btn-primary btn-lg">Place Order</button> 

here is php code, 
if (isset($_POST['submit'])) 
      { 



         $product_code = $_POST['ID']; 

         $price = $_POST['grandtotal']; 

         $quantity = $_POST['qty']; 

         $description = $_POST['description']; 


          $email=$_SESSION["email"]; 



           $con=mysql_connect("localhost", "root", ""); 
         mysql_select_db("login",$con); 



       $qry="INSERT INTO order (order_description , product_code, order_quantity, order_price, customer_name, email, customer_id) VALUES ('$description', '$product_code', '$quantity', '$price', (SELECT name from users where email='$email'), '$email', (SELECT user_id from users where email='$email')) "; 

        $result=mysql_query($qry,$con); 
        if($result) 

        { 
     echo '<script>alert("Your order has been placed")</script>'; 
        echo '<script>window.location="portfolionew.php"</script>'; 
    } else { 
    die("Error While Adding Stock ! Please Try Again ."); 
    } 
     } 
+0

请删除JavaScript代码,只是混淆人没有JS在你的代码,也不是你面临的问题是JS有关。谢谢 –

+0

@N。伊万诺夫谢谢我删除它 –

+0

老实说 - 只有最后一行插入是你的问题最少;你正在使用一个陈旧的数据库连接器,其代码可以广泛应用于SQL注入攻击......以及嵌套查询中“INNER JOIN”会更好的问题是什么?我建议找一个最新的教程,然后重新开始。 – CD001

回答

-1
<table align="center"> 
    <thead> 
     <tr > 
     <th>Product</th> 
     <th>Price</th> 
     <th>QTY</th> 
     <th>SubTotal</th> 

     </tr> 
    </thead> 

    <?php 
      if (!empty($_SESSION["shopping_cart"])) 
      { 
       $total=0; 
       $i = 1; 
       foreach ($_SESSION["shopping_cart"] as $keys => $values) { 

    ?> 

    <tr> 
     <td style=" text-align: left;" > 
     <p style="margin-top: -120px; margin-left: 150px;">Product Code:<?php echo $values["item_id"]; ?> <br/> 
     <?php echo $values["item_gender"]; ?><br/> 
<?php echo $values["item_description"]; ?> </p></td> 

     <td style="text-align: left; width: 80px;">PKR<br/> 
<input style="width: 70px;" type="number" name="price<?= $i; ?>" id="price<?= $i; ?>" value="<?php echo $values["item_price"];?>" readonly ></td> 

     <td><input type="number" name="qty_<?= $i; ?>" id="qty_<?= $i; ?>" value="<?php echo $values["item_quantity"];?>"></td> 

     <td style="text-align: left; width: 80px;">&nbsp;&nbsp;<b>PKR</b> 
    <br/>&nbsp;<input style="width: 70px; " type="number" name="total" id="total" value="<?php echo ($values["item_quantity"] * $values["item_price"]) ?>"readonly></td> 


    </tr> 

    <?php 
        $total=$total+($values['item_quantity']*$values["item_price"]); 
        $i++; 
       /* $total=$total+$values["item_price"]; */ 
    } 
     } ?> 
    <input hidden="iTotalProducts" value="<?php echo $i; ?>" name="product_count"> 
    </table> <br/><br/> 

<button type="submit" name="submit" class="btn btn-primary btn-lg">Place Order</button> 

    //here is php code, 
     if (isset($_POST['submit'])) 
     { 




        $product_code = $_POST['ID']; 

        $price = $_POST['grandtotal']; 

        $quantity = $_POST['qty']; 

        $description = $_POST['description']; 


         $email=$_SESSION["email"]; 



          $con=mysql_connect("localhost", "root", ""); 
        mysql_select_db("login",$con); 

        $cnt = $_POST["iTotalProducts"]; 

        for($i = 1; $i <= $cnt; $i++) { 
         // here you can write your insert code 
        } 



       if($result) 

       { 
    echo '<script>alert("Your order has been placed")</script>'; 
       echo '<script>window.location="portfolionew.php"</script>'; 
} else { 
    die("Error While Adding Stock ! Please Try Again ."); 
    } 
    } `enter code here` 
+0

它不起作用 –

相关问题