-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
125 lines (102 loc) · 3.83 KB
/
index.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
<?php
require_once './db.php';
//Get all tables
$stmt=$conn->prepare("SHOW TABLE STATUS FROM {$database}");
$stmt->execute();
$tables=$stmt->get_result();
//Get selected table name, default is the first table
$selected_table=$tables->fetch_assoc()["Name"];
if(isset($_GET["table"])){
$selected_table=$_GET["table"];
}
$tables->data_seek(0);
//Get table column names
$stmt=$conn->prepare("SHOW FULL COLUMNS FROM {$selected_table}");
$stmt->execute();
$columns=$stmt->get_result();
//Clicked on delete record
if(isset($_GET["delete"])){
$pk_column=$_GET["pk"];
$id_record=$_GET["delete"];
$stmt=$conn->prepare("DELETE FROM {$selected_table} WHERE {$pk_column}='{$id_record}'");
$stmt->execute();
}
//Get data from the selected table
$stmt=$conn->prepare("SELECT * FROM {$selected_table}");
$stmt->execute();
$rows=$stmt->get_result();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<? include './templates/head.php' ?>
</head>
<body>
<? include './templates/navbar.php' ?>
<div class="container-fluid">
<div class="row">
<div class="col-md-2" style="padding-bottom: 25px">
<div class="list-group">
<? include './templates/table_list.php' ?>
</div>
</div>
<div class="col-md-10">
<?php
if(isset($_GET["success"])){
echo '<div class="alert alert-success" role="alert">New record has been successfully added!</div>';
}
if(isset($_GET["error"])){
if(empty($_GET["error"])) $_GET["error"]="Error: cannot insert the new record!";
echo "<div class='alert alert-danger' role='alert'>{$_GET["error"]}</div>";
}
if(isset($_GET["edited"])){
echo "<div class='alert alert-success' role='alert'>Record has been successfully modified!</div>";
}
?>
<div class="card">
<div class="card-header">
<div class="d-flex align-items-center">
<div class="mr-auto">
<?php
while($table=$tables->fetch_assoc()){
echo ($selected_table==$table['Name']) ? $table['Comment'] : "";
}
?>
</div>
<a class="btn btn-success float-right" href="./new.php?table=<?= $_GET["table"] ?>" role="button"><i class="fa fa-plus"></i> New Record</a>
</div>
</div>
<div class="card_data-body">
<table class="table table-bordered table-hover">
<thead>
<tr>
<?php
while($column=$columns->fetch_assoc()){
echo "<th><small><b>${column['Comment']}</b></small></th>";
if($column["Key"]=="PRI") $primary_key=$column['Field'];
}
?>
<th style="width:10%"><small><b></b></small></th>
</tr>
</thead>
<tbody>
<?php
while($row=$rows->fetch_assoc()){
echo '<tr>';
foreach($row as $row_column){
echo "<td>{$row_column}</td>";
}
$id=reset($row);
echo "<td class='text-center'><a href='./edit.php?table={$selected_table}&pk={$primary_key}&id={$id}' style='color:black'><i class='fa fa-edit'></i></a> <a href='./?table={$selected_table}&pk={$primary_key}&delete={$id}' style='color:black'><i class='fa fa-trash fluid'></i></a></td>";
echo '</tr>';
}
?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</body>
</html>