|
13 | 13 | }
|
14 | 14 |
|
15 | 15 | // 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; |
52 | 30 | }
|
| 31 | + |
| 32 | + $updateStmt->close(); |
| 33 | + |
| 34 | + // Redirect back to database.php |
| 35 | + header("Location: database.php"); |
| 36 | + exit(); |
53 | 37 | }
|
54 | 38 |
|
55 | 39 | // Close the database connection
|
|
0 commit comments