Html site 9 5/1/2017


SUBMITTED BY: canonical

DATE: May 2, 2017, 1:55 a.m.

FORMAT: HTML

SIZE: 2.9 kB

HITS: 332

  1. If you want more of my pastes visit: https://randompaste.000webhostapp.com/index.html
  2. --------------------------------------------------------------------------------------
  3. <html>
  4. <head>
  5. <title>Task3</title>
  6. </head>
  7. <body>
  8. <div id="body">
  9. <?php include 'menu.inc'; ?>
  10. <?php
  11. //define server variables
  12. $servername = "localhost";
  13. $username = 'root' ;
  14. $password= '';
  15. $dbname = "task3";
  16. //define connection
  17. $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  18. $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
  19. $sql = "SELECT * FROM modules";
  20. //output in a table form
  21. echo "<h4>SQL Query using PDO showing results in Table form</h4>";
  22. echo "<table style='border:1px solid black;'> ";
  23. echo "<tr><th>Code</th><th>Name</th><th>Lecturer</th></tr>";
  24. foreach ($conn->query($sql) as $row) {
  25. echo "<tr> <td style='width:250px;border:1px solid black;'>" . $row["code"] .
  26. "</td> <td style='width:250px;border:1px solid black;'>" . $row["name"] .
  27. "</td> <td style='width:250px;border:1px solid black;'>" . $row["lecturer"] . "</td> </tr>";
  28. }
  29. //end for each
  30. //defining variables to enter into database
  31. $sqlValueCode="";
  32. $sqlValueName="";
  33. $sqlValueLecturer="";
  34. ?>
  35. <!--ADD Table to receive data for database-->
  36. </table>
  37. <br>
  38. <br>
  39. <!--Add Form-->
  40. <form action="task4.php" method="post">
  41. <strong>Add Form</strong><br>
  42. Code <br><input type="text" name="modCode"><br>
  43. Module Name <br><input type="text" name="modName"><br>
  44. Lecturer <br><input type="text" name="lectName"><br>
  45. <input type="submit">
  46. </form>
  47. <?php
  48. //reading in data from Add Form
  49. $modCode = filter_input (INPUT_POST, 'modCode' );
  50. $modName = filter_input (INPUT_POST, 'modName' );
  51. $lectName = filter_input (INPUT_POST, 'lectName' );
  52. //setting up variables to enter into database
  53. $sqlValueCode=$modCode;
  54. $sqlValueName=$modName;
  55. $sqlValueLecturer=$lectName;
  56. //testing output
  57. //echo "<br>". $modCode . "<br>" . $modName . "<br>". $lectName ;
  58. //define sql insert command
  59. $sqlAdd= "INSERT INTO modules (code, name, lecturer) values ("."'$sqlValueCode',"." '$sqlValueName',"." '$sqlValueLecturer')";
  60. //only inserting data if variables have been set
  61. if ($sqlValueCode != NULL or $sqlValueName != NULL or $sqlValueLecturer != NULL) {
  62. //executing sql insert command
  63. $conn->exec($sqlAdd);
  64. echo "entry added successfully";
  65. } else {
  66. echo "could not add the entry";
  67. }
  68. ?>
  69. <!--Update Form...................................................................-->
  70. <br><br>
  71. <form action="task4.php" method="post">
  72. <strong>Update Form</strong><br>
  73. Code <br><input type="text" name="modCode"><br>
  74. Module Name <br><input type="text" name="modName"><br>
  75. Lecturer <br><input type="text" name="lectName"><br>
  76. <input type="submit">
  77. </form>
  78. </div>
  79. </body>
  80. </html>

comments powered by Disqus