-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew.php
110 lines (86 loc) · 2.84 KB
/
new.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
<?php
require_once './db.php';
if($_SERVER['REQUEST_METHOD']==='POST') {
//Get table name
$table=$_POST["table"];
//Remove it from the POST array
unset($_POST["table"]);
//Check which which attribute is auto_increment (the one that's empty)
foreach($_POST as &$var){
if(empty($var)) $var="DEFAULT";
else $var="'".$var."'";
}
//Separate all values with comma to prepare the SQL statement
$values = implode(",", $_POST);
$stmt=$conn->prepare("INSERT INTO {$table} VALUES({$values})");
if($stmt->execute()){
header("Location: ./index.php?table={$table}&success");
}else{
header("Location: ./index.php?table={$table}&error={$stmt->error}");
}
$conn->close();
}
//Get table name
$selected_table=$_GET["table"];
//Get table display name
$stmt=$conn->prepare("SHOW TABLE STATUS FROM {$database}");
$stmt->execute();
$tables=$stmt->get_result();
//Get columns
$stmt=$conn->prepare("SHOW FULL COLUMNS FROM {$selected_table}");
$stmt->execute();
$columns=$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">
<form method="post" action="./new.php">
<div class="card" style="margin-bottom: 20px">
<div class="card-header">
<div class="d-flex align-items-center">
<div class="mr-auto">
<?php
while($table=$tables->fetch_assoc()){
echo ($_GET["table"]==$table['Name']) ? $table['Comment'] . " - new record" : "";
}
?>
</div>
</div>
</div>
<div class="card-body">
<div class="row">
<?php
while($input=$columns->fetch_assoc()){
echo "
<div class='col-6'>
<div class='form-group'>
<label>${input['Comment']}:</label>
<input type='text' class='form-control' name='${input['Field']}' ".(($input["Extra"]=="auto_increment")?"readonly":"required").">
</div>
</div>
";
}
?>
</div>
</div>
</div>
<input type="hidden" name="table" value="<?= $selected_table ?>">
<button type="submit" class="btn btn-success float-right">Add Record</button>
</form>
</div>
</div>
</div>
</body>
</html>