Adding Subentries to a Record


At this point in the chapter, you've learned how to add, remove, and view records. What's missing is adding additional entries to the related tables after you've already entered a master recordentries for home versus work telephone number, for example. All you need to do is make a few changes to existing scripts.

In the selentry.php script in Listing 20.3, change lines 166167 to read

$display_block .= "<p align=\"center\"> <a href=\"addentry.php?master_sel_id"]."\">add info</a> ... <a href=\"".$_SERVER["PHP_SELF"]."\">select another</a></p>";


This change simply adds a link to the addentry.php script and also passes it a variable accessible via $_GET["master_id"].

Now we need to modify the addentry.php script from Listing 20.2 to account for its dual purposes. Here is a summary of the changes to the original script.

Replace the first 10 lines of the original addentry.php script with the following snippet:

<?php if ((!$_POST) || ($_GET["master_id"] != "")) {     //haven't seen the form, so show it     $display_block = "     <form method=\"post\" action=\"".$_SERVER["PHP_SELF"]."\">";     if (isset($_GET["master_id"])) {         //connect to database         $mysqli = mysqli_connect("localhost", "joeuser", "somepass", "testDB");         //get first, last names for display/tests validity         $get_names_sql = "SELECT concat_ws(' ', f_name, l_name) AS display_name                           FROM master_name WHERE id = '".$_GET["master_id"]."'";         $get_names_res = mysqli_query($mysqli, $get_names_sql)                          or die(mysqli_error($mysqli));         if (mysqli_num_rows($get_names_res) == 1) {             while ($name_info = mysqli_fetch_array($get_names_res)) {                 $display_name = stripslashes($name_info['display_name']);             }         }     }     if (isset($display_name)) {         $display_block .= "<p>Adding information for         <strong>$display_name</strong>:</p>";     } else {         $display_block .= "         <p><strong>First/Last Names:</strong><br/>         <input type=\"text\" name=\"f_name\" size=\"30\" maxlength=\"75\">         <input type=\"text\" name=\"l_name\" size=\"30\" maxlength=\"75\">";     }     $display_block .= "<p><strong>Address:</strong><br/>


This snippet simply moves around the form elements, printing the first and last name fields only if they contain a new record. If they contain an addition to a record, the individual's name is extracted from the database for aesthetic purposes as well as for a validity check of the ID.

Next, find this line in the original addentry.php script:

<p><input type=\"submit\" name=\"submit\" value=\"Add Entry\"></p>


Directly above it, add the following:

    if ($_GET) {         $display_block .= "<input type=\"hidden\" name=\"master_id\"                value=\"".$_GET["master_id"]."\">";     }


This modification ensures that the known value of master_id is passed along to the next task, if it is present. Be sure to close the $display_block string in the line above this and restart the $display_block string in the line beneath this if statement.

Identify what were lines 4975 of the original script, beginning with the comment time to add to tables and ending with obtaining the value of $master_id. These lines should be replaced with the following:

    //time to add to tables, so check for required fields     if ((($_POST["f_name"] == "") || ($_POST["l_name"] == "")) &&     (!isset($_POST["master_id"]))) {         header("Location: addentry.php");         exit;     }     //connect to database     $mysqli = mysqli_connect("localhost", "joeuser", "somepass", "testDB");     if (!$_POST["master_id"]) {         //add to master_name table         $add_master_sql = "INSERT INTO master_name (date_added, date_modified,                           f_name, l_name) VALUES (now(), now(),                           '".$_POST["f_name"]."', '".$_POST["l_name"]."')";         $add_master_res = mysqli_query($mysqli, $add_master_sql)                           or die(mysqli_error($mysqli));         //get master_id for use with other tables         $master_id = mysqli_insert_id($mysqli);     } else {         $master_id = $_POST["master_id"];     }


These lines modify the check for required fields, allowing the script to continue without values for first and last names, but only if it has a $_POST["master_id"] value. Then the script connects to the database to perform all the additions we want it to, but it skips the addition to the master_name table if a value for $_POST["master_id"] exists.

Finally, in the section of the script that handles the insertion into the personal_notes table, change INSERT into to UPDATE to handle an update of the notes field:

$add_notes_sql = "UPDATE personal_notes set note = '".$_POST["note"]."' WHERE                   master_id = '".$master_id."'";


The new script should look like Listing 20.5.

Listing 20.5. New addentry.php Script

 1: <?php 2:   if ((!$_POST) || (isset($_GET["master_id"]))) { 3:       //haven't seen the form, so show it 4:       $display_block = " 5:       <form method=\"post\" action=\"".$_SERVER["PHP_SELF"]."\">"; 6: 7:       if (isset($_GET["master_id"])) { 8:           //connect to database 9:           $mysqli = mysqli_connect("localhost", "joeuser", "somepass", 10:                    "testDB"); 11: 12:          //get first, last names for display/tests validity 13:          $get_names_sql = "SELECT concat_ws(' ', f_name, l_name) AS 14:                           display_name FROM master_name WHERE id = 15:                           '".$_GET["master_id"]."'"; 16:          $get_names_res = mysqli_query($mysqli, $get_names_sql) 17:                           or die(mysqli_error($mysqli)); 18: 19:          if (mysqli_num_rows($get_names_res) == 1) { 20:               while ($name_info = mysqli_fetch_array($get_names_res)) { 21:                 $display_name = stripslashes($name_info['display_name']); 22:               } 23:          } 24:      } 25: 26:      if (isset($display_name)) { 27:          $display_block .= "<p>Adding information for 28:          <strong>$display_name</strong>:</p>"; 29:      } else { 30:          $display_block .= " 31:          <p><strong>First/Last Names:</strong><br/> 32:          <input type=\"text\" name=\"f_name\" size=\"30\" maxlength=\"75\"> 33:          <input type=\"text\" name=\"l_name\" size=\"30\" maxlength=\"75\">"; 34:      } 35:      $display_block .= "<p><strong>Address:</strong><br/> 36:      <input type=\"text\" name=\"address\" size=\"30\"></p> 37: 38:      <p><strong>City/State/Zip:</strong><br/> 39:      <input type=\"text\" name=\"city\" size=\"30\" maxlength=\"50\"> 40:      <input type=\"text\" name=\"state\" size=\"5\" maxlength=\"2\"> 41       <input type=\"text\" name=\"zipcode\" size=\"10\" maxlength=\"10\"></p> 42: 43:      <p><strong>Address Type:</strong><br/> 44:      <input type=\"radio\" name=\"add_type\" value=\"home\" checked> home 45:      <input type=\"radio\" name=\"add_type\" value=\"work\"> work 46:      <input type=\"radio\" name=\"add_type\" value=\"other\"> other</p> 47: 48:      <p><strong>Telephone Number:</strong><br/> 49:      <input type=\"text\" name=\"tel_number\" size=\"30\" maxlength=\"25\"> 50:      <input type=\"radio\" name=\"tel_type\" value=\"home\" checked> home 51:      <input type=\"radio\" name=\"tel_type\" value=\"work\"> work 52:      <input type=\"radio\" name=\"tel_type\" value=\"other\"> other</p> 53: 54:      <p><strong>Fax Number:</strong><br/> 55:      <input type=\"text\" name=\"fax_number\" size=\"30\" maxlength=\"25\"> 56:      <input type=\"radio\" name=\"fax_type\" value=\"home\" checked> home 57:      <input type=\"radio\" name=\"fax_type\" value=\"work\"> work 58:      <input type=\"radio\" name=\"fax_type\" value=\"other\"> other</p> 59: 60:      <p><strong>Email Address:</strong><br/> 61:      <input type=\"text\" name=\"email\" size=\"30\" maxlength=\"150\"> 62:      <input type=\"radio\" name=\"email_type\" value=\"home\" checked> home 63:      <input type=\"radio\" name=\"email_type\" value=\"work\"> work 64:      <input type=\"radio\" name=\"email_type\" value=\"other\"> other</p> 65: 66:      <p><strong>Personal Note:</strong><br/> 67:      <textarea name=\"note\" cols=\"35\" rows=\"3\" 68:      wrap=\"virtual\"></textarea></p> 69: 70:      <input type=\"hidden\" name=\"master_id\" value=\"".$_GET["master_id"]."\"> 71:      <p><input type=\"submit\" name=\"submit\" value=\"Add Entry\"></p> 72:      </form>"; 73: 74:  } else if ($_POST) { 75:      //time to add to tables, so check for required fields 76:      if ((($_POST["f_name"] == "") || ($_POST["l_name"] == "")) && 77:      (!isset($_POST["master_id"]))) { 78:          header("Location: addentry.php"); 79:          exit; 80:      } 81: 82:      //connect to database 83:      $mysqli = mysqli_connect("localhost", "joeuser", "somepass", "testDB"); 84: 85:      if (!$_POST["master_id"]) { 86:          //add to master_name table 87:          $add_master_sql = "INSERT INTO master_name (date_added, 88:                            date_modified, f_name, l_name) VALUES 89:                            (now(), now(), '".$_POST["f_name"]."', 90:                            '".$_POST["l_name"]."')"; 91:          $add_master_res = mysqli_query($mysqli, $add_master_sql) 92:                            or die(mysqli_error($mysqli)); 93: 94:          //get master_id for use with other tables 95:          $master_id = mysqli_insert_id($mysqli); 96:      } else { 97:          $master_id = $_POST["master_id"]; 98:       } 99: 100:      if (($_POST["address"]) || ($_POST["city"]) || ($_POST["state"]) 101:      || ($_POST["zipcode"])) { 102:      //something relevant, so add to address table 103:      $add_address_sql = "INSERT INTO address (master_id, date_added, 104:                         date_modified, address, city, state, zipcode, 105:                         type)  VALUES ('".$master_id."', now(), now(), 106:                         '".$_POST["address"]."', '".$_POST["city"]."', 107:                         '".$_POST["state"]."', '".$_POST["zipcode"]."', 108:                         '".$_POST["add_type"]."')"; 109:      $add_address_res = mysqli_query($mysqli, $add_address_sql) 110:                        or die(mysqli_error($mysqli)); 111:      } 112: 113:      if ($_POST["tel_number"]) { 114:          //something relevant, so add to telephone table 115:          $add_tel_sql = "INSERT INTO telephone (master_id, date_added, 116:                          date_modified, tel_number, type) VALUES 117:                          ('".$master_id."', now(), now(), 118:                          '".$_POST["tel_number"]."', 119:                          '".$_POST["tel_type"]."')"; 120:          $add_tel_res = mysqli_query($mysqli, $add_tel_sql) 121:                         or die(mysqli_error($mysqli)); 122:      } 123: 124:     if ($_POST["fax_number"]) { 125:         //something relevant, so add to fax table 126:         $add_fax_sql = "INSERT INTO fax (master_id, date_added, 127:                        date_modified, fax_number, type)  VALUES 128:                        ('".$master_id."', now(), now(), 129:                        '".$_POST["fax_number"]."', 130:                        '".$_POST["fax_type"]."')"; 131:         $add_fax_res = mysqli_query($mysqli, $add_fax_sql) 132:                        or die(mysqli_error($mysqli)); 133:     } 134: 135:     if ($_POST["email"]) { 136:         //something relevant, so add to email table 137:         $add_email_sql = "INSERT INTO email (master_id, date_added, 138:                          date_modified, email, type)  VALUES 139:                          ('".$master_id."', now(), now(), 140:                          '".$_POST["email"]."', 141:                          '".$_POST["email_type"]."')"; 142:         $add_email_res = mysqli_query($mysqli, $add_email_sql) 143:                          or die(mysqli_error($mysqli)); 144:     } 145: 146:     if ($_POST["note"]) { 147:         //something relevant, so add to notes table 148:         $add_notes_sql = "UPDATE personal_notes set note = 149:                          '".$_POST["note"]."' WHERE 150:                          master_id = '".$master_id."'"; 151:         $add_notes_res = mysqli_query($mysqli, $add_notes_sql) 152:                          or die(mysqli_error($mysqli)); 153:     } 154:     mysqli_close($mysqli); 155:     $display_block = "<p>Your entry has been added. 156:     Would you like to <a href=\"addentry.php\">add another</a>?</p>"; 157: } 158: ?> 159: <html> 160: <head> 161: <title>Add an Entry</title> 162: </head> 163: <body> 164: <h1>Add an Entry</h1> 165: <?php echo $display_block; ?> 166: </body> 167: </html>

You can try out this revised script by selecting a record to view and then following the add info link. You should see a form like Figure 20.7.

Figure 20.7. Adding to a record.


After submitting this form, you can go back through the selection sequence and view the record to verify that your changes have been made.




Sams Teach Yourself PHP, MySQL And Apache All in One
Sams Teach Yourself PHP, MySQL and Apache All in One (3rd Edition)
ISBN: 0672328739
EAN: 2147483647
Year: 2004
Pages: 327

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net