What is the correct SQL command to update existing data in a database table in PHP?
When updating existing data in a database table in PHP, you can use the SQL command UPDATE followed by the table name, SET to specify the columns to be updated, and WHERE to specify the condition for which rows to update. It is important to properly sanitize user input to prevent SQL injection attacks.
<?php
// Assuming $conn is the database connection object
// Sanitize user input
$id = mysqli_real_escape_string($conn, $_POST['id']);
$newValue = mysqli_real_escape_string($conn, $_POST['new_value']);
// SQL query to update data in the table
$sql = "UPDATE table_name SET column_name = '$newValue' WHERE id = $id";
// Execute the query
if(mysqli_query($conn, $sql)){
echo "Data updated successfully";
} else{
echo "Error updating data: " . mysqli_error($conn);
}
// Close the connection
mysqli_close($conn);
?>
Keywords
Related Questions
- How can PHP developers ensure that data displayed side by side in a row is responsive and mobile-friendly?
- Can using a Query Builder in conjunction with PDO provide database independence in PHP development?
- What is the significance of saving a PHP file as "UTF-8 without BOM" to prevent header modification errors?