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