-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit.php
138 lines (124 loc) · 4.59 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
<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "myshop";
// Create connection
$connection = new mysqli($servername, $username, $password, $database);
$name = "";
$email = "";
$phone = "";
$address = "";
$errorMessage = "";
$successMessage = "";
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (!isset($_GET["id"])) {
header("location: index.php");
exit;
}
$id = $_GET["id"];
// Read selected row data
$sql = "SELECT * FROM clients WHERE id=$id";
$result = $connection->query($sql);
$row = $result->fetch_assoc();
if (!$row) {
header("location: index.php");
exit;
}
// Check if the keys exist in the $row array before accessing them
$name = isset($row["name"]) ? $row["name"] : "";
$email = isset($row["email"]) ? $row["email"] : "";
$phone = isset($row["phone"]) ? $row["phone"] : "";
$address = isset($row["address"]) ? $row["address"] : "";
}
if (empty($id) || empty($name) || empty($email) || empty($phone) || empty($address)) {
$errorMessage = "All the fields are required";
} else {
// Update data query
$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;
} else {
$successMessage = "Client updated successfully";
header("location: index.php");
exit;
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Shop</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container my-5">
<h2>Edit 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-3">
<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-3">
<input type="text" 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-3">
<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-3">
<input type="text" class="form-control" name="address" value="<?php echo $address; ?>">
</div>
</div>
<?php
if (!empty($successMessage)) {
echo "
<div class='row mb-3'>
<div class='offset-sm-3 col-sm-3'>
<div class='alert alert-success 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>
</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">
<a class="btn btn-outline-primary" href="index.php" type="button">Cancel</a>
</div>
</div>
</form>
</div>
</body>
</html>