php shopping cart- [checkout]

4000 views php
-4


Iam a Beginer of PHP.
I want insert data from cart into database.
I make a hidden form.But it insert only last Data, Not All data !!
Please Help Me
Here is my code : .

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

?>

<form action="" method="post">
    <input type="hidden" name="title" value="<?php echo $value['title']; ?>">
    <input type="hidden" name="price" value="<?php echo $value['price']; ?>">
    <input type="hidden" name="img" value="<?php echo $value['img']; ?>">
    <input type="hidden" name="quantity" value="<?php echo $value['quantity']; ?>">
    <input type="hidden" name="grandtotal" value="240">
    <input type="hidden" name="email" value="<?php echo $_SESSION['email']; 
?>">        

 <?php } } ?>
<button type="submit">Checkout</button>
</form>

.

Its for insert Data

<?php


    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $title   = $_REQUEST['title'];
    $grandtotal   = $_REQUEST['grandtotal'];
    $quantity   = $_REQUEST['quantity'];
    $email   = $_REQUEST['email'];
    $img   = $_REQUEST['img'];




        if (empty($title) ||empty($grandtotal) || empty($quantity) || empty($email) || empty($img)) {
            echo "<div class='alert alert-danger'>"."All Fields are Requied !!"."</div>";
        } else {
            echo "<div class='alert alert-success'>"."Ordered Successfully!!"."</div>";
        }


        $title =  mysqli_real_escape_string($db->link,$_POST['title']);
        $grandtotal =  mysqli_real_escape_string($db->link,$_POST['grandtotal']);
        $quantity =  mysqli_real_escape_string($db->link,$_POST['quantity']);
        $email =  mysqli_real_escape_string($db->link,$_POST['email']);
        $img =  mysqli_real_escape_string($db->link,$_POST['img']);
        $price =  mysqli_real_escape_string($db->link,$_POST['price']);






        if($title =='' || $grandtotal =='' || $quantity =='' || $email =='' || $img ==''){
            $error = "Field Must not Be Empty";
        }else{
            $query = "INSERT INTO tbl_orders(title,price,img,qty,email,grandtotal) VALUES('$title','$price','$img','$quantity','$email','$grandtotal')";
            $create  =   $db->update($query);

        }

    }

    ?>


if its not possible , please give me more option for insert data into database from cart . thanks.

answered question

Where is the code for database operations ?

please check now..i add code for insert data

1 Answer

13

in that code block will generate something like that:

<input type="hidden" name="title" value="<?php echo $value['title']; ?>">
<input type="hidden" name="title" value="<?php echo $value['title']; ?>">

so title value will change for every input. And will send the last title data.

You should use square brackets [] in names for that. It will add to an array your datas. See the ex. code:

<input type="hidden" name="title[]" value="<?php echo $value['title']; ?>">

It will send an array to your server. Insert this with foreach loop.

posted this

Have an answer?

JD

Please login first before posting an answer.