· 6 years ago · Oct 15, 2019, 01:36 AM
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 VerifyStudentsTable($connection, DB_DATABASE);
16
17 /* If input fields are populated, add a row to the EMPLOYEES table. */
18 $student_name = htmlentities($_POST['NAME']);
19 $student_lname = htmlentities($_POST['L_NAME']);
20
21 if (strlen($student_name) || strlen($student_lname)) {
22 AddStudent($connection, $student_name, $student_lname);
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>LAST NAME</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="L_NAME" 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>LAST NAME</td>
53 </tr>
54
55<?php
56
57$result = mysqli_query($connection, "SELECT * FROM STUDENTS");
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 AddStudent($connection, $name, $lname) {
86 $n = mysqli_real_escape_string($connection, $name);
87 $a = mysqli_real_escape_string($connection, $lname);
88
89 $query = "INSERT INTO STUDENTS (NAME, L_NAME) 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 VerifyStudentsTable($connection, $dbName) {
96 if(!TableExists("STUDENTS", $connection, $dbName))
97 {
98 $query = "CREATE TABLE STUDENTS (
99 ID int(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
100 NAME VARCHAR(45),
101 L_NAME VARCHAR(90)
102 )";
103
104 if(!mysqli_query($connection, $query)) echo("<p>Error creating table.</p>");
105 }
106}
107
108/* Check for the existence of a table. */
109function TableExists($tableName, $connection, $dbName) {
110 $t = mysqli_real_escape_string($connection, $tableName);
111 $d = mysqli_real_escape_string($connection, $dbName);
112
113 $checktable = mysqli_query($connection,
114 "SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_NAME = '$t' AND TABLE_SCHEMA = '$d'");
115
116 if(mysqli_num_rows($checktable) > 0) return true;
117
118 return false;
119}
120?>