将多行插入数据库

时间:2014-03-12 14:54:42

标签: php mysql shopping-cart

我们是一个研究小组,所以我们不幸被迫发明轮子。

我们正在建立一个简单的数据库系统,其中包含购物车功能和结帐,当接受销售时应该存储

我们遇到了一个问题:我们想发送我们的产品ID数量和每行的订单ID相同 - 到我们的数据库

我们完全不知道我们一直在寻找这个网站上的答案。但我们甚至都不知道女巫的方向。

数据显示为包含$ sub_id和$ quantity等不同变量的列表,每行中有一个订单ID。 例如,我们可以有多个sub_id' s + $数量,我们希望将它们全部发送到我们的数据库。

系统是MySQL PHP HTML。

这一切都在名为cart的会话中运行。

if($_SESSION['cart']) { //if the cart isn't empty
    //show the cart

    echo "<table id='overview' border=\"0\" padding=\"3\" width=\"100%\">"; //format the cart using a HTML table
    echo "<th>";

            echo "<th align=\"center\">Name</th>";
            echo "<th align=\"center\">color</th>";
            echo "<th align=\"center\">type</th>";
            echo "<th align=\"center\">price</th>";
            echo "<th align=\"center\">quantity</th>";
    echo "</tr>";

        //iterate through the cart, the $sub_id is the key and $quantity is the value
        foreach($_SESSION['cart'] as $sub_id => $quantity) {    

                    //use sprintf to make sure that $sub_id is inserted into the query as a number - to prevent SQL injection
            $sql = sprintf("SELECT * FROM sub_product 
            WHERE sub_id = %d;",
                            $sub_id); 

            $result = mysql_query($sql);

            //Only display the row if there is a product (though there should always be as we have already checked)
            if(mysql_num_rows($result) > 0) {

                list($sub_id, $sp_id, $sub_color, $sub_colorcode, $sub_type, $sub_price) = mysql_fetch_row($result);

                $line_cost = $sub_price * $quantity;        //work out the line cost
                $total = $total + $line_cost - $discount;
                $moms = $total * 0.20;  
                    //add to the total cost

    ?>

        <?php
                echo "<tr font-size=\"xx-large\">";
            //show this information in table cell
                    echo "<td align=\"center\">$sub_id</td>";
                    echo "<td align=\"center\">$sp_name</td>";
                    echo "<td align=\"center\">$sub_color</td>";
                    echo "<td align=\"center\">$sub_type</td>";
                    echo "<td align=\"center\">$sub_price</td>";
                    //along with a 'remove' link next to the quantity - which links to this page, but with an action of remove, and the id of the current product
                    echo "<td align=\"center\"><a href=\"$_SERVER[PHP_SELF]?action=remove&id=$sub_id\"> - </a>$quantity<a href=\"$_SERVER[PHP_SELF]?action=add&id=$sub_id\">+</a></td>";
                    echo "<td align=\"center\">$line_cost</td>";


                echo "</tr>";

            }

        }

如何进行?

很多时候提前感谢你! -

0 个答案:

没有答案