Skip to content

Commit b28b190

Browse files
authored
Update update.php
1 parent d9e7e25 commit b28b190

File tree

1 file changed

+20
-36
lines changed

1 file changed

+20
-36
lines changed

php/update.php

+20-36
Original file line numberDiff line numberDiff line change
@@ -13,43 +13,27 @@
1313
}
1414

1515
// Handle update action
16-
if ($_SERVER["REQUEST_METHOD"] == "GET" && isset($_GET['action']) && $_GET['action'] == 'update' && isset($_GET['id'])) {
17-
$idToUpdate = $_GET['id'];
18-
19-
// Fetch the current data for the selected ID
20-
$selectSql = "SELECT * FROM names WHERE id = ?";
21-
$selectStmt = $conn->prepare($selectSql);
22-
$selectStmt->bind_param("i", $idToUpdate);
23-
$selectStmt->execute();
24-
$result = $selectStmt->get_result();
25-
$row = $result->fetch_assoc();
26-
27-
$nameToUpdate = $row['name']; // Assuming you want to update the 'name' field
28-
29-
// Display the form for updating
30-
echo "<h2>Update Name</h2>";
31-
echo "<form action='updateTable.php' method='post'>";
32-
echo "<input type='hidden' name='id' value='$idToUpdate'>";
33-
echo "Name: <input type='text' name='new_name' value='$nameToUpdate' required>";
34-
echo "<br><input type='submit' value='Update'>";
35-
echo "</form>";
36-
37-
// Handle the form submission for update
38-
if ($_SERVER["REQUEST_METHOD"] == "POST") {
39-
$newName = $_POST['new_name'];
40-
41-
$updateSql = "UPDATE names SET name = ? WHERE id = ?";
42-
$updateStmt = $conn->prepare($updateSql);
43-
$updateStmt->bind_param("si", $newName, $idToUpdate);
44-
45-
if ($updateStmt->execute()) {
46-
echo "Record with ID $idToUpdate updated successfully.";
47-
} else {
48-
echo "Error updating record: " . $updateStmt->error;
49-
}
50-
51-
$updateStmt->close();
16+
if ($_SERVER["REQUEST_METHOD"] == "POST") {
17+
$idToUpdate = $_POST['id'];
18+
$nameToUpdate = $_POST['name'];
19+
// You can add other fields as needed
20+
21+
// Update the record in the database
22+
$updateSql = "UPDATE names SET name = ? WHERE id = ?";
23+
$updateStmt = $conn->prepare($updateSql);
24+
$updateStmt->bind_param("si", $nameToUpdate, $idToUpdate);
25+
26+
if ($updateStmt->execute()) {
27+
echo "Record with ID $idToUpdate updated successfully.";
28+
} else {
29+
echo "Error updating record: " . $updateStmt->error;
5230
}
31+
32+
$updateStmt->close();
33+
34+
// Redirect back to database.php
35+
header("Location: database.php");
36+
exit();
5337
}
5438

5539
// Close the database connection

0 commit comments

Comments
 (0)