Source code of dbUpdate3.php
<?php
// <?php
// Rather than listing my MySQL password directly in this PHP
// page, I've stored it in an external file
$inv_id = $_REQUEST['inv_id'];
$newprice = $_REQUEST['newprice'];
$allLines = file("mysql.txt");
$password = trim($allLines[0]);
$dbLink = mysql_connect("localhost", "tboegel", $password);
mysql_select_db("clearwater", $dbLink);
// We need to be extremely careful any time we get information from
// a user, to make sure that the data they have provided is valid.
// We should _never_ just take input from a text box and insert it
// into a query
// First, let's cast the inputted value as a float:
$price = (float) $newprice;
// Let's check to make sure that the inputted value is:
// 1) a number
// 2) between 0 and 999999.99 (the highest value that can be stored
// in this column
if (is_finite($price) && $price > 0 && $price <= 999999.99) {
$query = "UPDATE inventory SET inv_price = $price WHERE inv_id = $inv_id";
mysql_query($query, $dbLink);
if (mysql_affected_rows() == 1) {
// Here I'm going to use the header() function to send a raw HTTP
// header. The header I'm sending, Location, will cause the client's
// browser to be redirected to another web page. In this example,
// I'm redirecting back to the page of item prices, so that
// the client can see the effect of their update immediately.
header("Location: dbUpdate1.php");
// If I didn't want to redirect, I could print up some standard HTML
// results like this:
// print "<p>The price has been updated</p>";
//
// Note: when using header(), I need to make sure that the HTTP header
// I'm sending comes before any other content. If I print something
// out and then try to use header(), it won't work.
} else {
print "
<html>
<head>
<title>Updating information in a database</title>
</head>
<body>
<h2>Updating Prices</h2>";
print "<p>The price has not been updated</p>";
}
} else {
print "<p>The value you have entered is not a valid price.</p>";
}
mysql_close($dbLink);
?>
<p><a href='dbUpdate1.php'>Back to the price list</a></p>
</body>
</html>