How can PHP loops like foreach be utilized to process multiple form inputs for database insertion?
When processing multiple form inputs for database insertion, PHP loops like foreach can be utilized to iterate over the form data and insert each input into the database. By looping through the form inputs, you can dynamically handle any number of fields without hardcoding each one individually.
// Assume $formData is an array containing the form inputs
foreach ($formData as $key => $value) {
// Sanitize input data before inserting into the database
$sanitizedValue = mysqli_real_escape_string($connection, $value);
// Build and execute the SQL query to insert each form input into the database
$query = "INSERT INTO table_name (column_name) VALUES ('$sanitizedValue')";
mysqli_query($connection, $query);
}