Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. <?php include "../inc/dbinfo.inc"; ?>
  2. <html>
  3. <body>
  4. <h1>Sample page</h1>
  5.  
  6. <?php
  7. /* Connect to MySQL and select the database. */
  8. $connection = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD);
  9. if (mysqli_connect_errno())
  10. echo "Failed to connect to MySQL: " . mysqli_connect_error();
  11.  
  12. $database = mysqli_select_db($connection, DB_DATABASE);
  13. /* Ensure that the Employees table exists. */
  14. VerifyEmployeesTable($connection, DB_DATABASE);
  15. /* If input fields are populated, add a row to the Employees table. */
  16. $employee_name = htmlentities($_POST['Name']);
  17. $employee_address = htmlentities($_POST['Address']);
  18. if (strlen($employee_name) || strlen($employee_address)) {
  19. AddEmployee($connection, $employee_name, $employee_address);
  20. }
  21. ?>
  22.  
  23.  
  24. <!-- Input form -->
  25. <form action="<?PHP echo $_SERVER['SCRIPT_NAME'] ?>" method="POST">
  26. <table border="0">
  27. <tr>
  28. <td>Name</td>
  29. <td>Address</td>
  30. </tr>
  31. <tr>
  32. <td>
  33. <input type="text" name="Name" maxlength="45" size="30" />
  34. </td>
  35. <td>
  36. <input type="text" name="Address" maxlength="90" size="60" />
  37. </td>
  38. <td>
  39. <input type="submit" value="Add Data" />
  40. </td>
  41. </tr>
  42. </table>
  43. </form>
  44. <!-- Display table data. -->
  45. <table border="1" cellpadding="2" cellspacing="2">
  46. <tr>
  47. <td>ID</td>
  48. <td>Name</td>
  49. <td>Address</td>
  50. </tr>
  51. <?php
  52. $result = mysqli_query($connection, "SELECT * FROM Employees");
  53. while($query_data = mysqli_fetch_row($result)) {
  54. echo "<tr>";
  55. echo "<td>" . $query_data[0] . "</td>" .
  56. "<td>" . $query_data[1] . "</td>" .
  57. "<td>" . $query_data[2] . "</td>";
  58. echo "</tr>";
  59. }
  60. ?>
  61. </table>
  62. <!-- Clean up. -->
  63. <?php
  64. mysqli_free_result($result);
  65. mysqli_close($connection);
  66. ?>
  67. </body>
  68. </html>
  69.  
  70.  
  71. <?php
  72. /* Add an employee to the table. */
  73. function AddEmployee($connection, $name, $address) {
  74. $n = mysqli_real_escape_string($connection, $name);
  75. $a = mysqli_real_escape_string($connection, $address);
  76. $query = "INSERT INTO `Employees` (`Name`, `Address`) VALUES ('$n', '$a');";
  77. if(!mysqli_query($connection, $query)) echo("<p>Error adding employee data.</p>");
  78. }
  79.  
  80. /* Check whether the table exists and, if not, create it. */
  81. function VerifyEmployeesTable($connection, $dbName) {
  82. if(!TableExists("Employees", $connection, $dbName))
  83. {
  84. $query = "CREATE TABLE `Employees` (
  85. `ID` int(11) NOT NULL AUTO_INCREMENT,
  86. `Name` varchar(45) DEFAULT NULL,
  87. `Address` varchar(90) DEFAULT NULL,
  88. PRIMARY KEY (`ID`),
  89. UNIQUE KEY `ID_UNIQUE` (`ID`)
  90. ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1";
  91.  
  92. if(!mysqli_query($connection, $query)) echo("<p>Error creating table.</p>");
  93. }
  94. }
  95.  
  96. /* Check for the existence of a table. */
  97. function TableExists($tableName, $connection, $dbName) {
  98. $t = mysqli_real_escape_string($connection, $tableName);
  99. $d = mysqli_real_escape_string($connection, $dbName);
  100. $checktable = mysqli_query($connection, "SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_NAME ='$t' AND TABLE_SCHEMA = '$d'");
  101. if(mysqli_num_rows($checktable) > 0) return true;
  102. return false;
  103. }
  104. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement