-
Notifications
You must be signed in to change notification settings - Fork 4
/
update.php
108 lines (96 loc) · 3.79 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<!DOCTYPE html>
<?php
require 'Database.php';
require 'Auth.php';
$pdo = Database::connect();
$auth = new Auth($pdo);
if(!$auth->isLoggedIn()) {
header("Location: index.php");
die();
}
$id = null;
if ( !empty($_GET['id'])) {
$id = $_REQUEST['id'];
}
if ( null==$id ) {
header("Location: index.php");
}
if ( !empty($_POST)) {
// keep track validation errors
$usernameError = null;
$passwordError = null;
// keep track post values
$username = $_POST['name'];
$password = $_POST['email'];
// validate input
$valid = true;
if (empty($username)) {
$usernameError = 'Please enter username';
$valid = false;
}
if (empty($password)) {
$passwordError = 'Please enter password';
$valid = false;
}
// update data
if ($valid) {
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "UPDATE user set username = ?, password = ? WHERE id = ?";
$query = $pdo->prepare($sql);
$query->execute(array($username,$password,$id));
Database::disconnect();
header("Location: index.php");
}
} else {
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM user where id = ?";
$query = $pdo->prepare($sql);
$query->execute(array($id));
$data = $query->fetch(PDO::FETCH_ASSOC);
$username = $data['username'];
$password = $data['password'];
Database::disconnect();
}
?>
<html lang="en">
<head>
<meta charset="utf-8">
<link href="css/bootstrap.min.css" rel="stylesheet">
<script src="js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="span10 offset1">
<div class="row">
<h3>Update a Customer</h3>
</div>
<form class="form-horizontal" action="update.php?id=<?php echo $id?>" method="post">
<div class="control-group <?php echo !empty($usernameError)?'error':'';?>">
<label class="control-label">UserName</label>
<div class="controls">
<input name="name" type="text" placeholder="Username" value="<?php echo !empty($username)?$username:'';?>">
<?php if (!empty($usernameError)): ?>
<span class="help-inline"><?php echo $usernameError;?></span>
<?php endif; ?>
</div>
</div>
<div class="control-group <?php echo !empty($passwordError)?'error':'';?>">
<label class="control-label">Password</label>
<div class="controls">
<input name="email" type="text" placeholder="Password" value="<?php echo !empty($password)?$password:'';?>">
<?php if (!empty($passwordError)): ?>
<span class="help-inline"><?php echo $passwordError;?></span>
<?php endif;?>
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-success">Update</button>
<a class="btn" href="index.php">Back</a>
</div>
</form>
</div>
</div> <!-- /container -->
</body>
</html>