Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP Shopping cart - Help

Status
Not open for further replies.

Bastien

Programmer
May 29, 2000
1,683
CA
Hi All,

I am trying to develop a shopping cart. This cart takes the values from the form and places them into a temp table created to hold the data serverside. The interesting thing is that the form is dynamic. Each product MAY have certain options that only appear when the page for that (well actually it is one page that is dynamically generated with PHP based on an index of products) item is clicked. The challenge is to create the cart table and the SQL statement to allow for the options, and of course not all options have to be choosen.

Here is the script for the table:
Create table $PHPSESSID (
item_sold_id int(10) NOT NULL auto_increment,
date_sold timestamp(14) NOT NULL,
prod_ID int(10) NOT NULL,
prod_name varchar(50), NOT NULL,
distrib_id int(10) default NULL,
quantity_purch tinyint(2) NOT NULL,
option_name varchar (30) NULL,
option_price float (10,2) NULL,
total_price float(10,2) NOT NULL,
PRIMARY KEY (item_sold_id),
UNIQUE KEY item_sold_id(item_sold_id),
KEY item_sold_id_2(item_sold_id)
);
Here is the code that creates the form with the data for the product:

$row = mysql_fetch_array($SQLResult);
$PNum = $row["prod_id"];
$Ptype = $row["prod_type"];
$PName = $row["prod_name"];
$PPrice = $row["prod_price"];
$PDescription = $row["prod_description"];
$PbigPic = $row["gulf_prod_pic"];
mysql_free_result($SQLResult);
?>

<table cols = 2 bgcolor = white border=0><tr><td rowspan = 3>
<img src=&quot; <? echo $PbigPic; ?> &quot;></td><td>
<table border = 0 width = &quot;100%&quot;><form name = buy action = cart.php method = post>
<input type = hidden value = &quot;<? echo $PNum; ?>&quot;>
<tr><td colspan = 2><h3>Price: </h3></td><td><h3><p align = &quot;right&quot;>$<? echo $PPrice; ?></h3>
<input type=hidden valu e= $PPrice></td></tr>

<tr><td colspan = 3><? echo $PDescription; ?></td></tr>
<?

echo &quot;</table></td><tr><td><table width=\&quot;100%\&quot; ><tr><td colspan = 2>&quot;;
echo &quot;<h3><center>Buy This Item</center></h3></td></tr><tr><td>&quot;;
if (($Ptype == &quot;Footspa&quot;)|| ($Ptype == &quot;Chair&quot;)){
//show the chair select boxes
Show_ChairColor();
echo &quot;</td></tr>&quot;;

$SQLOption = &quot;select * from gulf_options where prod_id = $PNum and
option_avail = 1&quot;;

$SQLGetOp = mysql_query($SQLOption, $conn) or die (&quot;Couldn't execute query&quot;);

echo &quot;<tr><td><table border = 0 cols = 3 width = \&quot;100%\&quot;><tr><td><h4>Options Available</td>&quot;;
$x=0;
//get the options and assign the array
while ($row1 = mysql_fetch_array($SQLGetOp)){
$POpNum = $row1[&quot;option_id&quot;];
$POpDesc = $row1[&quot;option_desc&quot;];
$POptionPrice = $row1[&quot;option_price&quot;];
$POpName = $row1[&quot;option_name&quot;];
$x++;
echo &quot;<tr><td>$POpName</td><input type =hidden value = $PopName>
<td><center><input type =hidden value = $POptionPrice>
\$$POptionPrice</td>
<td width = 20><input type = checkbox name = Option$x></td></tr>&quot;;
}
echo &quot;</table><input type=hidden value = $x>&quot;;


How can I bring the data from the form to the SQL statement to store it in the DB table?

Is the table structure correct?

Is there a ready made solution that will accomplish what I need this to do? If so, where can I get it?

TIA



Bastien

There are many ways to skin this cat,
but it still tastes like chicken
 
Anyone??? Please just a little help...

TIA Bastien

There are many ways to skin this cat,
but it still tastes like chicken
 
I don't know that much about php, but I have used php and mysql to update a database from a form, the syntax I used is

mysql_query(&quot;UPDATE tableName SET field1 =\'$infoForField1\', field2=\'$infoForField2\' WHERE id=$id);

I understand that your page changes based on the item, if there aren't too many different scenarios, you could use if statements to determine which sql statement to run. This probably isn't the most efficient way of doing it, but maybe it will help. Also, I have noticed that when I post a smaller message I tend to get more help, so if this doesn't help you might want to try posting with as little info as possible.

Good Luck,
Jewel
 
OK. I am not the best PHP developer out there, but I think I can do what you want. But I need more info first.

#1. I hope I am understanding this right. Why are you creating a temp table for every user? What happens if the user closes their browser or goes some place else? Now you have dangling tables. Why not just make one generic temp table and insert, update, and delete from that table as needed. You will save yourself and your server a big headache. Then create a script that will delete enteries that are just so old.

As for bringing the data from the form, you will have to get the user to click a submit button and send the variable to a page that puts it into the database.

mysql_query(insert into temp_table(row1,row2,row3)
values($var1,$var2,$var3);

Then use mysql_affected_rows() to verify the addition.
Notice you can not use $result = mysql_query(insert ....);
and then check the result. It will fail. $result only works with SELECT.

I hope that gets you on the way. Contact me if you have more questions.

scott@kingdon.net

Scott
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top