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