How can PHP handle data from multiple form submissions and insert them into a single table efficiently?
To handle data from multiple form submissions and insert them into a single table efficiently, you can use PHP to loop through the submitted data and insert each set of values into the database within a single transaction. This way, all the data will be inserted at once, improving efficiency and reducing the number of database queries.
<?php
// Assuming $formData is an array containing multiple sets of form data
// Establish a database connection
$pdo = new PDO('mysql:host=localhost;dbname=your_database', 'username', 'password');
// Begin a transaction
$pdo->beginTransaction();
// Prepare the SQL statement for inserting data into the table
$stmt = $pdo->prepare("INSERT INTO your_table (column1, column2) VALUES (:value1, :value2)");
// Loop through the form data and insert each set of values into the table
foreach($formData as $data) {
$stmt->execute(array(':value1' => $data['value1'], ':value2' => $data['value2']));
}
// Commit the transaction
$pdo->commit();
// Close the database connection
$pdo = null;
?>