Hi there, i have the following php code that works fine. But when i
click on the submit button and then it takes me to the next page, it
displays the following error mesage at the top
Warning: MySQL: A link to the server could not be established in
c:\program files\nusphere\apache\nsdocs\mug_world.php on line 129
(the line that is in bold and highlighted in red)
Everything works fine except the above line. (The information i filled
in did go in the table that i created in mysql...)
<?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>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php