How can I use $_POST to update data in a database?
803 Aug 2024
In update.php
, you can update the database record:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test_db";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$id = $_POST["id"];
$name = $_POST["name"];
// Prepare and bind
$stmt = $conn->prepare("UPDATE users SET name = ? WHERE id = ?");
$stmt->bind_param("si", $name, $id);
// Execute the query
if ($stmt->execute()) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
$stmt->close();
}
$conn->close();
?>
This script uses a prepared statement to update a record securely. Prepared statements help prevent SQL injection by separating SQL code from data.
0 likes
Top related questions
Related queries
Latest questions
ईरान का दक्षिणी इज़राइल पर हमला: ऊंची इमारत पर मिसाइल गिरने से आसमान में छाया काला धुआं
20 Jun 2025 0
नितिन गडकरी ने की FASTag वार्षिक पास की घोषणा: ₹3,000 में करें हाईवे पर बिना रुकावट की यात्रा
18 Jun 2025 0
ईरान-इज़रायल तनाव के बीच भारत की बड़ी कार्रवाई,
17 Jun 2025 0
इजरायल का तेहरान पर और हमले का ऐलान,
14 Jun 2025 0
कोविड-19 भारत 2025: सक्रिय मामले 6,800 के पार
11 Jun 2025 1
पति की हत्या के बाद पत्नी ने किया आत्मसमर्पण:
09 Jun 2025 5