How can multiple data be read from and written to a table in PHP?
To read and write multiple data to a table in PHP, you can use SQL queries to insert, update, or retrieve data from the database table. You can use loops to iterate over multiple data entries and perform the necessary database operations for each entry.
// Connect to database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Example of inserting multiple data entries into a table
$data = array(
array('John', 'Doe', 'john.doe@example.com'),
array('Jane', 'Smith', 'jane.smith@example.com')
);
foreach ($data as $entry) {
$sql = "INSERT INTO users (first_name, last_name, email) VALUES ('" . $entry[0] . "', '" . $entry[1] . "', '" . $entry[2] . "')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
// Close connection
$conn->close();