![]() |
|
Workshop: Using PHP/mySQL With PHP/mySQL you don't need an Oracle license or DBA to get your e-business back end up to speed. September 24, 2000 Code Sample 2: mug_world.php
<?php
# Global variables
$script_name = 'mug_world.php';
$db_name = 'mug_world';
$db_host = 'localhost';
$db_username = 'webuser';
$db_password = 'customer';
function view_products() {
# explicity request to use these global variables
global $script_name, $db_name, $db_host, $db_username, $db_password;
# connect to database
$link = mysql_connect($db_host, $db_username, $db_password)
or die("Connection failed: " . mysql_error());
# select our table
mysql_select_db($db_name)
or die("Database selection failed [$db_name]: " . mysql_error());
# send a query
$sql = "SELECT * FROM products ORDER BY product_id";
$result = mysql_query($sql)
or die("Query failed: " . mysql_error());
echo "<H2>Welcome to Mug World</H2>\n";
echo "<FORM METHOD=POST ACTION=\"$script_name\">\n";
echo "<TABLE BORDER=1>\n";
echo "Name: <INPUT TYPE=TEXT NAME=\"name\">\n";
# print the product selection table
$table_row = "<TR>";
$table_row .= "<TH> </TH>";
$table_row .= "<TH>Mug Quote</TH>";
$table_row .= "<TH>Price</TH>";
$table_row .= "</TR>";
echo "$table_row\n";
while ($row = mysql_fetch_array($result)) {
$table_row = "<TR>";
$table_row .= "<TD><INPUT TYPE=RADIO NAME=\"product\"";
$table_row .= " VALUE=\"$row[product_id]\"></TD>";
$table_row .= "<TD>$row[quote]</TD>";
$table_row .= "<TD>$row[cost]</TD>";
$table_row .= "<TR>";
echo "$table_row\n";
}
echo "</TABLE>\n";
echo "<INPUT TYPE=SUBMIT NAME=\"button\" VALUE=\"Submit Order\">\n";
echo "<INPUT TYPE=SUBMIT NAME=\"button\" VALUE=\"View Orders\">\n";
echo "</FORM>\n";
mysql_free_result($result);
mysql_close($link);
}
function view_orders() {
# explicity request to use these global variables
global $script_name, $db_name, $db_host, $db_username, $db_password;
# connect to database
$link = mysql_connect($db_host, $db_username, $db_password)
or die("Connection failed: " . mysql_error());
# select our table
mysql_select_db($db_name)
or die("Database selection failed [$db_name]: " . mysql_error());
# send a query
$sql = "SELECT orders.order_num,orders.name,orders.product_id,";
$sql .= "products.quote,products.cost ";
$sql .= "FROM orders ";
$sql .= "LEFT JOIN products ON orders.product_id=products.product_id ";
$sql .= "ORDER BY orders.order_num";
$result = mysql_query($sql)
or die("Query failed: " . mysql_error());
echo "<H2>Current Mug World Orders</H2>\n";
echo "<TABLE BORDER=1>\n";
# print the product selection table
$table_row = "<TR>";
$table_row .= "<TH>Order Number</TH>";
$table_row .= "<TH>Name</TH>";
$table_row .= "<TH>Product</TH>";
$table_row .= "<TH>Cost</TH>";
$table_row .= "</TR>";
echo "$table_row\n";
while ($row = mysql_fetch_array($result)) {
$table_row = "<TR>";
$table_row .= "<TD>$row[order_num]</TD>";
$table_row .= "<TD>$row[name]</TD>";
$table_row .= "<TD>$row[quote]</TD>";
$table_row .= "<TD>$row[cost]</TD>";
$table_row .= "<TR>";
echo "$table_row\n";
}
echo "</TABLE>\n";
echo "<FORM METHOD=POST ACTION=\"$script_name\">\n";
echo "<INPUT TYPE=SUBMIT NAME=\"button\" VALUE=\"Back to Order Page\">\n";
echo "</FORM>\n";
mysql_free_result($result);
mysql_close($link);
}
function save_order($name, $product) {
# explicity request to use these global variables
global $script_name, $db_name, $db_host, $db_username, $db_password;
# connect to database
$link = mysql_connect($db_host, $db_username, $db_password)
or die("Connection failed: " . mysql_error());
# select our table
mysql_select_db($db_name)
or die("Database selection failed [$db_name]: " . mysql_error());
# send a query
$sql = "INSERT INTO orders ";
$sql .= "SET name='$name',product_id='$product'";
mysql_query($sql)
or die("Query failed: " . mysql_error());
mysql_close($link);
return mysql_insert_id();
}
?>
<HTML>
<HEAD><TITLE>Welcome to Mug World</TITLE></HEAD>
<BODY>
<?php
# main routine
switch ($button) {
case 'Submit Order':
save_order($name, $product);
view_products();
break;
case 'View Orders':
view_orders();
break;
default:
view_products();
}
?>
</BODY>
</HTML>
Page 1 | 2 | 3 | 4 | 5 | First page
Send your comments on this article to Patrick A. Paskvan at paskvan@data.assist.com.
| |












