update table

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
johnnysmith66
Forum Newbie
Posts: 2
Joined: Wed Jan 19, 2011 12:15 am

update table

Post by johnnysmith66 »

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. This is the cart code i need to pass product name, price, quantity and cart total into same fields in a table upon a submit button. I already tried passing the variables using hidden fields in a form I am really new to PHP the table name is customers and i am using an item_id as PK and auto increment.

Code: Select all

<?php
$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>";
		
		}
?>

thanks in advance.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: update table

Post by social_experiment »

The values are present in a session variable why use hidden fields? On the page where you want to add the information you can use something like this

Code: Select all

<?php
foreach($_SESSION["cart_array"]as $each_item){
 // assign the session values like in the previous query
}
$insertQuery = "INSERT INTO customers (yourField, yourField1, yourField2) VALUES 
($value, $value1, $value2)";
$insertSql = mysql_query($insertQuery);
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
johnnysmith66
Forum Newbie
Posts: 2
Joined: Wed Jan 19, 2011 12:15 am

Re: update table

Post by johnnysmith66 »

Thank you for the reply, i was given a lot of code to use but decided to make my cart a little better instead of just easy so at the final hurdle got stuck the code i was given was the below code but the other code looked better. Once I can get the product name, price and total price to go into the database I can then work on adding maybe customer Id instead of just the auto increment id. But I am grateful for the response.

Code: Select all

<?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');

?>
Post Reply