How can a data record be modified in a table using PHP?
To modify a data record in a table using PHP, you can use an SQL UPDATE query. This query allows you to specify the table, columns to update, and the new values for those columns based on a condition. You can use PHP's MySQLi or PDO extension to execute the query and update the data record in the table.
<?php
// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// SQL query to update a data record in a table
$sql = "UPDATE table_name SET column1 = 'new_value1', column2 = 'new_value2' WHERE condition";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
// Close the connection
$conn->close();
?>
Related Questions
- Why is it recommended to use <?php ?> instead of <script language="PHP"> for executing PHP code?
- In the context of PHP programming, what are best practices for handling user input from forms to prevent injection attacks?
- What are the advantages and disadvantages of using exec vs include to run PHP files in a Linux environment?