-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex2.php
382 lines (352 loc) · 16.4 KB
/
index2.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
<?php
/**
* @license
* Copyright(c) 2002-2019 Jose Ricardo Mantilla Mantilla. All Rights Reserved.
* Author: Jose Ricardo Mantilla Mantilla <[email protected]> / <[email protected]>
* Website Author: http://pepiuox.net/ / http://labemotion.net/
* Author's licenses: http://pepiuox.net/license / http://labemotion.net/license
* Project Name: EasyCRUD
* Contributors:
* Website Contributors:
* Library list.
* Bootstrap http://www.getbootstrap.com
* Jquery http://www.jquery.com ,
* Poppers https://popper.js.org/
* This file is a module or application for make simple CRUD based in PHP.
* Use of this source code is governed by an PEPIUOX / LAB EMOTION License included
* MIT License that can be found in the LICENSE file in the root directory of
* this source tree.
* Distributed under the MIT License
* (license terms are at http://opensource.org/licenses/MIT).
* */
ob_start();
require_once("db.php");
$path = basename($_SERVER['REQUEST_URI']);
$file = basename($path);
$fileName = basename($_SERVER['PHP_SELF']);
if ($file == $fileName) {
header("Location: index.php?view=select");
}
function protect($string) {
$protection = htmlspecialchars(trim($string), ENT_QUOTES);
return $protection;
}
/* Get id table as first column */
function getID($tble) {
global $link;
if (!empty(protect($_GET['tbl']))) {
$tble = protect($_GET['tbl']);
$query = "SELECT * from " . $tble;
if ($result = $link->query($query)) {
/* Get field information for first column */
$result->field_seek(0);
$deletemeta = $result->fetch_field();
return $deletemeta->name;
}
}
}
$view = protect($_GET['view']);
?>
<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset = "UTF-8" name = "viewport" content = "width-device=width, initial-scale=1" />
<title><?php
if (isset($_GET['tbl'])) {
$titleTbl = str_replace("_", " ", protect($_GET['tbl']));
}
if ($view === "select") {
echo 'Select your table - PHP CRUD';
} elseif ($view === "list") {
echo 'List ' . $titleTbl . ' - PHP CRUD';
} elseif ($view === "add") {
echo 'Add ' . $titleTbl . ' - PHP CRUD';
} elseif ($view === "edit") {
echo 'Edit ' . $titleTbl . ' - PHP CRUD';
} elseif ($view === "delete") {
echo 'Delete ' . $titleTbl . ' - PHP CRUD';
}
?></title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-4bw+/aepP/YC94hEpVNVgiZdgIC5+VKNBQNGCHeKRQN+PtmoHDEXuppvnDJzQIu9" crossorigin="anonymous">
<script src="https://unpkg.com/@popperjs/core@2"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-HwwvtgBNo3bZJJLYd8oVXjrBZt8cqVSpeBNS5n7C8IVInixGAoxmnlMuBnhbgrkm" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js" integrity="sha512-v2CJ7UaYy4JwqLDIrZUI/4hqeoQieOmAZNXBeQyjo21dadnwR+8ZaIJVT8EE2iyI61OV8e6M8PP2/4hpQINQ/g==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<style>
body{
font-size: 14px;
}
</style>
</head>
<body>
<?php
/* Select table */
if ($view === "select") {
?>
<div class="container">
<div class = "row">
<div class="col-md-6">
<h3>Form </h3>
</div>
<div class="col-md-6">
<div class="form-group">
<script>
$(function () {
$("#selecttb").change(function (e) {
e.preventDefault();
var selecttb = $(this).val();
//var path = $(location).attr('href');
var url = 'index.php?view=list&tbl=' + selecttb;
window.location.replace(url);
});
});
</script>
<label class="control-label" for="selecttb">Select Table</label>
<select id="selecttb" name="selecttb" class="form-control">
<option value="">Select Table</option>
<?php
/* Get table names */
$tableList = array();
$res = $link->query("SHOW TABLES");
while ($row = $res->fetch_array()) {
$tableList[] = $row[0];
}
foreach ($tableList as $tname) {
$remp = str_replace("_", " ", $tname);
echo '<option value="' . $tname . '">' . ucfirst($remp) . '</option>' . "\n";
}
?>
</select>
</div>
</div>
</div>
</div>
<?php
/* View data in the selected table */
} elseif ($view == "list") {
if (!empty(protect($_GET['tbl']))) {
$tble = protect($_GET['tbl']);
?>
<div class="container-fluid">
<div class="row">
<div class="w-100">
<?php
echo '<h2><a href="index.php?view=select" class="btn btn-primary">Back to select</a> List ' . $titleTbl . '</h2>';
?>
</div>
<div class="w-100">
<?php
echo '<form method="POST">' . "\n";
echo '<table class="table table-bordered">' . "\n";
echo '<thead class="bg-info">' . "\n";
echo '<tr>' . "\n";
$sql = "SELECT * FROM $tble";
$result = $link->query($sql);
$i = 0;
while ($i < mysqli_num_fields($result)) {
$meta = mysqli_fetch_field($result);
$remp = str_replace("_", " ", $meta->name);
echo '<th>' . ucfirst($remp) . '</th>' . "\n";
$i = $i + 1;
}
echo '<th><a id="addrow" name="addrow" class="btn btn-primary" href="index.php?view=add&tbl=' . $tble . '">Add new</a></th>' . "\n";
echo '</tr>' . "\n";
echo '</thead>' . "\n";
echo '<tbody>' . "\n";
while ($row = mysqli_fetch_row($result)) {
echo '<tr>' . "\n";
$count = count($row);
$y = 0;
while ($y < $count) {
$c_row = current($row);
if ($y == 0) {
echo '<td id="' . $c_row . '">' . $c_row . '</td>' . "\n";
} else {
echo '<td>' . $c_row . '</td>' . "\n";
}
next($row);
$y = $y + 1;
}
$i_row = $row[0];
echo '<td><!-- Button -->
<a id="editrow" name="editrow" class="btn btn-success" href="index.php?view=edit&tbl=' . $tble . '&id=' . $i_row . '">Edit</a>
<a id="deleterow" name="deleterow" class="btn btn-danger" href="index.php?view=delete&tbl=' . $tble . '&id=' . $i_row . '">Delete</a>
</td>';
echo '</tr>' . "\n";
$i = $i + 1;
}
echo '</tbody>' . "\n";
echo '</table>' . "\n";
echo '</form>' . "\n";
?>
</div>
</div>
</div>
<?php
} else {
header("Location: index.php?view=select");
}
/* Add data in the selected table */
} elseif ($view == "add") {
if (!empty(protect($_GET['tbl']))) {
$tble = protect($_GET['tbl']);
?>
<div class="container">
<div class="row">
<div class="w-100">
<?php
echo '<h2><a href="index.php?view=list" class="btn btn-primary">Back to list</a> Add ' . $tble . '</h2>';
?>
</div>
<div class="w-100">
<?php
echo '<form class="form-horizontal">
<fieldset>
<!-- Form Name -->
<legend>' . $tble . '</legend>';
$addQuery = 'SELECT * FROM ' . $tble;
$addResult = $link->query($addQuery);
$idCol = getID($tble);
/* Init loop */
if (mysqli_num_fields($addResult) > 0) {
$addmetas = $addResult->fetch_fields();
foreach ($addmetas as $addmeta) {
$remp = str_replace("_", " ", $addmeta->name);
if ($addmeta->name === $idCol) {
continue;
} else {
echo '<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="textinput">' . ucfirst($remp) . '</label>
<div class="col-md-4">
<input id="' . $addmeta->name . '" name="' . $addmeta->name . '" placeholder="' . ucfirst($remp) . '" class="form-control input-md" type="text">
<small class="form-text text-muted">' . ucfirst($remp) . '</small>
</div>
</div>';
}
}
}
/* End loop */
echo '<!-- Button -->
<div class="form-group">
<div class="col-md-4">
<button id="addrow" name="addrow" class="btn btn-primary">Save</button>
</div>
</div>';
echo '</fieldset>
</form>';
} else {
header("Location: index.php?view=select");
}
?>
</div>
</div>
</div>
<?php
/* Edid data in the selected table */
} elseif ($view == "edit") {
if (!empty(protect($_GET['tbl'])) || !empty(protect($_GET['id']))) {
$tble = protect($_GET['tbl']);
$id = protect($_GET['id']);
$idCol = getID($tble);
?>
<div class="container">
<div class="row">
<div class="w-100">
<?php
echo '<h2><a href="index.php?view=list" class="btn btn-primary">Back to list</a> Edit ' . $tble . '</h2>';
?>
</div>
<div class="w-100">
<?php
echo '<form class="form-horizontal">
<fieldset>
<!-- Form Name -->
<legend>' . $tble . '</legend>';
$editQuery = "SELECT * FROM $tble WHERE ";
$editQuery .= $idCol . "=" . $id;
$editResult = $link->query($editQuery);
if (mysqli_num_fields($editResult) > 0) {
$editmetas = $editResult->fetch_fields();
$rqu = $editResult->fetch_array();
foreach ($editmetas as $editmeta) {
$remp = str_replace("_", " ", $editmeta->name);
if ($editmeta->name === $idCol) {
continue;
} else {
echo '<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="textinput">' . ucfirst($remp) . '</label>
<div class="col-md-4">
<input id="' . $editmeta->name . '" name="' . $editmeta->name . '" value="' . $rqu[$editmeta->name] . '" class="form-control input-md" type="text">
<small class="form-text text-muted">' . ucfirst($remp) . '</small>
</div>
</div>';
}
}
}
echo '<!-- Button -->
<div class="form-group">
<div class="col-md-4">
<button id="editrow" name="editrow" class="btn btn-primary">Edit</button>
</div>
</div>';
echo '</fieldset>
</form>';
?>
</div>
</div>
</div>
<?php
} else {
header("Location: index.php?view=select");
}
/* detele data in the selected table */
} elseif ($view == "delete") {
if (!empty(protect($_GET['tbl'])) || !empty(protect($_GET['id']))) {
$tble = protect($_GET['tbl']);
$id = protect($_GET['id']);
$idCol = getID($tble);
?>
<div class="container">
<div class="row">
<div class="w-100">
<?php
echo '<h2><a href="index.php?view=list" class="btn btn-primary">Back to list</a> Delete ' . $tble . '</h2>';
?>
</div>
<div class="w-100">
<?php
$deletequery = "SELECT * FROM $tble WHERE $idCol = '$id' ";
$deleteresult = $link->query($deletequery);
echo '<form role="form" id="delete_' . $tble . '" method="POST">
<legend>' . $tble . '</legend>' . "\n";
$deletemetas = $deleteresult->fetch_fields();
$drow = $deleteresult->fetch_array();
foreach ($deletemetas as $deletemeta) {
$cdta = $drow[$deletemeta->name];
if ($deletemeta->name === $idCol) {
continue;
} else {
$remp = str_replace("_", " ", $deletemeta->name);
echo '<div class="form-group">
<label for="' . $deletemeta->name . '">' . ucfirst($remp) . ':</label>
<input type="text" class="form-control" id="' . $deletemeta->name . '" name="' . $deletemeta->name . '" value="' . $cdta . '" readonly>
</div>' . "\n";
}
}
echo '<div class="form-group">
<button type = "submit" id="deleterow" name="deleterow" class="btn btn-primary"><span class = "glyphicon glyphicon-plus"></span> Delete</button>
</div>' . "\n";
echo '</form>' . "\n";
}
?>
</div>
</div>
</div>
<?php
} else {
header("Location: index.php?view=select");
}
?>
</body>
</html>