0

I have this code bellow:

<?php
if(isset($_SESSION["products"])) {
    $total = 0;
    echo '<form action="cart-post-config.php" enctype="multipart/form-data" method="POST">>';
    foreach ($_SESSION["products"] as $cart_itm) {
                    echo '<tr>';
                    echo '<td></td>';
                    echo '<td><input type="text" name="product_name[]" value="'.$cart_itm["name"].'"/></td>';
                    echo '<td><input type="text" name="product_price[]" value="'.$cart_itm["price"].'"/></td>';
                    echo '<td><input type="text" name="product_quantity[]" value="'.$cart_itm["qty"].'"/></td>';                            
                    $totalPerEach = ($cart_itm["price"]*$cart_itm["qty"]);

                    echo '<td><input type="text" name="product_eachTotal[]" value="'.$totalPerEach.'"/></td>';
                    echo '<td>View Save Delete</td>';
                    echo '</tr>';
    }
    echo '<input type="submit" name="submit" />';
    echo '</form>';//close the form
}
else {
    echo 'Your Cart is empty'; }
?>

The code above is used to collect the cart information of the member, where the information that has been collected are putted to the input of the form that will be posted to the database.

This is the config:

if($_SERVER["REQUEST_METHOD"] == "POST")
    {
        for($i=0; $i<count($_POST['product_name']);$i++)
            {
                $product_name=$_POST['product_name'][$i];
                $product_price=$_POST['product_price'][$i];
                $product_quantity=$_POST['product_quantity'][$i];
                $product_eachTotal=$_POST['product_eachTotal'][$i];

        mysql_query("insert into member_cart (cart_code, product_cart_name, product_cart_price, product_cart_quantity, total_cart_price) 
                    values(0, '$product_name',  '$product_price', '$product_quantity', '$product_eachTotal')");
            }
    }

The config above will be read the form that will be posted where will loop the row of eahc products that have been added to the chart.

My problem now is after posting the form, the row of product_cart_quantityand the row of total_cart_price' only got the text/data ofArray` in the database.

Please help I don't have any idea what I am doing wrong here.

Thanks

Riad
  • 3,822
  • 5
  • 28
  • 39
Al Kasih
  • 887
  • 5
  • 24
  • Omg,I am sorry. I forgot to edit the product_price. – Al Kasih Oct 29 '14 at 09:00
  • You [shouldn't use mysql_* functions in new code](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use PDO or MySQLi. – Daniel Gelling Oct 29 '14 at 09:01
  • What do you mean how to get the total? you have shown me how to. – Al Kasih Oct 29 '14 at 09:05
  • @Arif_suhail_123 can I have your facebook acount? I need to have more friends who are programmers like you to help me out with my programming studying. – Al Kasih Oct 29 '14 at 09:08
  • Thanks, you can delete the link now. I am sorry about that. I have not checked it.I have to finish an ecommerce project now. When one thing is solved, I will move to another matter. I am sorry. But I will check it soon. – Al Kasih Oct 29 '14 at 09:12

1 Answers1

0

You have set $product_price to 3 different values which ends up being product_eachTotal. Following is the corrected code.

if($_SERVER["REQUEST_METHOD"] == "POST") {
    for($i=0; $i<count($_POST['product_name']);$i++) {

        $product_name=$_POST['product_name'][$i];
        $product_price=$_POST['product_price'][$i];
        $product_quantity=$_POST['product_quantity'][$i];
        $product_eachTotal=$_POST['product_eachTotal'][$i];

        mysql_query("insert into member_cart (cart_code, product_cart_name, product_cart_price, product_cart_quantity, total_cart_price) 
                     values(0, '$product_name',  '$product_price', '$product_quantity', '$product_eachTotal')");
    }
}
Kraang Prime
  • 9,981
  • 10
  • 58
  • 124