What is the best practice for storing multiple sets of data from a form in a MySQL database using PHP?

When storing multiple sets of data from a form in a MySQL database using PHP, it is best practice to use prepared statements to prevent SQL injection attacks and to ensure data integrity. You can achieve this by looping through the form data and inserting each set of data into the database within the loop.

// Assume $formData is an array containing multiple sets of form data

// Establish a database connection
$mysqli = new mysqli("localhost", "username", "password", "database");

// Check connection
if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
}

// Prepare a SQL statement
$stmt = $mysqli->prepare("INSERT INTO table_name (column1, column2) VALUES (?, ?)");

// Bind parameters
$stmt->bind_param("ss", $value1, $value2);

// Loop through the form data and insert each set of data into the database
foreach ($formData as $data) {
    $value1 = $data['value1'];
    $value2 = $data['value2'];
    
    // Execute the statement
    $stmt->execute();
}

// Close the statement and connection
$stmt->close();
$mysqli->close();