user3305327
user3305327

Reputation: 907

Excel to MySQL using PHP

I have to upload excel files record into mysql and i have written the following code which is working fine...

<?php

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

$data = new Spreadsheet_Excel_Reader("file.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>";
            }
        $Productcode = mysqli_real_escape_string($connection,$data->sheets[$i][cells][$j][1]);
            $Artikel = mysqli_real_escape_string($connection,$data->sheets[$i][cells][$j][2]);
            $EANcode = mysqli_real_escape_string($connection,$data->sheets[$i][cells][$j][3]);
            $url_kiesk_nl = mysqli_real_escape_string($connection,$data->sheets[$i][cells][$j][4]);

            $query = "insert into test(Productcode,Artikel,EANcode,url_kiesk_nl) values('".$Productcode."','".$Artikel."','".$EANcode."','".$url_kiesk_nl."')";

            mysqli_query($connection,$query);
            $html.="</tr>";
        }
    }

}

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

Now I have to add a word into the 4th column for each record for that I have written this line. Actually my requirement is to add "1" to each cells of 4th column.

$url_kiesk_nl = mysqli_real_escape_string($connection,$data->"1".sheets[$i][cells][$j][4]);

But this is not working... can anyone please help me?

Upvotes: 1

Views: 386

Answers (1)

lpg
lpg

Reputation: 4937

Use this:

$url_kiesk_nl = "1".mysqli_real_escape_string($connection,$data->sheets[$i][cells][$j][4]);

Or this:

$url_kiesk_nl = mysqli_real_escape_string($connection,"1".$data->sheets[$i][cells][$j][4]);

Upvotes: 2

Related Questions