-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedit.php
More file actions
123 lines (100 loc) · 3.39 KB
/
edit.php
File metadata and controls
123 lines (100 loc) · 3.39 KB
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
<?php
require_once './db.php';
if($_SERVER['REQUEST_METHOD']==='POST') {
//Get table name
$table=$_POST["table"];
$pk=$_POST["pk"];
$id=$_POST["id"];
//Remove that part from the POST array
unset($_POST["table"]);
unset($_POST["pk"]);
unset($_POST["id"]);
//Check which which attribute is auto_increment (the one that's empty) and prepare the UPDATE statement
$values=array();
foreach ($_POST as $column => $value){
array_push($values, $column."='".$value."'");
}
$new_values=implode(",", $values);
//Execute UPDATE statement
$stmt=$conn->prepare("UPDATE {$table} SET {$new_values} WHERE {$pk}='{$id}'");
if($stmt->execute()){
header("Location: ./index.php?table={$table}&edited");
}else{
header("Location: ./index.php?table={$table}&error={$stmt->error}");
}
$conn->close();
die();
}
//Get selected 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 table columns
$stmt=$conn->prepare("SHOW FULL COLUMNS FROM {$selected_table}");
$stmt->execute();
$columns=$stmt->get_result();
//Get data from the table
$pk_column=$_GET["pk"];
$id_record=$_GET["id"];
$stmt=$conn->prepare("SELECT * FROM {$selected_table} WHERE {$pk_column}='{$id_record}'");
$stmt->execute();
$rows=$stmt->get_result();
$data=$rows->fetch_assoc();
?>
<!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="./edit.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'] . " - edit 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' value='{$data[$input['Field']]}' name='{$input['Field']}' ".(($input["Extra"]=="auto_increment") ? "readonly" : "required").">
</div>
</div>
";
}
?>
</div>
</div>
</div>
<input type="hidden" name="table" value="<?= $selected_table ?>">
<input type="hidden" name="pk" value="<?= $pk_column ?>">
<input type="hidden" name="id" value="<?= $id_record ?>">
<button type="submit" class="btn btn-success float-right">Save Changes</button>
</form>
</div>
</div>
</div>
</body>
</html>