Jump to content

insert into table from cart


supernatural

Recommended Posts

I hope this is the correct forum and Hi to everybody.

I have a shopping cart and I cant figure out how to send the final cart information into a table when the user presses checkout, I also need the cart to clear.

 

here is the code I used.

 

$priceTotal="";
$product_name="";
$price="";
$details="";
$cartOutput="";
$cartTotal="";
if(!isset($_SESSION["cart_array"])||count($_SESSION["cart_array"])<1){
    $cartOutput="<h2 align='center'>your cart is empty</h2>";
}else{
        //index starts at 0
    $i=0;
    foreach($_SESSION["cart_array"]as $each_item){
    $item_id=$each_item['item_id'];
    $sql=mysql_query("SELECT * FROM products WHERE id='$item_id'LIMIT 1");
    while($row=mysql_fetch_array($sql)){
        $product_name=$row["product_name"];
        $price=$row["price"];
        $details=$row["details"];
        }
        $pricetotal=$price * $each_item['quantity'];  
        $cartTotal=$pricetotal + $cartTotal;
                //formats currency
                setlocale(LC_MONETARY, "en_GB");
                $pricetotal=money_format("%!10.2n",$pricetotal);
        //Dynamic tabel starts here
        $cartOutput .= "<tr>";
                $cartOutput .= '<td><a href="product.php?id=' . $item_id . '"></a><br/><img src="inventory_images/' . $item_id . '.jpg" alt="' . $product_name . '" width="117" height="178" border="1" /></td>';
                $cartOutput .= '<td>' . $details . '</td>';
                $cartOutput .= '<td>£' . $price . ' </td>';
                $cartOutput .= '<td>' . $each_item['quantity'] . '</td>';
                $cartOutput .= '<td>£' . $pricetotal . '</td>';  
                $cartOutput .= '<td><form action="cart.php" method="post"><input name="deleteBtn' . $item_id . '"type="submit" value="x"/><input name="item_to_remove" type="hidden" value="' . $i . '" /></form></td>';
                $cartOutput .='</tr>';  
               
                 //items add by 1
                $i++;
                  }
               
                setlocale(LC_MONETARY, "en_GB");
                $cartTotal=money_format("%!10.2n",$cartTotal);
        $cartTotal="<div align='right'>Cart Total:£" .$cartTotal. " </div>";
               
                }

Link to comment
Share on other sites

I have now looked all over and the best i could get was to use the paypal function, i did try the code below but could not figure out how to get my variables into it, this is the last piece of the jigsaw so to speak then i can try out other things. I will be eternally grateful to the person who can help me out, like I mentioned i need to send an auto increment Id product name, price, total price,.

 

<?php

session_start();
$_SESSION["valid_id"];
$_SESSION["cost"];
$_SESSION["products"];
$_SESSION["order"];
$items = explode(",",$_SESSION["order"]);

include ("dbConfig.php");
include ("cart.php");


$sql="SELECT * FROM products";
$result=mysql_query($sql);

while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
	{
			for( $i = 0; $i < count($items); ++$i )		
	{	
				if ($items[$i]==$row['product_id'])
				{
				$order = $order.$row['title'].",";
				}		 
	}
	}	

$sql='INSERT INTO orders VALUES ("",'.$_SESSION["valid_id"].',"'.$order.'",'.$_SESSION["cost"].')';
mysql_query($sql); 

$_SESSION["cost"]=0;
$_SESSION["products"]=0;
$_SESSION["order"]="";

header ('Location: index.php?order=true');

?>


Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.