PHP- Export Excel file content to MySql database
<?php
// Connect to the MySQL server and select the corporate database
mysql_connect("localhost","root","")or die ("could not connect to database");
mysql_select_db("corporate");
$query="CREATE TABLE sales
id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
client_id SMALLINT UNSIGNED NOT NULL,
order_time TIMESTAMP NOT NULL,
sub_total DECIMAL(8,2) NOT NULL,
shipping_cost DECIMAL(8,2) NOT NULL,
total_cost DECIMAL(8,2) NOT NULL
)";
//$result=mysql_query($query)or die("table not created".mysql_error());
// Open and parse the sales.csv file
$fh = fopen("book1.csv", "r");
while ($line = fgetcsv($fh, '\n', ","))
{
$id = $line[0];
$client_id = $line[1];
$order_time = $line[2];
$sub_total = $line[3];
$shipping_cost = $line[4];
$total_cost = $line[5];
// Insert the data into the sales table
$query = "INSERT INTO sales SET id='$id',
client_id='$client_id', order_time='$order_time',
sub_total='$sub_total', shipping_cost='$shipping_cost',
total_cost='$total_cost'";
$res=mysql_query($query)or die(mysql_error());
}
if($res){
echo"info updated";
}
flose($fh);
mysql_close();
?>