Forum Moderators: coopster

Message Too Old, No Replies

passing data to database table

upon press of submit btn

         

johnnysmith66

11:10 pm on Jan 20, 2011 (gmt 0)

10+ Year Member



I have a cart and everything works fine, but it is just for fun so i do not want something like paypal to be used just for the user to press a checkout btn and the info passed to a customer table. below is the code for the cart.I have tried a few things but cannot get product name, total price and cart total into the database it just updates with 0.00 and the auto increment customerId.This is



<?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>";

}
?>

vincevincevince

11:56 am on Jan 21, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



while($row=mysql_fetch_array($sql)){
$product_name=$row["product_name"];
$price=$row["price"];
$details=$row["details"];
} <<------
$pricetota..


This closing brace needs to come after the end of the dynamic block

johnnysmith66

12:12 pm on Jan 21, 2011 (gmt 0)

10+ Year Member



sorry all that part works it is when i tryed to add a button and send certain variables into a table like below


<?php echo $cartOutput ?>

</table>
<table width="400" border="1">
<tr>

<td width="134" align="right" bgcolor="#666666"><a href="cart.php?cmd=emptycart">&nbsp;Empty Cart&nbsp;</a></td>
<td width="150" align="left" bgcolor="#666666"><?php echo $cartTotal ?></td
>
<td width="150" align="left" bgcolor="#666666"><form id="checkBtn" name="checkBtn" method="post" action="cartsubmit.php">
<input type="submit" name="button" id="button" value="Submit" />
</form></td
>


when checkBtn is pressed i wanted the variables to be sent but nothing using an insert query.

rocknbil

5:27 pm on Jan 21, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome aboard johnnysmith . . look at your form. All you are submitting is a key "button" with the value "Submit"

<form id="checkBtn" name="checkBtn" method="post" action="cartsubmit.php">
<input type="submit" name="button" id="button" value="Submit" />
</form>
So if you added this at the top of your script,

var_dump($_POST);

or

foreach ($_POST as $key => $value) { echo "Key: $key Value: $value<br>"; }

you'd get

Key: button Value: Submit

Whatever you submit needs to be inside your form as input elements.

An aside, never use "reserved words" like button, submit, search, etc. for form element names or document element ID's. In this case it's OK but if you apply any Javascript, it may give you unexpected results - bad habit to get into.

johnnysmith66

7:00 pm on Jan 21, 2011 (gmt 0)

10+ Year Member



This is the code I was given to work off below, but i tried a little harder with a more advanced cart thus i end up here and stuck as to passing my variables into a table but I am grateful for the help..


<form name="cart" method="post" action="checkout.php" >
<p class="center">SHOPPING CART</p>
<p>No of Items: <strong></strong>
<br/>Total Price:<strong>0.00</strong>
<br>Product ID: <strong></strong><br/>
</p>
<p class="button"><input type="submit" name="Submit" value=" Check Out "></form>

passed to

<?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` (`customer_id`, `order_details`, `total_price`)"

."VALUES ('".$_SESSION["valid_id"]."', "
."'".$_SESSION["order"]."', "
."'".$_SESSION["cost"]."')";

mysql_query($sql);
$_SESSION["cost"]=0;
$_SESSION["products"]=0;
$_SESSION["order"]="";

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