· 9 years ago · Oct 30, 2016, 05:50 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
10if (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. */
15VerifyEmployeesTable($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
21if (strlen($employee_name) || strlen($employee_address)) {
22AddEmployee($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
59 while($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
68mysqli_free_result($result);
69mysqli_close($connection);
70
71?>
72
73</body>
74
75<?php
76
77/* Add an employee to the table. */
78function AddEmployee($connection, $name, $address) {
79$n = mysqli_real_escape_string($connection, $name);
80$a = mysqli_real_escape_string($connection, $address);
81
82$query = "INSERT INTO `Employees` (`Name`, `Address`) VALUES ('$n', '$a');";
83
84if(!mysqli_query($connection, $query)) echo("<p>Error adding employee data.</p>");
85
86/* Check whether the table exists and, if not, create it. */
87 function VerifyEmployeesTable($connection, $dbName) {
88 if(!TableExists("Employees", $connection, $dbName))
89{
90 $query = "CREATE TABLE `Employees` (
91 `ID` int(11) NOT NULL AUTO_INCREMENT,
92 `Name` varchar(45) DEFAULT NULL,
93 `Address` varchar(90) DEFAULT NULL,
94 PRIMARY KEY (`ID`),
95 UNIQUE KEY `ID_UNIQUE` (`ID`)
96 ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1";
97
98 if(!mysqli_query($connection, $query)) echo("<p>Error creating table.</p>");
99 }
100
101/* Check for the existence of a table. */
102 function TableExists($tableName, $connection, $dbName) {
103 $t = mysqli_real_escape_string($connection, $tableName);
104$d = mysqli_real_escape_string($connection, $dbName);
105
106$checktable = mysqli_query($connection,
107 "SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_NAME = '$t' AND TABLE_SCHEMA = '$d'");
108
109if(mysqli_num_rows($checktable) > 0) return true;
110
111return false;
112}
113?>
114
115<tr>
116 <td>ID</td>
117 <td>Name</td>
118 <td>Address</td>
119 <td>Delete</td>
120</tr>
121
122<?php
123
124$result = mysqli_query($connection, "SELECT * FROM Employees");
125
126while($query_data = mysqli_fetch_row($result)) {
127echo "<tr>";
128echo "<td>",$query_data[0], "</td>",
129 "<td>",$query_data[1], "</td>",
130 "<td>",$query_data[2], "</td>";
131echo "</tr>";
132}
133?>