This repository has been archived by the owner on Mar 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.php
89 lines (73 loc) · 2.32 KB
/
update.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
<?php
require_once('./header.php');
require_once('./db_connect.php');
if(isset($_POST['id'])){
$id=$_POST['id'];
}else{
$id=$_GET['id'];
}
print '<h3 align="center">'.ucfirst($table).'</h3>';
?>
<div class="container" align="center">
<div class="row">
<div class="col-md-3"></div>
<div class="col-md-6">
<form method="post" action="update.php?table=<?=$table?>">
<table class="table table-bordered table-responsive table-hover">
<?php
$sth = $pdo->prepare("SELECT * from $table WHERE id = :id");
$sth->bindValue(':id', $id, PDO::PARAM_STR); // No select e no delete basta um bindValue
$sth->execute();
$reg = $sth->fetch(PDO::FETCH_OBJ);
$sql = "SELECT * FROM $table";
$sth = $pdo->query($sql);
$numfields = $sth->columnCount();
for($x=0;$x<$numfields;$x++){
$meta = $sth->getColumnMeta($x);
$field = $meta['name'];
?>
<tr><td><b><?=ucfirst($field)?></td><td><input type="text" name="<?=$field?>" value="<?=$reg->$field?>"></td></tr>
<?php
}
?>
<input name="id" type="hidden" value="<?=$id?>">
<tr><td></td><td><input name="send" class="btn btn-primary" type="submit" value="Update">
<input name="send" class="btn btn-warning" type="button" onclick="location='index.php?table=<?=$table?>'" value="Back"></td></tr>
</table>
</form>
</div>
<div>
</div>
<?php
if(isset($_POST['send'])){
$set='';
$sths='';
$numfields = $sth->columnCount();
for($x=0;$x<$numfields;$x++){
$meta = $sth->getColumnMeta($x);
$field = $meta['name'];
$$field = $_POST[$field];
if($x<$numfields-1){
if($x==0) continue;
$set .= "$field=:$field,";
}else{
if($x==0) continue;
$set .= "$field=:$field";
}
}
$sql = "UPDATE $table SET $set WHERE id = :id";
$sth = $pdo->prepare($sql);
for($x=0;$x<$numfields;$x++){
$select = $pdo->query("SELECT * FROM $table");
$meta = $select->getColumnMeta($x);
$field=$meta['name'];
$sth->bindParam(":$field", $_POST["$field"], PDO::PARAM_INT);
}
if($sth->execute()){
print "<script>location='index.php?table=$table';</script>";
}else{
print "Error on update register!<br><br>";
}
}
require_once('./footer.php');
?>