-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedit.php
More file actions
119 lines (91 loc) · 2.93 KB
/
edit.php
File metadata and controls
119 lines (91 loc) · 2.93 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
<?php
include "./auth.php";
// Check if ID is provided in GET request
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
header("Location: ./index.php");
exit;
}
$id = intval($_GET['id']); // Ensure it's an integer
// Fetch product data from database
$sql = "SELECT * FROM products WHERE id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
$product = $result->fetch_assoc();
if (!$product) {
die("❌ Product not found.");
}
// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Validate & sanitize inputs
$name = trim($_POST['name']);
$price = trim($_POST['price']);
if (empty($name) || empty($price)) {
echo "❌ Name and price cannot be empty.";
} elseif (!is_numeric($price)) {
echo "❌ Price must be a valid number.";
} else {
// Update the product using prepared statements
$sql = "UPDATE products SET name = ?, price = ? WHERE id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ssi", $name, $price, $id);
if ($stmt->execute()) {
echo "✅ Record updated successfully";
} else {
echo "❌ Error updating record: " . $conn->error;
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Update Product</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>CRUD | PHP | Update Product</h1>
<div class="container">
<a href="./index.php" class="btn btn-add">🏠 Back Home</a>
<form action="" method="post">
<label for="name">Name
<input type="text" name="name" id="name" value="<?= htmlspecialchars($product['name']); ?>">
</label>
<label for="price">Price
<input type="text" name="price" id="price" value="<?= htmlspecialchars($product['price']); ?>">
</label>
<input type="submit" class="btn btn-add" value="Update Product" />
</form>
</div>
</body>
</html>
<!--
include "./auth.php";
if(!$_GET['id']) {
header("Location: ./index.php");
}
$id = intval($_GET['id']);
// Fetch data from the database
if($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = $conn->real_escape_string($_POST['name']);
$price = $conn->real_escape_string($_POST['price']);
$sql = "UPDATE products SET name = '$name', price = '$price' WHERE id = $id";
if ($conn->query($sql) === TRUE) {
echo "✅ Record updated successfully";
// header("Location: ./index.php");
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
// check if the product exists
if(!$product) {
header("Location: ./index.php");
}
// Retrieve the current data
$sql = "SELECT * FROM products WHERE id = $id";
$result = $conn->query($sql);
$product = $result->fetch_assoc();
-->