Results 1 to 10 of 17
ok, i feel dumb........ i am creating a simple web cart from this site ( Building a Simple PHP Shopping Cart ). the shopping cart works great but now i ...
- 02-24-2009 #1Linux Newbie
- Join Date
- Dec 2006
- Posts
- 119
[SOLVED] Insert Values to Database
ok, i feel dumb........ i am creating a simple web cart from this site (Building a Simple PHP Shopping Cart). the shopping cart works great but now i would like to be able to insert the values on the shopping cart (cart.php) into MySQL database.
so how i could do that?
note: i changed couple or variables, but not a lot that will differ from that site. here is my current (cart.php).
thanks in advance for your help.PHP Code:<?php
function writeShoppingCart() {
$cart = $_SESSION['cart'];
if (!$cart) {
return '<p>You have no items in your shopping cart</p>';
} else {
// Parse the cart session variable
$items = explode(',',$cart);
$s = (count($items) > 1) ? 's':'';
return '<p>You have <a href="cart.php">'.count($items).' item'.$s.' in your shopping cart</a></p>';
}
}
function showCart() {
global $db;
$cart = $_SESSION['cart'];
if ($cart) {
$items = explode(',',$cart);
$contents = array();
foreach ($items as $item) {
$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
}
//$output[] = '<form action="cart.php?action=update" method="post" id="cart">';
$output[] = '<form action="inc/orderReportCheckoutPost.php" method="post" id="cart">';
$output[] = '<table>';
foreach ($contents as $id=>$qty) {
$sql = 'SELECT * FROM ProductItems WHERE ProductItemsID = '.$id;
$result = $db->query($sql);
$row = $result->fetch();
extract($row);
$output[] = '<tr>';
$output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
$output[] = '<td>'.$Item.'</td>';
$output[] = '<td>$'.$Price.'</td>';
$output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
$output[] = '<td>$'.($Price * $qty).'</td>';
$total += $Price * $qty;
$output[] = '</tr>';
}
$output[] = '</table>';
$output[] = '<p>Grand total: <strong>$'.$total.'</strong></p>';
$output[] = '<div><button type="submit">Update cart</button>';
$output[] = '<input Name = "Submit1" type="submit"></div>';
$output[] = '</form>';
} else {
$output[] = '<p>You shopping cart is empty.</p>';
}
return join('',$output);
}
?>
- 02-25-2009 #2Just Joined!
- Join Date
- Feb 2009
- Posts
- 4
So I guess we can start at the beginning are you making a connection to you db yet and only needing help with the insert statement or are you having needing help with the whole thing.
mysql connect info is located here.
I generally use the query to do my inserts.
- 02-25-2009 #3Linux Newbie
- Join Date
- Dec 2006
- Posts
- 119
- 02-25-2009 #4Just Joined!
- Join Date
- Feb 2009
- Posts
- 4
Without know the the table layout it will be kind of difficult.
I am going on the assumption it will look something like this. There maybe more or less but this should help you get the idea.
tablename
purchase_id auto increment pk
item_id pk
price
qty
It would end up something like this. One of the reasons I have price in there is in case you change prices.
Also you might need to put quotes around the Price depending on how it is setup.Code:INSERT INTO tablename (id, price, qty) VALUES ($Item, $Price, $qty)
It is a little rough of an answer. I hope it helps.
- 02-25-2009 #5Linux Newbie
- Join Date
- Dec 2006
- Posts
- 119
thnx for your reply so quick. actually the table will look something like this:
the only values that i would like todo is......Code:Order Table OrderID (PK) TotalPrice ProductItemsID (FK) Product Table ProductItemsID (PK) Items Price
insert each ProductItemsID and TotalPrice to the Order's Table
so in other words i would like to create some type of loop that it will pass $id, $total, and $Item to the Order's Table
sorry that i didn't provide enough information at first.
really............ thank you so much for your help and effort.
- 02-25-2009 #6
Be careful with SQL injection. I don't know php but this:
looks like it might be vulnerable. Here is some background. Basically, you should be sanitizing any data inputs that could be passed in a WHERE clause to the database.PHP Code:$sql = 'SELECT * FROM ProductItems WHERE ProductItemsID = '.$id;
$result = $db->query($sql);
- 02-25-2009 #7Linux Newbie
- Join Date
- Dec 2006
- Posts
- 119
- 02-25-2009 #8Linux Guru
- Join Date
- Nov 2004
- Posts
- 6,110
- 02-25-2009 #9
Very VEry VERy VERY vulnerable.
You should be looking at validating $id for data type and data values. In fact you should do that for every user input and by user I mean people and remote systems.If we hit that bullseye, the rest of the dominoes will fall like a house of cards. Checkmate! (Zapp Brannigan)
My new blog. It's probably not as good as I think it is.
- 02-26-2009 #10Linux Newbie
- Join Date
- Dec 2006
- Posts
- 119




