Click to See Complete Forum and Search --> : Help with Ordering form + database


Stace
06-26-2006, 01:50 AM
Hi :)

Basically, I'm still fairly new to PHP. Anyway, I have created a database through phpmyadmin (see below)


# MySQL dump 8.12
#
# Host: localhost Database: database
#--------------------------------------------------------
# Server version 4.2.2

#
# Table structure for table 'inventory'
#

CREATE TABLE inventory (
id int(11) default NULL,
colour varchar(5) default NULL,
img varchar(15) default NULL,
width varchar(2) default NULL,
height varchar(2) default NULL,
prod varchar(50) default NULL,
maxquant decimal(5,0) default NULL,
price decimal(5,2) default NULL,
PRIMARY KEY (id)
) TYPE=MyISAM;

#
# Dumping data for table 'inventory'
#

INSERT INTO inventory VALUES ('1','large','glassl','70','68','Large Glass Widget','50',2.00);
INSERT INTO inventory VALUES ('2','small','glasss','45','44','Small Glass Widget','50',1.75);
INSERT INTO inventory VALUES ('3','large','marblel','70','68','Large Marble Widget','50',3.50);
INSERT INTO inventory VALUES ('4','small','marbles','45','44','Small Marble Widget','50',3.00);
INSERT INTO inventory VALUES ('5','large','stonel','70','68','Large Stone Widget','50',1.50);
INSERT INTO inventory VALUES ('6','small','stones','45','44','Small Stone Widget','50',1.25);
INSERT INTO inventory VALUES ('7','large','woodl','70','68','Large Wood Widget','50',1.00);
INSERT INTO inventory VALUES ('8','small','woods','45','44','Small Wood Widget','50',0.75);


I am creating an order form - which has 3 separate pages and processes. The first page will display the product data and the customer can input how many of each product they would like. Once they successfully submit what products they would like they will be taken to a page where they are asked to enter their shipping information, then once that is successfully filled they will be taken to an order summary page where information on what they ordered with tax and shipping is shown as well as their shipping information and that is where they are asked to confirm their order. Once they have confirmed it I will need to update the inventory in my database (subtract the number ordered from the in stock count). I am working on the first stage (page) and this is what I have:


<?php // Products page

// Include header
require_once ('header.php');

// Initialize some variables
$debug = 0; // set to 0 to turn off debug
$whereclause = '';

// Order Display
$f_order = 'id';

// access settings
$hostname = 'localhost';
$dbname = 'dbname';
$user = 'user';
$pwd = 'pwd';

// Connect to database
$sql = "select * from inventory $whereclause order by $f_order";
// print('$sql='.$sql);
$dblink = mysql_connect($hostname, $user, $pwd) or die ("Error: No connection to MySQL server\n");
mysql_select_db($dbname,$dblink) or die ("Error: MySQL database not selected\n");
$result = mysql_query($sql, $dblink) or die ("SQL query failed: $sql");

// Get number of rows (records) returned by SQL statement
$rows = mysql_num_rows($result);

// If there is at least one row ($rows > 0) , display in table
if ($rows > 0) {
// Display the SQL query if $debug is on
if ($debug) {
echo "<p><tt>$sql</tt></p>";
}

// Display Order Form Steps Table
echo '<table id="steps" summary="Order Form Steps">
<tr>
<td class="step">1. Order Widgets</td>
<td class="nostep">2. Shipping Information</td>
<td class="nostep">3. Confirm Order</td>
</tr>
</table><br />';

// Table For Display Products
echo '<form action="products.php" method="post">
<fieldset>
<legend>Product Information</legend>

<table summary="Table displaying product information">
<tr>
<td class="hdr">ID</td>
<td class="hdr">Image</td>
<td class="hdr">Product</td>
<td class="hdr">Quantity</td>
<td class="hdr">Price</td>
</tr>';

// Classic C-style syntax for loop: initialize; test; increment
for ($i = 0 ; $i < $rows ; $i++) {
$myrow = mysql_fetch_array($result);

// Products Array
echo '<tr class="'.$myrow["colour"].'">
<td>'.$myrow["id"].'</td>
<td><img src="images/'.$myrow["img"].'.jpg" width="'.$myrow["width"].'" height="'.$myrow["height"].'" alt="'.$myrow["prod"].'" /></td>
<td>'.$myrow["prod"].'</td>
<td><input type="text" maxlength="4" size="4" name="'.$myrow["img"].'" id="'.$myrow["img"].'" value="0" /></td>
<td>$'.$myrow["price"].'</td>
</tr>';
}

// Close the database link
mysql_close($dblink);

// Close the table
echo '</table>';

// Submit Form
echo '</fieldset>
<p class="center"><input type="submit" name="submit" id="submit" class="submit" value="Submit" />
<input type="reset" name="clear" id="clear" class="clear" value="Clear" /></p><br />
</form>';
}

// Include footer
require_once ('footer.php');

?>


I need to verify there is enough of each product they order. For instance, if they order 40 large glass widgets and I only have 30, it needs to be mentioned above the form on the same page (after they have hit submit), I would also like to highlight the corresponding product 'tr' (in the table) ergo 'class="large(or small)" would become class="nostock" for each item they've ordered in which there are not enough in stock.

What I have now doesn't touch upon this, but I want to verify if it's correct, I don't want to completely screw everything up all at once :P

So basically, I'm asking, is the code I have now correct, and how would I go about integrating the verification of stock?

Thank you!

Stace
06-26-2006, 03:33 PM
Sorry to bump, but anyone?? please..