How can multiple data records be updated at once in a PHP form?
To update multiple data records at once in a PHP form, you can use a loop to iterate through the records and update them individually. This can be achieved by submitting the form with an array of values for each record, then processing these arrays in the PHP script to update the corresponding records in the database.
<?php
// Assuming form data is submitted as arrays of values
$ids = $_POST['id'];
$names = $_POST['name'];
$ages = $_POST['age'];
// Connect to database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);
// Update records
for ($i = 0; $i < count($ids); $i++) {
$id = $ids[$i];
$name = $names[$i];
$age = $ages[$i];
$sql = "UPDATE table SET name='$name', age='$age' WHERE id='$id'";
$conn->query($sql);
}
// Close database connection
$conn->close();
?>