-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInsertSelectDB.php
290 lines (175 loc) · 8.74 KB
/
InsertSelectDB.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?php //index.php
class InsertSelectDB{
//Connection variable
private $connection;
//connection to MySQL works continue the program Attempt to connect to the Database
public function connectToDBMS(){
require_once ('mylogin.php');
$this->connection = new mysqli($hn, $un, $pw, $db);
if($this->connection===FALSE)
die("Fatal Error - Not possible to connect to MySQL <br>" . mysqli_connect_error());
else
return TRUE;
}
//Attributes
private $first_name;
private $last_name;
private $birthday_year;
private $email_address;
private $phone_number;
//getters and setters
public function getFirstName()
{
return $this->first_name;
}
public function getLastName()
{
return $this->last_name;
}
public function getBirthdayYear()
{
return $this->birthday_year;
}
public function getPhoneNumber()
{
return $this->phone_number;
}
public function getEmailAddress()
{
return $this->email_address ;
}
//Constructor and destructor
function __construct($fname,$lname,$birthday_year,$email,$p_number){
$this->first_name = $fname;
$this->last_name = $lname;
$this->birthday_year = $birthday_year;
$this->email_address = $email;
$this->phone_number = $p_number;
}
function __destruct(){
$this->connection->close();
}
private function sqlQueries(){
$select = "SELECT * FROM custprofile";
return array($select);
}
private function connectToDB(){
$check_connect_to_db = mysqli_select_db($this->connection, 'customers');
if($check_connect_to_db===FALSE)
die("Fatal Error - Attempt to create DB but not possible to connect to it" . $connection->error);
return TRUE;
}
private function createDB(){
$sql_create_db = "CREATE DATABASE customers";
$create_db = $this->connection->query($sql_create_db);
if($create_db===FALSE)
die("Fatal Error - DB cannot be created" . $connection->error);
return TRUE;
}
private function connectToTable(){
$sql_desc_table = "DESC custprofile";
$check_table_exists = $this->connection->query($sql_desc_table);
if($check_table_exists===FALSE)
die("Fatal Error - Table does not exist" . $connection->error);
return TRUE;
}
private function createTableNColumns(){
$sql_create_table = "CREATE TABLE custprofile(
id INT(5) PRIMARY KEY AUTO_INCREMENT,
fname VARCHAR(55) NOT NULL,
lname VARCHAR(55) NOT NULL,
bday VARCHAR(55) NOT NULL,
email VARCHAR(30) NOT NULL,
pnumber VARCHAR(15) NOT NULL); ";
$create_table = $this->connection->query($sql_create_table);
//If table creation fails display an error message
if($create_table===FALSE and $this->connection->error!=="Table 'custprofile' already exists")
die("Fatal Error - Table customers cannot be created" . $this->connection->error);
return TRUE;
}
private function addToTables(){
$sql_insert_query="INSERT INTO custprofile (fname, lname, bday, email, pnumber)
VALUES ('$this->first_name', '$this->last_name',
'$this->birthday_year', '$this->email_address', '$this->phone_number') ";
$insert_query = $this->connection->query($sql_insert_query);
if($insert_query===FALSE)
die("Fatal Error - Data cannot be inserted in the table<br>" . $connection->error);
return TRUE;
}
private function receivedFromTables(String $flag){
if ($flag==="1"){
$sql_select_query = "SELECT * FROM custprofile";
echo 'Find below the data retreived from the database<br>';
}else{
$sql_select_query = "SELECT * FROM custprofile ORDER BY ID DESC LIMIT 1";
echo 'Your data have been saved to the DB<br>';
echo 'Find the data below<br>';
}
$select_query = $this->connection->query($sql_select_query);
if ($select_query === FALSE)
die("Fatal Error - Query cannot be performed in the table" . $connection->error);
else{
//If the query works gets the number of rows in the result
$number_of_rows = $select_query->num_rows;
//Use a loop to display each row
for ($j = 0 ; $j < $number_of_rows ; ++$j){
$each_row = $select_query->fetch_array(MYSQLI_ASSOC);
echo ' ID : ' . htmlspecialchars($each_row['id']) . '<br>';
echo 'First Name : ' . htmlspecialchars($each_row['fname']) . '<br>';
echo ' Last Name : ' . htmlspecialchars($each_row['lname']) . '<br>';
echo ' Nick Name : ' . htmlspecialchars($each_row['bday']) . '<br>';
echo ' Email : ' . htmlspecialchars($each_row['email']) . '<br>';
echo ' Phone : ' . htmlspecialchars($each_row['pnumber']) . '<br><br>';
}
}
$select_query->close();
}
public function select(){
$this->connectToDBMS();
$this->connectToDB();
$this->receivedFromTables("1");
}
public function insert(){
//If connection to the MySQL fails display an error message
if ($this->connectToDBMS() === TRUE) {
if($this->connectToDB() === TRUE){
if($this->connectToTable()===TRUE){
if($this->createTableNColumns()===TRUE){
if($this->addToTables()===TRUE){
$this->receivedFromTables("0");
}
}else{
if($this->addToTables()===TRUE){
$this->receivedFromTables("0");
}
}
}
}else{
if ($this->createDB() === TRUE){
if($this->connectToTable()===FALSE){
if($this->createTableNColumns()===TRUE){
if($this->addToTables()===TRUE){
$this->receivedFromTables("0");
}
}else{
if($this->addToTables()===TRUE){
$this->receivedFromTables("0");
}
}
}else{
if($this->createTableNColumns()===TRUE){
if($this->addToTables()===TRUE){
$this->receivedFromTables("0");
}
}else{
if($this->addToTables()===TRUE){
$this->receivedFromTables("0");
}
}
}
}
}
}
}
}
?>