How can Excel data be inserted into a MySQL database for use in a web application?
To insert Excel data into a MySQL database for use in a web application, you can convert the Excel file into a CSV format and then use PHP to read the CSV file and insert the data into the MySQL database using SQL queries.
<?php
// Connect to MySQL database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Path to the CSV file
$csvFile = 'data.csv';
// Read the CSV file
if (($handle = fopen($csvFile, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$sql = "INSERT INTO table_name (column1, column2, column3) VALUES ('" . $data[0] . "', '" . $data[1] . "', '" . $data[2] . "')";
if ($conn->query($sql) === TRUE) {
echo "Record inserted successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
fclose($handle);
} else {
echo "Error: Unable to open file";
}
// Close MySQL connection
$conn->close();
?>