How can PHP arrays be utilized to streamline the process of storing user input in a database?
When storing user input in a database, PHP arrays can be utilized to streamline the process by organizing the data into a structured format before inserting it into the database. By using PHP arrays, you can easily loop through the user input, sanitize the data, and prepare it for database insertion in a more efficient manner.
// Assuming $userData is an array containing user input data
// Connect to the database
$connection = new mysqli('localhost', 'username', 'password', 'database');
// Prepare the SQL query
$sql = "INSERT INTO users (name, email, age) VALUES (?, ?, ?)";
$stmt = $connection->prepare($sql);
// Bind parameters and execute the query for each user
foreach ($userData as $user) {
$name = $user['name'];
$email = $user['email'];
$age = $user['age'];
$stmt->bind_param('ssi', $name, $email, $age);
$stmt->execute();
}
// Close the connection
$connection->close();