Creating the Record Addition Mechanism


Just because you'll potentially be adding information to six different tables doesn't mean your form or script will be monstrous. In fact, your scripts won't look much different from any of the ones you created in previous lessons, and with practice, you will be able to make these verbose scripts much more streamlined and efficient.

In Listing 20.2, you can see a basic record addition script, called addentry.php, that has two parts: what to do if the form should be displayed (lines 246) and what actions to take if the form is being submitted (lines 48133). Lines 246 simply place the contents of the HTML form into a string called $display_block.

Listing 20.2. Basic Record Addition Script Called addentry.php

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

As already noted, this script performs one of two tasks at any given time: It either shows the record addition form, or it performs the SQL queries related to adding a new record. The logic that determines the task begins at line 2, with a test for the value of $_POST. If there is no value in the $_POST superglobal, the user has not submitted the form and therefore needs to see the form. The HTML for the form is placed in a string called $display_block, from lines 446. The script then breaks out of the if...else construct and jumps down to line 136, which outputs the HTML and prints the value of $display_block, in this case the form. Figure 20.2 displays the outcome.

Figure 20.2. The record addition form.


The else condition on Line 48 is invoked if there is a value in $_POST, meaning that the user has submitted the form. In this simple example, two fields have been designated as required fields: the first name and last name of the person. So, lines 5962 check for values in $_POST["f_name"] and $_POST["l_name"] and redirect the user back to the form if either value is missing.

After making it through the check for required fields, we connect to the database in line 65. Next comes the multitude of insertion statements, only one of which is requiredthe insertion of a record into the master_name table. This occurs on lines 6872. After the insertion is made, the id of this record is extracted using mysqli_insert_id() on line 75. We use this value, now referred to as $master_id, in our remaining SQL queries.

The SQL queries for inserting records into the remaining tables are all conditional, meaning they will occur only if some condition is true. In lines 7778, we see that the condition that must be met is that a value exists for any of the following variables:

$_POST["address"], $_POST["city"], $_POST["state"], $_POST["zipcode"].


Lines 8087 create and issue the query if this condition is met.

The same principle holds true for adding to the telephone table (lines 9099), the fax table (lines 101110), the email table (lines 112121), and the personal_notes table (lines 123130). If the conditions are met, records are inserted into those tables.

Once through this set of conditions, the message for the user is placed in the $display_block variable, and the script exits this if...else construct and prints HTML from lines 136145.

Figure 20.3 shows an output of the record addition script.

Figure 20.3. A record has been added.


Add a few records using this form so that you have some values to play with in the following sections. On your own, try to modify this script in such a way that the values entered in the form are printed to the screen after successful record insertion.




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