You have a number of entries in the my_contacts table, so you'll need a quick way to select a single record for modification. The next script will create a drop-down menu of all the people in your database, from which you can select one record to modify.
Open a new file in your text editor and start a PHP block, and then start a session, or continue a session if one currently exists:
<? session_start();
Start an if…else block that checks the value of $_SESSION[valid] and performs a particular action based on the result. If the value is not yes, the user didn't go through the proper authentication channels:
if ($_SESSION[valid] != "yes") {
Send the user back to the login form, and exit this script:
header("Location: http://127.0.0.1/contact_menu.php");
exit;
}
Create variables to hold the name of the database on which the table resides, as well as the name of the table itself:
$db_name = "testDB"; $table_name = "my_contacts";
Add the connection information as you have been:
$connection = @mysql_connect("localhost", "spike", "9sj7En4")
or die(mysql_error());
Select the database as you have learned:
$db = @mysql_select_db($db_name, $connection) or die(mysql_error());
Create the SQL statement. You want to select just the ID number, first name, and last name of each record in the table:
Create a variable to hold the result of the mysql_query() function, as you have learned:
$result = @mysql_query($sql,$connection) or die(mysql_error());
Check for results using the mysql_num_rows() function:
c$num = @mysql_num_rows($result);
Check the value returned by the mysql_num_rows() function, and create a variable called $display_block to hold an error message if the number is less than 1 (in other words, if there are no rows returned and therefore no records in the table):
if ($num < 1) {
$display_block = "<P><em>Sorry! No results.</em></p>";
Continue the if…else block, so the script continues if the count of rows is one or more:
} else {
Start the while loop. The while loop will create an array called $row for each record in the result set ($result):
while ($row = mysql_fetch_array($result)) {
Get the individual elements of the record, and give them good names:
$id = $row['id']; $f_name = $row['f_name']; $l_name = $row['l_name'];
Create a variable called $option_block, which will contain the individual elements in the drop-down menu:
$option_block .= "<option value=\"$id\">$l_name, $f_name</option>";
Close the while loop:
Create a variable called $display_block, which will hold the form. Although this same variable was used to hold an error message in Step 10, it will not be used at that point unless there is an error. If there is an error, the script will never get to this step, so you have no worries about overwriting variables and can begin your form. For the form, assume that the method is POST and the action is a script called show_modcontact.php:
$display_block = "<FORM METHOD=\"POST\" ACTION=\"show_modcontact.php\">
Create a text label for the drop-down menu:
<P><strong>Contact:</strong>
Start the drop-down menu:
<select name=\"id\">
Place the $option_block string inside the <select> </select> tag pair. It should contain at least one <option> element:
$option_block
Finish the drop-down menu:
</select>
Add a submit button. Then, close your form, the string, the if…else block, and the PHP block:
<INPUT TYPE=\"SUBMIT\" NAME=\"submit\" VALUE=\"Select this Contact\"></P> </form>"; } ?>
Add this HTML:
Display the contents of $display_block:
<? echo "$display_block"; ?>
Add a link back to the main menu:
<br><p><a href="contact_menu.php">Return to Main Menu</a></p>
Add some more HTML so that the document is valid:
</BODY> </HTML>
Save the file with the name pick_modcontact.php, and place this file in the document root of your web server.
Your code should look something like this:
<?
//start a session
session_start();
//check validity of user
if ($_SESSION[valid] != "yes") {
header("Location: http://127.0.0.1/contact_menu.php");
exit;
}
//set up table and database names
$db_name = "testDB";
$table_name = "my_contacts";
//connect to server and select database
$connection = @mysql_connect("localhost", "spike", "9sj7En4")
or die(mysql_error());
$db = @mysql_select_db($db_name, $connection) or die(mysql_error());
//build and issue query
$sql = "SELECT id, f_name, l_name FROM $table_name ORDER BY l_name";
$result = @mysql_query($sql,$connection) or die(mysql_error());
//check the number of results
$num = @mysql_num_rows($result);
if ($num < 1) {
//if there are no results, display message
$display_block = "<P><em>Sorry! No results.</em></p>";
} else {
//if results are found, loop through them
//and make a form selection block
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
$f_name = $row['f_name'];
$l_name = $row['l_name'];
$option_block .= "<option value=\"$id\">$l_name, $f_name</option>";
}
//create the entire form block
$display_block = "
<FORM METHOD=\"POST\" ACTION=\"show_modcontact.php\">
<P><strong>Contact:</strong>
<select name=\"id\">
$option_block
</select>
<INPUT TYPE=\"SUBMIT\" NAME=\"submit\" VALUE=\"Select this Contact\"></P>
</form>";
}
?>
<HTML>
<HEAD>
<TITLE>My Contact Management System: Modify a Contact</TITLE>
</HEAD>
<BODY>
<h1>My Contact Management System</h1>
<h2><em>Modify a Contact - Select from List</em></h2>
<P>Select a contact from the list below, to modify the contact's record.</P>
<? echo "$display_block"; ?>
<br>
<p><a href="contact_menu.php">Return to Main Menu</a></p>
</BODY>
</HTML>
In the next section, you create the record-modification form, which looks strikingly similar to the record-addition form.