Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. <!-- In step 1, you created variables to hold the name of the table ($_POST[table_name]) and the number of fields in that table ($_POST[num_fields]). In this step, you'll create a php script to display additional form elements needed for further definition of the fields: name, type, and length. -->
  2.  
  3. <?php
  4.  
  5. // Check that values were actully entered for $_POST[table_name] and $_POST[num_fields]. If they weren't, direct them back to the form and exit the script.
  6. if ((!$_POST['table_name']) || (!$_POST['num_fields'])) {
  7. header ("Location: show_createtable.html");
  8. exit;
  9. }
  10.  
  11. /* Start building a string called $form_block, starting with the form action and method. Assume that the method is POST and the action is a scriped called do_createtable.php. Remember to escape your quotation marks! Add a hidden field to hold the value of $_POST[table_name], which you'll use at the end of the sequence just to show the user that the proper table has been created. Display your form in an HTML table so that the fields line up nicely. Start with a row of column headings and close the $form_block string for now. */
  12. $form_block = "
  13. <form method=\"POST\" action=\"do_createtable.php\">
  14. <input type=\"hidden\" name=\"table_name\" value=\"$_POST[table_name]\">
  15. <table cellspacing=5 cellpadding=5>
  16. <tr>
  17. <th>Field Name</th><th>Field Type</th><th>Field Length</th>
  18. </tr>";
  19.  
  20. /* Start a for loop to handle the creation of the form fields. Like a while loop, a for loop continues as long as a condition is true. in this case, the for loop starts out with the variable $i having a value of 0, and it continues for as long as $i is less than the value of $_POST[num_fields]. After each loop, $i is incremented by 1. Within the for loop you'll add to the original $form_block. You'll add one row for each field you want to have in your database table. Start with the table row tag abd a table data cell containing an input type for trhe field name. The use of the brackets (the []) after field_name in your input field indicates an arry. for each field you defione in this form, you'll be adding a value to the $_POST[field_name] array. In the next table data cell, create a drop down list containing some common field types. In the final table data cell, create a text field for the length of the field and close your table row. Also close the $form_block string because your done with it for now. */
  21. for ($i = 0; $i < $_POST['num_fields']; $i++) {
  22.  
  23. $form_block .= "
  24. <tr>
  25. <td align=center><input type=\"text\" name=\"field_name[]\"
  26. <size=\"30\"></td>
  27.  
  28. <td align=center>
  29. <select name=\"field_type[]\">
  30. <option value=\"char\">char</option>
  31. <option value=\"date\">date</option>
  32. <option value=\"float\">float</option>
  33. <option value=\"int\">int</option>
  34. <option value=\"text\">text</option>
  35. <option value=\"varchar\">varchar</option>
  36. </select>
  37. </td>
  38.  
  39. <td align=center><input type=\"text\" name=\"field_length[]\"
  40. size=\"5\"></td>
  41. </tr>";
  42.  
  43. // Close the for loop.
  44. }
  45.  
  46. // Add the final chucnk of HTML to the form_block string. You'll add one row that holds the submit button and then close your table and form.
  47. $form_block .= "
  48. <tr>
  49. <td align=center colspan=3><input type=\"submit\" value\"Create Table\"></td>
  50. </tr>
  51. </table>
  52. </form>";
  53.  
  54. ?>
  55.  
  56. <html>
  57. <head>
  58. <title>Create a Database Table: Step 2</title>
  59. </head>
  60. <body>
  61.  
  62. <h1>Define fields for <?php echo "$_POST[table_name]"; ?></h1>
  63.  
  64. <!-- Display contents of $form_block. -->
  65. <?php echo "$form_block"; ?>
  66.  
  67. </body>
  68. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement