dongye9071 2014-03-21 03:05 采纳率: 0%
浏览 78
已采纳

提交内部循环多个ID的按钮?(购物车)

I want to Create a Shoping Cart on PHp,

The Code is simple, When the customer fill the QTY and click button Add to cart, the code will save the PRoduct ID and Qty to a Cart Table. But the problem is that Form inside the Looping. And How Can I Get ID and Qty only from The button that Customer Click.

The Program look like this

enter image description here

And The Script Like This

<?php

    if(isset($_POST[ADD]))
        {
            $qty   = $_POST[QTY];
            $harga = $_POST[HARGA_ASLI]; 
            $id    = $_POST[ID];

            print_r($_POST);
        }

         $kolom = 3;

        $sql = "SELECT *,FORMAT(harga,0)AS harga_digit FROM item";

        $hasil = mysql_query($sql);
        echo "<form method=POST action=index.php>";
        echo "<table>
               <tr>";
              $i = 0;
              while($data=mysql_fetch_array($hasil))
              {
                 if($i >= $kolom)
                 {
                    echo "</tr><tr>";
                    $i = 0;
                 }          
                 $i++;
                 echo "<td align='center'><br><a href='detailBarang.php?ID=$data[ID]'><img src='$data[img]' width='200' height='150'/><br>$data[nama_produk]</a><br>
                         Rp. $data[harga_digit]<br> 
                         <input type='submit' name='ADD' id='ADD' value='Add to Cart'>
                         <input type='text' name='QTY' id='QTY' placeholder='Qty' /><br>
                         <input type='hidden' name='HARGA_ASLI' id='HARGA_ASLI' value='$data[harga]' /><br>
                         <input type='hidden' name='ID' id='ID' value='$data[ID]' />
                         <br></td>";


              }//end of while

        echo "<tr></table>";
        echo "</form>"; 

  ?>

If i fill the Qty and click Add to Cart, only the last Item can Post The Data.

How To Post The data Only for Customer Choose?

Im very Appreciated Your Answer.

Thanks

  • 写回答

2条回答 默认 最新

  • donglizhan7848 2014-03-21 04:13
    关注

    First, let's convert your MySQL to MySQLi. More explanation inside the comments /* */:

    <?php   
            $connection=mysqli_connect("YourHost","YourUsername","YourPassword","NameofYourDatabase");
    
    if(mysqli_connect_errno()){
    
    echo "Error".mysqli_connect_error();
    
    }
    
    $res=mysqli_query($con,"SELECT * FROM item");
    
    while($row=mysqli_fetch_array($res)){
    
    $nameofsubmitbutton=$row['ID'];
    
    if(isset($_POST[$nameofsubmitbutton])){
    
    $nameofproduct=$row['namaproduk'];
    $nameofnumbersubmitted=$nameofsubmitbutton."number";
    $quantity=$_POST[$nameofnumbersubmitted];
    
    if(empty($quantity)){
    echo "You wanted to buy a ".$nameofproduct."?<br>Type in a number so you can add it to your cart.";
    }
    
    else {
    mysqli_query($connection,"INSERT INTO yourTable ('','') VALUES ('$quantity','$nameofproduct')");
    echo "You bought ".$quantity." of ".$nameofproduct;
    }
    
    } /* END OF IF ISSET */
    
    } /* END OF WHILE LOOP $RES */
    
    $kolom = 3;
    
    $hasil = mysqli_query($connection,"SELECT *,FORMAT(harga,0) AS harga_digit FROM item"); /* YOU SURE WITH THIS QUERY? */
    
    echo "<form method=POST action=''>"; /* SUBMIT ON ITSELF */
    echo "<table><tr>";
    
    $i = 0; /* THIS WOULD ALSO SET AS YOUR COUNTER */
    while($data=mysqli_fetch_array($hasil))
    {
    
    $id=$data['ID'];
    
    if($i >= $kolom){
    
    echo "</tr><tr>";
    $i = 0;
    
    } /* END OF IF $i >= $KOLOM */   
    
    $i++;
    
    echo "<td align='center'><br><a href='detailBarang.php?ID=$data[ID]'><img src='$data[img]' width='200' height='150'/><br>".$data[nama_produk]."</a><br>Rp. ".$data[harga_digit]."<br>"; /* IF TO ECHO VARIABLES, USE ".$variable." */
    
    $numbername=$id."number";
    
    echo "<input type='number' name='$numbername' id='QTY' placeholder='Qty' /><br>"; /* CHANGE YOUR INPUT TYPE TO NUMBER */
    
    /* NO NEED FOR THE HIDDEN INPUT */
    
    echo "<input type='submit' name='$id' id='ADD' value='Add to Cart'></td>"; /* CHANGE THE NAME OF SUBMIT BUTTON TO THE CORRESPONDING ID FROM YOUR TABLE */
    
    
    
    } /* END OF WHILE LOOP */
    
    echo "<tr></table>";
    echo "</form>"; 
    
    ?>
    

    I tried it on my local computer. You should too.

    Here's a sample screen shot. enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据