0

I'm building e-commerce site, I have checkout page which is the same as cart page,
I want to get all products with their info (quantity selected, price, etc) and put it in 1 query in form to send everything to db.

I am searching for solution for 2 days now , but cant get the $_SESSION array value correctly printed and sent, and how to send multiple products?

this is the page that everything will be going here

the numbers does not update unless i refresh the page too, why?

<form action="#" method="post"> // rest of the form ...

    <button class="submit check_out checkout-right-basket">Delivery to this Address</button>
</form>
// I want when submitted to send everything in query to db this form in checkout page


// cart function that display the products in checkout

function cart()
{
    $total = 0;
    $item_quantity = 0;
    $i = 0;

    foreach ($_SESSION as $name => $value) {

        if ($value > 0) {

            if (substr($name, 0, 8) == "product_") {

                $length = strlen($name - 8);
                $id = substr($name, 8, $length);

                $result = query("SELECT * FROM products WHERE product_id = " . escape($id) . " ");

                while ($row = fetch_array($result)) {
                    $i++;
                    $sub = $row['product_price'] * $value;
                    $item_quantity += $value;

                    $product = <<<DELIMETER

        <tr class="rem1">
            <td class="invert">{$i}</td>
            <td class="invert-image">
                <a href="single.php">
                    <img src="{$row['product_image']}" alt=" " class="img-responsive">
                </a>
            </td>
            <td class="invert">
                <div class="quantity">
                    <div class="quantity-select">
                        <a href="../resources/cart.php?remove={$row['product_id']}"><div class="entry value-minus">&nbsp;</div></a>
                        <div class="entry value">
                            <span>{$value}</span>
                        </div>
                        <a href="../resources/cart.php?add={$row['product_id']}"><div class="entry value-plus active">&nbsp;</div></a>
                    </div>
                </div>
            </td>
            <td class="invert">{$row['product_title']} </td>

            <td class="invert">&#163;{$row['product_price']}</td>
            <td class="invert">
                <div class="rem">
                    <a href="../resources/cart.php?delete={$row['product_id']}"><div class="close1"> </div></a>
                </div>

            </td>
        </tr>


DELIMETER;
echo $product;

echo "<pre>";
echo $row['product_title'] . ' - ';
echo $_SESSION['item_quantity'] . ' pices';
echo "</pre>";
// this print wrong quantity for each product , howto print each product with its own quantity?

                }
                $_SESSION['items_total'] = $total += $sub;
                $_SESSION['item_quantity'] = $item_quantity;
            }

        }

    }

}


I expect everything in 1 page and when submitted send query to db and display it in admin area

Hope that was clear, Thanks

Xanonz
  • 52
  • 7
  • You need an array for multiple items, like `$_SESSION['cart'][0]['item_quantity']` etc – Lawrence Cherone Jun 24 '19 at 22:14
  • @Lawrence, Sorry I am kinda new, how to do so? can I get some kind of guild? – Xanonz Jun 24 '19 at 22:18
  • @LawrenceCherone I don't know how you would even implement that, but it would be a very stupid idea. Considering the naming `escape` it is probably some poor attempt at making the value valid to be injected into SQL. I really doubt that this would be a wrapper around prepare/execute. – Dharman Jun 24 '19 at 22:32
  • @Dharman I will turn all my queries to stmt but in the final step, now i want it to work only, can you give me solution? – Xanonz Jun 24 '19 at 22:38

1 Answers1

0

You should have to use ajax to update the value without loading the page. Use j Query Ajax for update on fly. Update data on a page without refreshing