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