Blessan Kurien
Blessan Kurien

Reputation: 1665

Uploading excel sheet to mysql using php

I am developing a web application using php and bootstrap.Uploading an excel sheet is one of the requirements of my application.Now i am partially implemented it (extract the data from excel sheet),the next thing i want upload these data into database table but failed.Here is my code

$file = fopen($filename, "r");
$row=1;

while (($Data = fgetcsv($file, 10000, ",")) !== FALSE)
{
    echo $Data[0];
}
fclose($file);

Here is the output of $Data[0]

enter image description here

I want to store the data in database table with these fields .How can i achieve this?

Upvotes: 1

Views: 1979

Answers (1)

Abdullah Al Noman
Abdullah Al Noman

Reputation: 2878

Here is a sample code of how you should do it . details can be found here . Excel reader library will Handel most of the hustle for you

 ini_set("display_errors",1);
    require_once 'excel_reader2.php';



$data = new Spreadsheet_Excel_Reader("example.xls");

echo "Total Sheets in this xls file: ".count($data->sheets)."<br /><br />";

$html="<table border='1'>";
for($i=0;$i<count($data->sheets);$i++) // Loop to get all sheets in a file.
{
if(count($data->sheets[$i][cells])>0) // checking sheet not empty
{
echo "Sheet $i:<br /><br />Total rows in sheet $i  ".count($data->sheets[$i][cells])."<br />";
for($j=1;$j<=count($data->sheets[$i][cells]);$j++) // loop used to get each row of the sheet
{
$html.="<tr>";
for($k=1;$k<=count($data->sheets[$i][cells][$j]);$k++) // This loop is created to get data in a table format.
{
$html.="<td>";
$html.=$data->sheets[$i][cells][$j][$k];
$html.="</td>";
}

$html.="</tr>";
}
}

}

$html.="</table>";
echo $html;
echo "<br />Data Inserted in dababase";
?>

Upvotes: 2

Related Questions