As I am trying to import MS Excel data to MySQL data base using the following code:
<?php
$db_username="root"; //database user name
$db_password="";//database password
$db_database="hr_mysql"; //database name
$db_host="localhost";
mysql_connect($db_host,$db_username,$db_password);
@mysql_select_db($db_database) or die( "Unable to connect to database.");
$handle = fopen("UploadIt.xls", "r"); //test.xls excel file name
if ($handle)
{
$array = explode("\n", fread($handle, filesize("UploadIt.xls")));
}
$total_array = count($array);
$i = 0;
$Leave_Type_Id1="LTY001";
$Leave_Type_Id2="LTY002";
while($i < $total_array)
{
$data = explode(",", $array[$i]);
//$sql = "insert into test values ('$data[0]','$data[1]')";
$sql = "update `hs_hr_employee_leave_quota` set `no_of_days_allotted`= {$data[0]} WHERE `employee_id`= {$data[0]} and `leave_type_id`= '{$Leave_Type_Id1}'";
$result = mysql_query($sql);
$sql = "update `hs_hr_employee_leave_quota` set `no_of_days_allotted`= {$data[2]} WHERE `employee_id`= {$data[0]} and `leave_type_id`= '{$Leave_Type_Id2}'";
$result = mysql_query($sql);
$i++;
}
if($result==false)
echo "Not succed";
else
{
echo "completed";
}
?>
And my .xls sheet is:
But I am getting error saying
htdocs\\Verify\\XL_To_DBTable.php line 28 - Undefined offset: 2
(Assume above one as xls sheet only)
You need to properly read the file as an MS Excel file. You are treating it as an ascii text file. You could manually dump the file to CSV format and parse it using similar logic to what you have already. Or you could use:
and use that to parse the file, get the contents and then create the appropriate queries to be executed in mysql.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.