Guest User

Untitled

a guest
May 31st, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. <!--
  2. The following HTML is what renders the form. Since this
  3. script is also the processing script for adding a task item to
  4. the database, the action of the form is calling the same
  5. script. This is one of a few different ways that processing
  6. data coming to the web server application from a client (the web browser). This method is fairly safe as long as the data
  7. coming from the web server is "sanitized", meaning we don't insert it into the database until we have cleaned it.
  8. -->
  9. <!DOCTYPE html>
  10. <html>
  11. <body>
  12. <!--
  13. The following code will display the lists that are stored in the
  14. list table. The list will be displayed as an HTML hyperlink with
  15. the list id in the get string. This will ensure that the id for
  16. the list (which groups task list items) will be available when the task list item is inserted.
  17. -->
  18. <h2>List Table</h2>
  19.  
  20. <table>
  21. <tr>
  22. <th>Id</th>
  23. <th>List</th>
  24. </tr>
  25. <?php
  26. include "connectdb.php";
  27. $servername = "localhost";
  28. $username = "root";
  29. $password = "";
  30. $dbname = "tasklist";
  31. // Create connection
  32. $conn = connectdb($servername, $username, $password, $dbname);
  33.  
  34. $sql = "SELECT id,description FROM list";
  35. $result = $conn->query($sql);
  36. if ($result->num_rows > 0) {
  37. // output data of each row
  38. while($row = $result->fetch_assoc()) {
  39. echo "<tr><td>" . $row['id']. '</td><td><a href="?id=' .$row['id'].'">'. $row['description']. "</a><td></tr>";
  40. }
  41. }
  42. ?>
  43. </table>
  44.  
  45. <?php
  46.  
  47. /*
  48. This is interesting, the HTML code will only display if the
  49. there is an id on the get string of the URL
  50. */
  51. if(filter_has_var(INPUT_GET,'id'))
  52. {
  53. // Get the ID of the list, this was attached to the URL and
  54. // will come in when the URL is clicked on the list table --
  55. // The HTML table that has the list descriptions and was
  56. // rendered above.
  57. $list_id = filter_input(INPUT_GET,'id',FILTER_SANITIZE_NUMBER_INT);
  58. ?>
  59. <h2>Add a Task Item</h2>
  60.  
  61. <form action="insert_and_display_task_items.php?id=<?php echo $list_id; ?>" method="post">
  62. Task Item Description: <input type="text" name="description"><br>
  63. Completed: <input type="text" name="completed" size="1"><br>
  64. <input type="submit">
  65. </form>
  66.  
  67. <?php
  68. /* Make sure that we close the if statement*/
  69. }
  70. ?>
  71.  
  72. <!--
  73. The following PHP code is for processing the data coming from the form.
  74. This code will also select all data from the list table of the tasklist
  75. database and will display the data in a table. The example code in this repository
  76. display.php has some good ideas for how to display the code. Put it after the
  77. "filter and insert into the database" step
  78. -->
  79. <?php
  80.  
  81. /*
  82. 1. Check if there is data in the $_POST array
  83. We will be using the filter functions which
  84. do a pretty good job of filtering out potentially dangerous characters and possible attacks such as cross site scripting.
  85. 2. If there is data in the $_POST and/or $_GET array, clean it and prepare the SQL statement
  86.  
  87. */
  88. // filter and insert data into the database if available
  89. if(filter_has_var(INPUT_GET,'id'))
  90. {
  91.  
  92. // process the form if there is data in it.
  93. if(filter_has_var(INPUT_POST,'description'))
  94. {
  95. $description = filter_input(INPUT_POST,'description',FILTER_SANITIZE_SPECIAL_CHARS);
  96. $completed = filter_input(INPUT_POST,'completed',FILTER_SANITIZE_SPECIAL_CHARS);
  97. $sql = "INSERT INTO listitem (id, list_id,description,completed) VALUES (NULL,'$list_id','$description','$completed')";
  98.  
  99. if ($conn->query($sql) === TRUE) {
  100. print "New list item record successfully created";
  101. }
  102. else {
  103. echo "Error: " . $sql . "<br>" . $conn->error;
  104. }
  105.  
  106. }
  107. }
  108.  
  109. ?>
  110. <!--
  111. @Student Code@
  112. Put the task item display code after this comment, this will display the items as a table. This is very similar to the last assignment.
  113. -->
  114.  
  115. <?php
  116. $conn->close();
  117. ?>
  118.  
  119. </body>
  120. </html>
Add Comment
Please, Sign In to add comment