Chapter 14 Code - Online Selling: A Quick Way to E-Commerce
Try It Out 1
<?php
//connect to the database - either include a connection variable file or
//type the following lines:
$connect = mysql_connect("localhost", "root", "mysqlpass") or
die ("Hey loser, check your server connection.");
//Create the ecommerce database
if (mysql_create_db("ecommerce")) {
echo "Woo hoo! Database created!";
}
else echo "Sorry, try creating the database again.";
mysql_select_db ("ecommerce");
//Define the product table
$query = "CREATE TABLE products (
prodnum CHAR(5) NOT NULL,
name VARCHAR(20) NOT NULL,
proddesc TEXT NOT NULL,
price DEC (6,2) NOT NULL,
dateadded DATE NOT NULL,
PRIMARY KEY(prodnum))";
$product = mysql_query($query)
or die(mysql_error());
//Define the customer table
$query2 = "CREATE TABLE customers (
custnum INT(6) NOT NULL AUTO_INCREMENT,
firstname VARCHAR (15) NOT NULL,
lastname VARCHAR (50) NOT NULL,
add1 VARCHAR (50) NOT NULL,
add2 VARCHAR (50),
city VARCHAR (50) NOT NULL,
state CHAR (2) NOT NULL,
zip CHAR (5) NOT NULL,
phone CHAR (12) NOT NULL,
fax CHAR (12),
email VARCHAR (50) NOT NULL,
PRIMARY KEY (custnum))";
$customers = mysql_query($query2)
or die(mysql_error());
//Define the general order table
$query3 = "CREATE TABLE ordermain (
ordernum INT(6) NOT NULL AUTO_INCREMENT,
orderdate DATE NOT NULL,
custnum INT(6) NOT NULL,
subtotal DEC (7,2) NOT NULL,
shipping DEC (6,2),
tax DEC(6,2),
total DEC(7,2) NOT NULL,
shipfirst VARCHAR(15) NOT NULL,
shiplast VARCHAR(50) NOT NULL,
shipcompany VARCHAR (50),
shipadd1 VARCHAR (50) NOT NULL,
shipadd2 VARCHAR(50),
shipcity VARCHAR(50) NOT NULL,
shipstate CHAR(2) NOT NULL,
shipzip CHAR(5) NOT NULL,
shipphone CHAR(12) NOT NULL,
shipemail VARCHAR(50),
PRIMARY KEY(ordernum)) ";
$ordermain = mysql_query($query3)
or die(mysql_error());
//Define the order details table
$query4 = "CREATE TABLE orderdet (
ordernum INT (6) NOT NULL,
qty INT(3) NOT NULL,
prodnum CHAR(5) NOT NULL,
KEY(ordernum))";
$orderdet = mysql_query($query4)
or die(mysql_error());
?>
Try It Out 2
<?php
//connect to the database - either include a connection variable file or
//type the following lines:
$connect = mysql_connect("localhost", "root", "mysqlpass") or
die ("Hey loser, check your server connection.");
mysql_select_db ("ecommerce");
$query = "INSERT INTO products VALUES (
'00001', 'CBA Logo T-shirt',
'This T-shirt will show off your CBA connection.
Our t-shirts are high quality and 100% preshrunk cotton.',
17.95, '2003-08-01'),
('00002','CBA Bumper Sticker',
'Let the world know you are a proud supporter of the
CBA Web site with this colorful bumper sticker.',
5.95, '2003-08-01'),
('00003', 'CBA Coffee Mug',
'With the CBA logo looking back at you over your
morning cup of coffee, you\'re sure to have a great
start to your day. Our mugs are microwave and dishwasher
safe.',
8.95, '2003-08-01'),
('00004', 'Superhero Body Suit',
'We have a complete selection of colors and sizes for you
to choose from. This body suit is sleek, stylish, and
won\'t hinder your crime-fighting or evil scheming abilities.
We also offer your choice in monogrammed letter applique.',
99.95, '2003-08-01'),
('00005', 'Small Grappling Hook',
'This specialized hook will get you out of the tightest
places. Specially designed for portability and stealth,
please be aware that this hook does come with a weight limit.',
139.95, '2003-08-01'),
('00006', 'Large Grappling Hook',
'For all your heavy-duty building-to-building swinging needs,
this large version of our grappling hook will safely transport
you throughout the city. Please be advised however that at
50 pounds, this is hardly the hook to use if you\'re a lightweight.',
199.95, '2003-08-01')";
$result = mysql_query($query)
or die(mysql_error());
echo "Products added successfully!";
?>
<?php
//connect to the database - either include a connection variable file or
//type the following lines:
$connect = mysql_connect("localhost", "root", "mysqlpass") or
die ("Hey loser, check your server connection.");
mysql_select_db ("ecommerce");
$query = "SELECT * FROM products";
$results = mysql_query($query)
or die(mysql_error());
?>
<HTML>
<HEAD>
<TITLE>Product List</TITLE>
</HEAD>
<BODY>
<table>
<tr>
<td width='10%'>Product Number</td>
<td width='20%'>Name</td>
<td width='50%'>Description</td>
<td width='10%'>Price</td>
<td width='10%'>Date Added</td>
</tr>
<tr>
<?php
while ($row = mysql_fetch_array($results)) {
extract ($row);
echo "<td width='10%'>";
echo $prodnum;
echo "</td><td width='20%'>";
echo $name;
echo "</td><td width='50%'>";
echo $proddesc;
echo "</td><td width='10%'>";
echo $price;
echo "</td><td width='10%'>";
echo $dateadded;
echo "</td></tr>";
}
?>
</table>
</BODY>
</HTML>
Try It Out 3
<?php
//connect to the database - either include a connection variable file or
//type the following lines:
$connect = mysql_connect("localhost", "root", "mysqlpass") or
die ("Hey loser, check your server connection.");
mysql_select_db ("ecommerce");
$query = "SELECT * FROM products";
$results = mysql_query($query)
or die(mysql_error());
?>
<HTML>
<HEAD>
<TITLE>Comic Book Appreciation Site Product List</TITLE>
</HEAD>
<BODY>
<div align='center'>
Thanks for visiting our site! Please see our list of awesome
products below, and click on the link for more information:
<br><br>
<table width='300'>
<tr>
<?php
// Show only Name, Price and Image
while ($row = mysql_fetch_array($results)) {
extract ($row);
echo "<td>";
echo "<a href = 'getprod.php?prodid=" . $prodnum ."'>";
echo "THUMBNAIL<br>IMAGE</td></a>";
echo "<td>";
echo "<a href = 'getprod.php?prodid=" . $prodnum ."'>";
echo $name;
echo "</td></a>";
echo "<td>";
echo "<a href = 'getprod.php?prodid=" . $prodnum ."'>";
echo $price;
echo "</td></a></tr>";
}
?>
</table>
</div>
</BODY>
</HTML>
Try It Out 4
<?php
session_start();
//connect to the database - either include a connection variable file or
//type the following lines:
$connect = mysql_connect("localhost", "root", "mysqlpass") or
die ("Hey loser, check your server connection.");
mysql_select_db ("ecommerce");
$prodid=$_REQUEST['prodid'];
$query = "SELECT * FROM products WHERE prodnum='$prodid'";
$results = mysql_query($query)
or die(mysql_error());
$row = mysql_fetch_array($results);
extract ($row);
?>
<HTML>
<HEAD>
<TITLE><?php echo $name ?></TITLE>
</HEAD>
<BODY>
<div align='center'>
<table width='500' cellpadding = '5'>
<tr>
<?php
echo "<td>IMAGE</td>";
echo "<td>";
echo "<strong>";
echo $name;
echo "</strong><br>";
echo $proddesc;
echo "<br>";
echo "Product Number: ";
echo $prodnum;
echo "<br>Price: ";
echo $price;
echo "</td>";
?>
</tr>
<tr>
<td>
</td>
<td><form method="POST" action="add.php">
Quantity:
<input type="text" name="qty" size="2">
<input type="hidden" name="prodnum" value="<?php echo $prodnum ?>">
<input type="submit" name="Submit" value="Add to cart">
</form>
<form method = "POST" action="cart.php">
<input type="submit" name="Submit" value="View cart">
</form>
</td>
</tr>
</table>
<a href="cbashop.php">Go back to the main page</a>
</div>
</BODY>
</HTML>
Try It Out 5
<?php
//connect to the database - either include a connection variable file or
//type the following lines:
$connect = mysql_connect("localhost", "root", "mysqlpass") or
die ("Hey loser, check your server connection.");
mysql_select_db ("ecommerce");
//Define the temp table
$query = "CREATE TABLE carttemp(
hidden INT(10) NOT NULL AUTO_INCREMENT,
sess CHAR(50) NOT NULL,
prodnum CHAR(5) NOT NULL,
quan INT(3) NOT NULL,
PRIMARY KEY (hidden),
KEY(sess))";
$temp = mysql_query($query)
or die(mysql_error());
?>
Try It Out 6
<?php
session_id();
session_start();
$connect = mysql_connect("localhost", "root", "mysqlpass") or
die ("Hey loser, check your server connection.");
mysql_select_db ("ecommerce");
$qty =$_POST['qty'];
$prodnum = $_POST['prodnum'];
$sess =session_id();
$query = "INSERT INTO carttemp (sess, quan, prodnum)
VALUES ('$sess','$qty','$prodnum')";
$results = mysql_query($query)
or die(mysql_error());
include("cart.php");
?>
Try It Out 7
<?php
session_id();
session_start();
//connect to the database
$connect = mysql_connect("localhost", "root", "mysqlpass") or
die ("Hey loser, check your server connection.");
mysql_select_db ("ecommerce");
?>
<HTML>
<HEAD>
<TITLE>Here is Your Shopping Cart!</TITLE>
</HEAD>
<BODY>
<div align="center">
You currently have
<?php
$sessid = session_id();
//display number of products in cart
$query = "SELECT * from carttemp WHERE sess = '$sessid'";
$results = mysql_query($query)
or die (mysql_query());
$rows = mysql_num_rows($results);
echo $rows;
?>
product(s) in your cart.<br>
<table border="1" align="center" cellpadding="5">
<tr>
<td>Quantity</td>
<td>Item Image</td>
<td>Item Name</td>
<td>Price Each</td>
<td>Extended Price</td>
<td></td>
<td></td>
<tr>
<?php
while ($row = mysql_fetch_array($results)) {
extract ($row);
$prod = "SELECT * FROM products WHERE prodnum =
'$prodnum'";
$prod2 = mysql_query($prod);
$prod3 = mysql_fetch_array($prod2);
extract ($prod3);
echo "<td><form method = 'POST' action='change.php'>
<input type='hidden' name='prodnum'
value='$prodnum'>
<input type='hidden' name='sessid'
value='$sessid'>
<input type='hidden' name='hidden'
value='$hidden'>
<input type='text' name='qty' size='2'
value='$quan'>";
echo "</td>";
echo "<td>";
echo "<a href = 'getprod.php?prodid=" .
$prodnum ."'>";
echo "THUMBNAIL<br>IMAGE</td></a>";
echo "<td>";
echo "<a href = 'getprod.php?prodid=" .
$prodnum ."'>";
echo $name;
echo "</td></a>";
echo "<td align='right'>";
echo $price;
echo "</td>";
echo "<td align='right'>";
//get extended price
$extprice = number_format($price * $quan, 2);
echo $extprice;
echo "</td>";
echo "<td>";
echo "<input type='submit' name='Submit'
value='Change Qty'>
</form></td>";
echo "<td>";
echo "<form method = 'POST' action='delete.php'>
<input type='hidden' name='prodnum'
value='$prodnum'>
<input type='hidden' name='qty' value='$quan'>
<input type='hidden' name='hidden'
value='$hidden'>
<input type='hidden' name='sessid'
value='$sessid'>";
echo "<input type='submit' name='Submit'
value='Delete Item'>
</form></td>";
echo "</tr>";
//add extended price to total
$total = $extprice + $total;
}
?>
<tr>
<td colspan='4' align='right'>Your total before shipping is:</td>
<td align='right'> <?php echo number_format($total, 2) ?></td>
<td></td>
<td></td>
</tr>
</table>
<form method="POST" action="checkout.php">
<input type='submit' name='Submit' value='Proceed to Checkout'>
</form>
<a href="cbashop.php">Go back to the main page</a>
</div>
</BODY>
</HTML>
Try It Out 8
<?php
session_id();
session_start();
$connect = mysql_connect("localhost", "root", "mysqlpass") or
die ("Hey loser, check your server connection.");
mysql_select_db ("ecommerce");
$qty =$_POST['qty'];
$hidden = $_POST['hidden'];
$sess = $_POST['sessid'];
$query = "UPDATE carttemp
SET quan = '$qty'
WHERE hidden = '$hidden'";
$results = mysql_query($query)
or die(mysql_error());
echo "Quantity Updated.<br> ";
include("cart.php");
?>
<?php
session_id();
session_start();
$connect = mysql_connect("localhost", "root", "mysqlpass") or
die ("Hey loser, check your server connection.");
mysql_select_db ("ecommerce");
$qty =$_POST['qty'];
$hidden = $_POST['hidden'];
$sess = $_POST['sessid'];
$query = "DELETE FROM carttemp WHERE hidden = '$hidden'";
$results = mysql_query($query)
or die(mysql_error());
echo "<div align='center'>
Item Deleted.<br>
<br></div> ";
include("cart.php");
?>
Try It Out 9
<?php
session_id();
session_start();
?>
<HTML>
<HEAD>
<TITLE>Step 1 of 3 - Billing and Shipping Information</TITLE>
</HEAD>
<BODY>
<strong>Order Checkout</strong><br>
<strong>Step 1 - Please Enter Billing and Shipping Information</strong><br>
Step 2 - Please Verify Accuracy of Order Information and Send Order<br>
Step 3 - Order Confirmation and Receipt<br>
<form method="post" action="checkout2.php">
<table width="300" border="1" align="left">
<tr>
<td colspan="2" bgcolor="#0000FF">
<div align="center"><font size="3" color="#FFFFFF">Billing Information
</font></div>
</td>
</tr>
<tr>
<td width="50%">
<div align="right">First Name</div>
</td>
<td width="50%">
<input type="text" name="firstname" maxlength="15">
</td>
</tr>
<tr>
<td width="50%">
<div align="right">Last Name</div>
</td>
<td width="50%">
<input type="text" name="lastname" maxlength="50">
</td>
</tr>
<tr>
<td width="50%">
<div align="right">Billing Address</div>
</td>
<td width="50%">
<input type="text" name="add1" maxlength="50">
</td>
</tr>
<tr>
<td width="50%">
<div align="right">Billing Address 2</div>
</td>
<td width="50%">
<input type="text" name="add2" maxlength="50">
</td>
</tr>
<tr>
<td width="50%">
<div align="right">City</div>
</td>
<td width="50%">
<input type="text" name="city" maxlength="50">
</td>
</tr>
<tr>
<td width="50%">
<div align="right">State</div>
</td>
<td width="50%">
<input type="text" name="state" size="2" maxlength="2">
</td>
</tr>
<tr>
<td width="50%">
<div align="right">Zip</div>
</td>
<td width="50%">
<input type="text" name="zip" maxlength="5" size="5">
</td>
</tr>
<tr>
<td width="50%">
<div align="right">Phone Number</div>
</td>
<td width="50%">
<input type="text" name="phone" size="12" maxlength="12">
</td>
</tr>
<tr>
<td width="50%">
<div align="right">Fax Number</div>
</td>
<td width="50%">
<input type="text" name="fax" maxlength="12" size="12">
</td>
</tr>
<tr>
<td width="50%">
<div align="right">E-Mail Address</div>
</td>
<td width="50%">
<input type="text" name="email" maxlength="50">
</td>
</tr>
</table>
<table width="300" border="1">
<tr bgcolor="#990000">
<td colspan="2">
<div align="center"><font size="3" color="#FFFFFF">Shipping Information
</font></div>
</td>
</tr>
<tr>
<td width="50%">
<div align="right">Shipping Info same as Billing</div>
</td>
<td width="50%">
<input type="checkbox" name="same">
</td>
</tr>
<tr>
<td width="50%">
<div align="right">First Name</div>
</td>
<td width="50%">
<input type="text" name="shipfirst" maxlength="15">
</td>
</tr>
<tr>
<td width="50%">
<div align="right">Last Name</div>
</td>
<td width="50%">
<input type="text" name="shiplast" maxlength="50">
</td>
</tr>
<tr>
<td width="50%">
<div align="right">Billing Address</div>
</td>
<td width="50%">
<input type="text" name="shipadd1" maxlength="50">
</td>
</tr>
<tr>
<td width="50%">
<div align="right">Billing Address 2</div>
</td>
<td width="50%">
<input type="text" name="shipadd2" maxlength="50">
</td>
</tr>
<tr>
<td width="50%">
<div align="right">City</div>
</td>
<td width="50%">
<input type="text" name="shipcity" maxlength="50">
</td>
</tr>
<tr>
<td width="50%">
<div align="right">State</div>
</td>
<td width="50%">
<input type="text" name="shipstate" size="2" maxlength="2">
</td>
</tr>
<tr>
<td width="50%">
<div align="right">Zip</div>
</td>
<td width="50%">
<input type="text" name="shipzip" maxlength="5" size="5">
</td>
</tr>
<tr>
<td width="50%">
<div align="right">Phone Number</div>
</td>
<td width="50%">
<input type="text" name="shipphone" size="12" maxlength="12">
</td>
</tr>
<tr>
<td width="50%">
<div align="right">E-Mail Address</div>
</td>
<td width="50%">
<input type="text" name="shipemail" maxlength="50">
</td>
</tr>
</table>
<p>
<input type="submit" name="Submit" value="Proceed to Next Step -->">
</p>
</form>
</BODY>
</HTML>
Try It Out 10
<?php
session_id();
session_start();
//connect to the database
$connect = mysql_connect("localhost", "root", "mysqlpass") or
die ("Hey loser, check your server connection.");
mysql_select_db ("ecommerce");
if ($_POST['same'] == 'on') {
$_POST['shipfirst'] = $_POST['firstname'];
$_POST['shiplast'] = $_POST['lastname'];
$_POST['shipadd1'] = $_POST['add1'];
$_POST['shipadd2'] = $_POST['add2'];
$_POST['shipcity'] = $_POST['city'];
$_POST['shipstate'] = $_POST['state'];
$_POST['shipzip'] = $_POST['zip'];
$_POST['shipphone'] = $_POST['phone'];
$_POST['shipemail'] = $_POST['email'];
}
?>
<HTML>
<HEAD>
<TITLE>Step 2 of 3 - Verify Order Accuracy</TITLE>
</HEAD>
<BODY>
Step 1 - Please Enter Billing and Shipping Information<br>
<strong>Step 2 - Please Verify Accuracy and Make Any Necessary
Changes</strong><br>
Step 3 - Order Confirmation and Receipt<br>
<form method="post" action="checkout3.php">
<table width="300" border="1" align="left">
<tr>
<td colspan="2" bgcolor="#0000FF">
<div align="center"><font size="3" color="#FFFFFF">Billing
Information
</font></div>
</td>
</tr>
<tr>
<td width="50%">
<div align="right">First Name</div>
</td>
<td width="50%">
<input type="text" name="firstname" maxlength="15"
value="<?php echo $_POST['firstname']?> ">
</td>
</tr>
<tr>
<td width="50%">
<div align="right">Last Name</div>
</td>
<td width="50%">
<input type="text" name="lastname" maxlength="50"
value="<?php echo $_POST['lastname']?>">
</td>
</tr>
<tr>
<td width="50%">
<div align="right">Billing Address</div>
</td>
<td width="50%">
<input type="text" name="add1" maxlength="50"
value="<?php echo $_POST['add1']?>">
</td>
</tr>
<tr>
<td width="50%">
<div align="right">Billing Address 2</div>
</td>
<td width="50%">
<input type="text" name="add2" maxlength="50"
value="<?php echo $_POST['add2']?>">
</td>
</tr>
<tr>
<td width="50%">
<div align="right">City</div>
</td>
<td width="50%">
<input type="text" name="city" maxlength="50"
value="<?php echo $_POST['city']?> ">
</td>
</tr>
<tr>
<td width="50%">
<div align="right">State</div>
</td>
<td width="50%">
<input type="text" name="state" size="2" maxlength="2"
value="<?php echo $_POST['state']?>">
</td>
</tr>
<tr>
<td width="50%">
<div align="right">Zip</div>
</td>
<td width="50%">
<input type="text" name="zip" maxlength="5" size="5"
value="<?php echo $_POST['zip']?>">
</td>
</tr>
<tr>
<td width="50%">
<div align="right">Phone Number</div>
</td>
<td width="50%">
<input type="text" name="phone" size="12" maxlength="12"
value="<?php echo $_POST['phone']?>">
</td>
</tr>
<tr>
<td width="50%">
<div align="right">Fax Number</div>
</td>
<td width="50%">
<input type="text" name="fax" maxlength="12" size="12"
value="<?php echo $_POST['fax']?>">
</td>
</tr>
<tr>
<td width="50%">
<div align="right">E-Mail Address</div>
</td>
<td width="50%">
<input type="text" name="email" maxlength="50"
value="<?php echo $_POST['email']?>">
</td>
</tr>
</table>
<table width="300" border="1">
<tr bgcolor="#990000">
<td colspan="2">
<div align="center"><font size="3" color="#FFFFFF">Shipping
Information
</font></div>
</td>
</tr>
<tr>
<td width="50%">
<div align="right">Shipping Info same as Billing</div>
</td>
<td width="50%">
<input type="checkbox" name="same"></td>
</tr>
<tr>
<td width="50%">
<div align="right">First Name</div>
</td>
<td width="50%">
<input type="text" name="shipfirst" maxlength="15"
value="<?php echo $_POST['shipfirst']?>">
</td>
</tr>
<tr>
<td width="50%">
<div align="right">Last Name</div>
</td>
<td width="50%">
<input type="text" name="shiplast" maxlength="50"
value="<?php echo $_POST['shiplast']?>">
</td>
</tr>
<tr>
<td width="50%">
<div align="right">Billing Address</div>
</td>
<td width="50%">
<input type="text" name="shipadd1" maxlength="50"
value="<?php echo $_POST['shipadd1']?>">
</td>
</tr>
<tr>
<td width="50%">
<div align="right">Billing Address 2</div>
</td>
<td width="50%">
<input type="text" name="shipadd2" maxlength="50"
value="<?php echo $_POST['shipadd2']?>">
</td>
</tr>
<tr>
<td width="50%">
<div align="right">City</div>
</td>
<td width="50%">
<input type="text" name="shipcity" maxlength="50"
value="<?php echo $_POST['shipcity']?>">
</td>
</tr>
<tr>
<td width="50%">
<div align="right">State</div>
</td>
<td width="50%">
<input type="text" name="shipstate" size="2" maxlength="2"
value="<?php echo $_POST['shipstate']?>">
</td>
</tr>
<tr>
<td width="50%">
<div align="right">Zip</div>
</td>
<td width="50%">
<input type="text" name="shipzip" maxlength="5" size="5"
value="<?php echo $_POST['shipzip']?>">
</td>
</tr>
<tr>
<td width="50%">
<div align="right">Phone Number</div>
</td>
<td width="50%">
<input type="text" name="shipphone" size="12" maxlength="12"
value="<?php echo $_POST['shipphone']?>">
</td>
</tr>
<tr>
<td width="50%">
<div align="right">E-Mail Address</div>
</td>
<td width="50%">
<input type="text" name="shipemail" maxlength="50"
value="<?php echo $_POST['shipemail']?>">
</td>
</tr>
</table>
<table border="1" align="left" cellpadding="5">
<tr>
<td>Quantity</td>
<td>Item Image</td>
<td>Item Name</td>
<td>Price Each</td>
<td>Extended Price</td>
<td></td>
<td></td>
</tr>
<tr>
<?php
$sessid = session_id();
$query = "SELECT * FROM carttemp WHERE sess = '$sessid'";
$results = mysql_query($query)
or die (mysql_query());
while ($row = mysql_fetch_array($results)) {
extract ($row);
$prod = "SELECT * FROM products WHERE
prodnum = '$prodnum'";
$prod2 = mysql_query($prod);
$prod3 = mysql_fetch_array($prod2);
extract ($prod3);
echo "<td>";
echo $quan;
echo "</td>";
echo "<td>";
echo "<a href = 'getprod.php?prodid=" .
$prodnum ."'>";
echo "THUMBNAIL<br>IMAGE</td></a>";
echo "<td>";
echo "<a href = 'getprod.php?prodid=" .
$prodnum ."'>";
echo $name;
echo "</td></a>";
echo "<td align='right'>";
echo $price;
echo "</td>";
echo "<td align='right'>";
//get extended price
$extprice = number_format($price * $quan, 2);
echo $extprice;
echo "</td>";
echo "<td>";
echo "<a href='cart.php'>Make Changes to Cart</a>";
echo "</td>";
echo "</tr>";
//add extended price to total
$total = $extprice + $total;
}
?>
<tr>
<td colspan='4' align='right'>Your total before shipping is:</td>
<td align='right'> <?php echo number_format($total, 2) ?></td>
<td></td>
<td></td>
</tr>
</table>
<input type="hidden" name="total" value="<?php echo $total?>">
<p>
<input type="submit" name="Submit" value="Send Order -->">
</p>
</form>
</BODY>
</HTML>
Try It Out 11
<?php
session_id();
session_start();
//connect to the database - either include a connection variable file or
//type the following lines:
$connect = mysql_connect("localhost", "root", "mysqlpass") or
die ("Hey loser, check your server connection.");
mysql_select_db ("ecommerce");
//Let's make the variables easy to access in our queries
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$firstname = $_POST['firstname'];
$add1 = $_POST['add1'];
$add2 = $_POST['add2'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$phone = $_POST['phone'];
$fax = $_POST['fax'];
$email = $_POST['email'];
$shipfirst = $_POST['shipfirst'];
$shiplast = $_POST['shiplast'];
$shipadd1 = $_POST['shipadd1'];
$shipadd2 = $_POST['shipadd2'];
$shipcity = $_POST['shipcity'];
$shipstate = $_POST['shipstate'];
$shipzip = $_POST['shipzip'];
$shipstate = $_POST['shipstate'];
$shipphone = $_POST['shipphone'];
$shipemail = $_POST['shipemail'];
$total = $_POST['total'];
$sessid = session_id();
$today = date("Y-m-d");
//1) Assign Customer Number to new Customer, or find existing customer number
$query = "SELECT * FROM customers WHERE
(firstname = '$firstname' AND
lastname = '$lastname' AND
add1 = '$add1' AND
add2 = '$add2' AND
city = '$city')";
$results = mysql_query($query)
or (mysql_error());
$rows = mysql_num_rows($results);
if ($rows < 1) {
//assign new custnum
$query2 = "INSERT INTO customers (
firstname, lastname, add1, add2, city, state, zip, phone, fax, email)
VALUES (
'$firstname',
'$lastname',
'$add1',
'$add2',
'$city',
'$state',
'$zip',
'$phone',
'$fax',
'$email')";
$insert = mysql_query($query2)
or (mysql_error());
$custid = mysql_insert_id();
}
//If custid exists, we want to make it equal to custnum
if($custid) $custnum = $custid;
//2) Insert Info into ordermain
//determine shipping costs based on order total (25% of total)
$shipping = $total * 0.25;
$query3 = "INSERT INTO ordermain (orderdate, custnum, subtotal,
shipping, shipfirst, shiplast, shipadd1, shipadd2,
shipcity, shipstate, shipzip, shipphone, shipemail)
VALUES (
'$today',
'$custnum',
'$total',
'$shipping'
'$shipfirst',
'$shiplast',
'$shipadd1',
'$shipadd2',
'$shipcity',
'$shipstate',
'$shipzip',
'$shipphone',
'$shipemail')";
$insert2 = mysql_query($query3)
or (mysql_error());
$orderid = mysql_insert_id();
//3) Insert Info into orderdet
//find the correct cart information being temporarily stored
$query = "SELECT * from carttemp WHERE sess='$sessid'";
$results = mysql_query($query)
or (mysql_error());
//put the data into the database one row at a time
while ($row = mysql_fetch_array($results)) {
extract ($row);
$query4 = "INSERT INTO orderdet (ordernum, qty, prodnum)
VALUES (
'$orderid',
'$quan',
'$prodnum')";
$insert4 = mysql_query($query4)
or (mysql_error());
}
//4)delete from temporary table
$query="DELETE FROM carttemp WHERE sess='$sessid'";
$delete = mysql_query($query);
//5)email confirmations to us and to the customer
/* recipients */
$to = "<" . $email .">";
/* subject */
$subject = "Order Confirmation";
/* message */
/* top of message */
$message = "
<html>
<head>
<title>Order Confirmation</title>
</head>
<body>
Here is a recap of your order:<br><br>
Order date:";
$message .= $today;
$message .= "
<br>
Order Number: ";
$message .= $orderid;
$message .= "
<table width='50%' border='0'>
<tr>
<td>
<p><font size='2'>Bill to:<br>";
$message .= $firstname;
$message .= " ";
$message .= $lastname;
$message .= "<br>";
$message .= $add1;
$message .= "<br>";
if ($add2) $message .= $add2 . "<br>";
$message .= $city . ", " . $state . " " . $zip;
$message .= "</p></td>
<td>
<p><font size='2'>Ship to:<br>";
$message .= $shipfirst . " " . $shiplast;
$message .= "<br>";
$message .= $shipadd1 . "<br>";
if ($shipadd2) $message .= $shipadd2 . "<br>";
$message .= $shipcity . ", " . $shipstate . " " . $shipzip;
$message .= "</font></p>
</td>
</tr>
</table>
<hr noshade width='250px' align='left'>
<table cellpadding='5'>
<tr>";
//grab the contents of the order and insert them
//into the message field
$query = "SELECT * from orderdet WHERE ordernum = '$orderid'";
$results = mysql_query($query)
or die (mysql_query());
while ($row = mysql_fetch_array($results)) {
extract ($row);
$prod = "SELECT * FROM products
WHERE prodnum = '$prodnum'";
$prod2 = mysql_query($prod);
$prod3 = mysql_fetch_array($prod2);
extract ($prod3);
$message .= "<td><font size='2'>";
$message .= $quan;
$message .= "</font></td>";
$message .="<td><font size='2'>";
$message .= $name;
$message .= "</font></td>";
$message .= "<td align='right'><font size='2'>";
$message .= $price;
$message .= "</font></td>";
$message .= "<td align='right'><font size='2'>";
//get extended price
$extprice = number_format($price * $quan, 2);
$message .= $extprice;
$message .= "</font></td>";
$message .= "</tr>";
}
$message .= "<tr>
<td colspan='3' align='right'><font size='2'>
Your total before shipping is:</font>
</td>
<td align='right'><font size='2'>";
$message .= number_format($total, 2);
$message .= "</font>
</td>
</tr>
<tr>
<td colspan='3' align='right'><font size='2'>
Shipping Costs:</font>
</td>
<td align='right'><font size='2'>";
$message .= number_format($shipping, 2);
$message .= "</font>
</td>
</tr>
<tr>
<td colspan='3' align='right'><font size='2'>
Your final total is:</font>
</td>
<td align='right'><font size='2'> ";
$message .= number_format(($total + $shipping), 2);
$message .= "</font>
</td>
</tr>
</table>
</body>
</html>";
/* headers */
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: <storeemail@email.com>\r\n";
$headers .= "Cc: <storeemail@email.com>\r\n";
$headers .= "X-Mailer: PHP / ".phpversion()."\r\n";
/* mail it */
mail ($to, $subject, $message, $headers);
//6)show them their order & give them an order number
?>
<HTML>
<HEAD>
<TITLE>Thank you for your order!</TITLE>
</HEAD>
<BODY>
Step 1 - Please Enter Billing and Shipping Information<br>
Step 2 - Please Verify Accuracy and Make Any Necessary Changes<br>
<strong>Step 3 - Order Confirmation and Receipt</strong><br><br>
Thank you for your order!<br><br>
Your order number is <?php echo $orderid ?>. Please print this page or retain
this number for your records.<br>
<br>
Here is a recap of your order:<br><br>
Order date: <?php echo $today ?><br>
<table width="50%" border="0">
<tr>
<td>
<p><font size="2">Bill to:<br>
<?php echo $firstname . " " . $lastname ?><br>
<?php echo $add1 ?><br>
<?php if ($add2) echo $add2 . "<br>"?>
<?php echo $city . ", " . $state . " " . $zip ?> </font></p>
</td>
<td>
<p><font size="2">Ship to:<br>
<?php echo $shipfirst . " " . $shiplast ?><br>
<?php echo $shipadd1 ?><br>
<?php if ($shipadd2) echo $shipadd2 . "<br>"?>
<?php echo $shipcity . ", " . $shipstate . " " . $shipzip ?> </font></p>
</td>
</tr>
</table>
<hr noshade width='250px' align='left'>
<table cellpadding="5">
<tr>
<?php
$query = "SELECT * from orderdet WHERE ordernum = '$orderid'";
$results = mysql_query($query)
or die (mysql_query());
while ($row = mysql_fetch_array($results)) {
extract ($row);
$prod = "SELECT * FROM products WHERE prodnum = '$prodnum'";
$prod2 = mysql_query($prod);
$prod3 = mysql_fetch_array($prod2);
extract ($prod3);
echo "<td><font size='2'>";
echo $quan;
echo "</font></td>";
echo "<td><font size='2'>";
echo $name;
echo "</font></td>";
echo "<td align='right'><font size='2'>";
echo $price;
echo "</font></td>";
echo "<td align='right'><font size='2'>";
//get extended price
$extprice = number_format($price * $quan, 2);
echo $extprice;
echo "</font></td>";
echo "</tr>";
}
?>
<tr>
<td colspan='3' align='right'><font size='2'>
Your total before shipping is:</font>
</td>
<td align='right'><font size='2'>
<?php echo number_format($total, 2) ?></font>
</td>
</tr>
<tr>
<td colspan='3' align='right'><font size='2'>
Shipping Costs:</font>
</td>
<td align='right'><font size='2'>
<?php echo number_format($shipping, 2) ?></font>
</td>
</tr>
<tr>
<td colspan='3' align='right'><font size='2'>
Your final total is:</font>
</td>
<td align='right'><font size='2'>
<?php echo number_format(($total + $shipping), 2) ?></font>
</td>
</tr>
</table>
</BODY>
</HTML>