-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit.php
157 lines (133 loc) · 4.64 KB
/
edit.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
$servername = "localhost:3308";
$username = "root";
$password = "";
$database = "myshop";
// create connection
$connection = new mysqli($servername, $username, $password, $database);
$id ="";
$name ="";
$email ="";
$phone ="";
$address="";
$errorMessage ="";
$successMessage ="";
if ( $_SERVER['REQUEST_METHOD'] == 'GET'){
//GET method:show the data of the client
if ( !isset($_GET["id"])){
header("location: /myshop/index.php");
exit;
}
$id = $_GET["id"];
//read the row of the selected client from database table
$sql="SELECT * FROM clients WHERE id=$id";
$result = $connection->query($sql);
$row = $result->fetch_assoc();
if (!$row) {
header("location: /myshop/index.php");
exit;
}
$name = $row["name"];
$email = $row["email"];
$phone = $row["phone"];
$address = $row["address"];
}
else {
//post method:update the data of the client
$id = $_POST["id"];
$name = $_POST["name"];
$email = $_POST["email"];
$phone = $_POST["phone"];
$address = $_POST["address"];
do {
if ( empty($id) || empty($name) || empty($email) || empty($phone)|| empty($address)){
$errorMessage = "All the fields are required";
break;
}
$sql = "UPDATE clients " .
"SET name ='$name', email= '$email', phone = '$phone', address= '$address'" .
"WHERE id = $id";
$result = $connection->query($sql);
if (!$result){
$errorMessage = "Invalid query: " . $connection->error;
break;
}
$successMessage = "client updated successfully";
header("location: /myshop/index.php");
exit;
}while (false);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Shop</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css
">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js
"></script>
</head>
<body>
<div class="container my-5">
<h2>New Client</h2>
<?php
if (!empty($errorMessage)){
echo "
<div class='alert alert-warning alert-dismissible fade show' role='alert'>
<strong>$errorMessage</strong>
<button type='button' class='btn-close' data-bs-dismiss='alert' aria-label='close'></button>
</div>
";
}
?>
<form method="post">
<input type="hidden" name="id" value="<?php echo $id; ?>">
<div class="row mb-3">
<label class="col-sm-3 col-form-label">Name</label>
<div class="col-sm-6">
<input type="text" class="form-control" name="name" value="<?php echo $name; ?>">
</div>
</div>
<div class="row mb-3">
<label class="col-sm-3 col-form-label">Email</label>
<div class="col-sm-6">
<input type="email" class="form-control" name="email" value="<?php echo $email; ?>">
</div>
</div>
<div class="row mb-3">
<label class="col-sm-3 col-form-label">Phone</label>
<div class="col-sm-6">
<input type="text" class="form-control" name="phone" value="<?php echo $phone; ?>">
</div>
</div>
<div class="row mb-3">
<label class="col-sm-3 col-form-label">Address</label>
<div class="col-sm-6">
<input type="text" class="form-control" name="address" value="<?php echo $address; ?>">
</div>
</div>
<?php
if (!empty($successMessage)) {
echo "
<div class='alert alert-warning alert-dismissible fade show' role='alert'>
<strong>$successMessage</strong>
<button type='button' class='btn-close' data-bs-dismiss='alert' aria-label='close'></button>
</div>
";
}
?>
<div class="row mb-3">
<div class="offset-sm-3 col-sm-3 d-grid">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
<div class="col-sm-3 d-grid">
<a class="btn btn-outline-primary" href="/myshop/index.php" role="button">Cancel</a>
</div>
</div>
</form>
</div>
</body>
</html>