Please help a noob with sessions!

I’m developing a site for a friend that is only to be used by his staff, not customers.

Part of this is a form to add items to an order. I have done this in the following manner:

The code is as follows:

[code]<?

// connect to db
include(“dbinfo.inc.php”);
$connection = mysql_connect($server,$username,$password);

if (!$connection){
echo mysql_errno().": “.mysql_error().”
";
exit;
}

if(!mysql_select_db($database)){
echo(“Database not found
”);
}

//RETURN RESULT
$result = mysql_query (“SELECT ID,Item,Price,SortOrder FROM products ORDER BY SortOrder”);
$total = mysql_num_rows($result);
?>

<? if($total>0) { ?> <? // print table header echo("

");

// fetch the current row into the array $row
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo("

");
echo("”);
echo("”);
?> <? echo ""; } echo("
Item Price Quantity
" . $row[“Item”] . “" . $row[“Price”] . “
"); ?> <? }

mysql_close();
?>

[/code]

As you can see, I used:

<td><input type="text" name="Quantity<?php echo $row[SortOrder];?>"" value ="0" size="5" /></td>

to create input boxes named Quantity1, Quantity2 etc.

I want to use that data in a shopping cart style, i.e. the items add to a cart. I made a quick test with viewcart.php which looks like this:

[code]<?
include(“dbinfo.inc.php”);
$connection = mysql_connect($server,$username,$password);

if (!$connection){
echo mysql_errno().": “.mysql_error().”
";
exit;
}

if(!mysql_select_db($database)){
echo(“Database not found
”);
}

//RETURN RESULT
$result = mysql_query (“SELECT ID,Item,Price,SortOrder FROM products ORDER BY SortOrder”);
$total = mysql_num_rows($result);
?>

<? if($total>0) { // print table header echo("

"); $q = "Quantity"; $t = 0; // fetch the current row into the array $row while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $s = $row["SortOrder"]; if($_POST[$q.$s]>0) { $t = $t + ($row["Price"] * $_POST[$q.$s]); echo(""); echo(""); echo(""); echo(""); echo (""); } } echo(""); echo("
Item Price Quantity
" . $row["Item"] . "" . $row["Price"] . "" . $_POST[$q.$s] . "
TOTAL: " . $t . "  
"); } mysql_close(); ?> [/code]

I’m sure this isn’t a good method, although it seems to be working. I’ve looked into sessions, but can’t get my head round them! Could someone advise me?

Many thanks in advance!

I guess your first stop would be this page.

Sponsor our Newsletter | Privacy Policy | Terms of Service