What are the advantages of using bind_param in PHP when inserting multiple records compared to directly including variables in the SQL query?

When inserting multiple records into a database using PHP, it is more secure and efficient to use bind_param to bind variables to placeholders in the SQL query. This helps prevent SQL injection attacks and improves performance by allowing the database to prepare the query once and execute it multiple times with different parameter values.

// Connect to database
$mysqli = new mysqli("localhost", "username", "password", "database");

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

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

// Set parameter values and execute the statement in a loop for multiple records
foreach ($records as $record) {
    $value1 = $record['value1'];
    $value2 = $record['value2'];
    $stmt->execute();
}

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