Parsing Excel data from specific row and columns to MySQL using PHP - php

currently im doing my homework project to parse specific data from excel to MySQL using PHP. The project is about uploading an excel file and then the excel data supposed to be in MySQL after excel file uploaded.
The problem that im facing is, my homework project requirement is only parse specific row and column amongst the data exists in the excel file. I have already try using codes that i found, but it doesn't work. It parse everything in the excel files to MySQL table. Not the specific Row and Columns data that i expected.
I'm still looking for a solution for this. Spent almost 2 weeks for this but still i'm facing a dead end. My deadline project in 1 week. So, i would be really thankful if someone could help me and get me a solution of this.
I have tried on this PHP script. But, all the data in excel were parsed to MySQL table and the data messed the row and column in MySQL table. I want only the specific part (row and column) of excel files to be inserted to MySQL table that i've crated. i provided screenshot and the part that i want to be parsed to MySQL table highlighted with red color.
if(!empty($_FILES['excelfile']['name'])){
// Get File extension eg. 'xlsx' to check file is excel sheet
$pathinfo = pathinfo($_FILES['excelfile']['name']);
// check file has extension xlsx, xls and also check
// file is not empty
if (($pathinfo['extension'] == 'xlsx' || $pathinfo['extension'] == 'xls')
&& $_FILES['excelfile']['size'] > 0 ){
$file = $_FILES['excelfile']['tmp_name'];
$reader = ReaderFactory::create(Type::XLSX);
$reader->open($file);
$count = 0;
foreach ($reader->getSheetIterator() as $sheet){
foreach($sheet->getRowIterator () as $row)
{
if ($count > 0){
$name = $row[2];
$job_schedule = $row[3];
$overtime = $row[4];
$notes = $row[5];
$start_working = $row[6];
$finished_working = $row[7];
$qry = "INSERT INTO `timesheet` (`name`,`job_schedule`, `overtime`,`notes`, `start_working`,`finished_working`) VALUES ('$name','$job_schedule','$overtime','$notes','$start_working','$finished_working')";
$res = mysqli_query($con,$qry);
}
$count++;
}
}
if($res){
echo "Success";
}
else{
echo "failed";
}
$reader->close();
}
else{
echo "Excel format is not supported";
}
```[Here is the SCREENSHOT LINK]
[1]: https://i.stack.imgur.com/0kIxo.png

You can follow this link:
Exporting data from php to excel
I recommend use PHPExcel php class, just plug and play, I've done it myself. It's works better than trying to figure everthing by yourself.

Related

Populating mysql database with csv file using php

I'm trying to populate a mysql database with the contents of a .csv file using php. I'd like to do it using php code (no phpadmin). I consulted another thread on stackoverflow (populating database from csv file using php) but am still getting stuck. Here is my code:
$file = fopen("input.csv", "r");
while ($data = fgetcsv($file, $lineLength = 0, $delimiter = ",")) {
$added = "INSERT INTO Items VALUES(".$data.")";
if($connection->query($added) === TRUE) {
echo "Values successfully added!";
} else {
echo "Error inserting values: " . $connection->error;
}
Some context: earlier in the code, I create a database, connect to it, and create a table in the database. That part works fine. I just get an error when I try to populate the database from a .csv file. Here is the error message I get:
Notice: Array to string conversion in C:\xampp\htdocs\assignment8\init.php on line 64
Error inserting values: Unknown column 'Array' in 'field list'
I get this message 12 times, which corresponds with the number of rows in the .csv file I'm trying to import. "Line 64" corresponds with the line in my code that starts with "$added = INSERT INTO..."
Any help or suggestions are greatly appreciated.
You have to access to your column in the array
If your csv is something like this
Inside your while, you can access the values like:
echo $data[0] // prints 'Value 1'
So you might want to do something like...
$added = "INSERT INTO Items VALUES(".$data[0].")";

How to know why an .xls file generated by phpSpreadsheet is corrupted?

I wrote a PHP script that generates XLs files from SQL queries on MariaDB using PhpSpreadSheet.
It works really fine most of the time, but I've got issues with my biggest extract: Excel tells me (when I try to open the files) that it is "corrupted". If I skip the alert and open it, all the expected rows are in the file (my Mac users can't open it at all).
Here are the results of my investigations and observations:
- for one given query, I can set a SQL "LIMIT" (max number of rows) to have a non-corrupted file again. For one given query, this LIMIT between ok and not-corrupted and corrupted files will always be the same number.
- for one given query, this "LIMIT" between corrupted and not corrupted files will be pretty much the same whether the IDs are sort ASCendig or DESCending (in the SQL query. This way, suppose it's not a specific character in one row that breaks the file. This conclusion is validated by the fact that if I exclude the rows around this limit, the problem remains. However, if I replace each value to be written in the cells of the XLs file by a big random string ("abcdefghijklm", slightly bigger than the average length of each cell from my request), the problem disappears.
I'm using PHP V7.0.33 (memory_limit 1024M) / Ubuntu16.04.1 / MariaDB.
There is no memory limit warning in the Apache2/log/error.log (no error at all)
<?php
//Initialization
require '/var/www/html/vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xls;
ob_clean();
//Getting data from DB
$connect = mysqli_connect("localhost", "user", "pass", "base","port");
$query = "SELECT * FROM ... WHERE ...";
$result = mysqli_query($connect, $query);
$filename="...";
//If data exist
if(mysqli_num_rows($result) > 0){
$spreadsheet = new Spreadsheet(); /*----Spreadsheet object-----*/
$Excel_writer = new Xls($spreadsheet); /*----- Excel (Xls) Object*/
$spreadsheet->setActiveSheetIndex(0);
$activeSheet = $spreadsheet->getActiveSheet();
$first = true;
$irow=0;
//Loop on each row
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){
//Headers
if ($first) {
$irow++;
$icol=0;
foreach (array_keys($row) as &$value) {
$icol++;
$activeSheet->setCellValueByColumnAndRow( $icol,$irow, $value );
}
$first = false;
}
//DataBodyRange
$irow++;
$icol=0;
foreach (($row) as &$value) {
$icol++;
$activeSheet->setCellValueByColumnAndRow( $icol,$irow, $value );
}
}
//Finalizartion
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0');
$Excel_writer->save('php://output');
}
?>
Romain Dub,
I had pretty much the same problem.
Mine was slightly reversed. I wanted an xlsx but kept getting the error as you were. After my spreadsheet was created, I changed the extension to xls and I got the spreadsheet to open and when it opened, I found lines in the spreadsheet about a couple of undefined variables. If you need to resolve your error, possibly check your code for undefined variables being inserted into your spreadsheet.
Just an idea that may or may not be the solution to your problem.

Error in reading Excel file & Insert data into MySQL Database using PHP

My purpose is to read an excel (.xls) file and store it in the database as well as show the inputs of excel file in the browser.
I am facing problem while reading the file. I have integrated Excelfilereader.php
The program reads the excel file but while printing its output in (excel file content) in the browser, data print in zig-zag way.
Example:
studentid name class school course address [name of fields]
In Browser, the same fields are printed, but the data of studentID is printed under the name, data of name prints under class, but rest all are printed as it is. In first two columns are printed unaligned.
Code used to call Excelreader.php:
[mysql_query("insert into submit_form(parent_id,name,lab,submission,title,sampletype,file) values('','".$_POST['name']."','".$_POST['lab']."','".$_POST['submission']."','".$_POST['title']."','".$_POST['sampletype']."','".$uploadfile."')");
$insertid=mysql_insert_id();
$data = new Spreadsheet_Excel_Reader($_FILES['uploadfile']['name']);
//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=2;$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>";
}
$data->sheets[$i][cells][$j][1];
$eid = $eid = mysqli_real_escape_string($connection,$data->sheets[$i][cells][$j][1]);
$age = mysqli_real_escape_string($connection,$data->sheets[$i][cells][$j][2]);
$gender = mysqli_real_escape_string($connection,$data->sheets[$i][cells][$j][3]);
$ethnic = mysqli_real_escape_string($connection,$data->sheets[$i][cells][$j][4]);
$cancer = mysqli_real_escape_string($connection,$data->sheets[$i][cells][$j][5]);
$sample = mysqli_real_escape_string($connection,$data->sheets[$i][cells][$j][6]);
$instrument = mysqli_real_escape_string($connection,$data->sheets[$i][cells][$j][7]);
$instrumenttype = mysqli_real_escape_string($connection,$data->sheets[$i][cells][$j][8]);
$query = "insert into submit_form(parent_id,name,lab,submission,title,patient,age,gender,ethnic,cancer,sample,instrument,instrumenttype,attach,sampletype,file) values('".$insertid."','".$_POST['name']."','".$_POST['lab']."','".$_POST['submission']."','".$_POST['title']."','".$eid."','".$age."','".$gender."','".$ethnic."','".$cancer."','".$sample."','".$instrument."','".$instrumenttype."','1','".$_POST['sampletype']."','".$uploadfile."')";
mysqli_query($connection,$query);
$html.="</tr>";
}
}][2]
You have mixed mysql_* calls with mysqli_* calls. Use only mysqli_* calls.
(And you should escape the $_POST[] columns, too. They are vulnerable to hackers.)

Run 2 sql query with 1 query for save query result as .csv format

In my web I've 2 page. 1) admin.php 2) csv.php. In admin.php page following query is showing data from db. In csv.php page I used same query to save data to .csv format but Can't save it.
I decided to run this same query in ONE QUERY. So that I can get the query result and can save it to csv format.
Questions:
1) How do i run this query to ONE query ?
2) Following query is showing data successfully. So how do i save it as .csv file ?
I search google for that and found many result which is showing how do I save data as .csv format with only one query. But you see that I've 2 while statement in my query then how do i save it as .csv format ? NO idea :(
Thanks and Looking for your help. :)
Note: I'm new learner about php and mysql.
$sqlagentdetails = "select * from users WHERE company_name != ''";
$rowresult = mysql_query($sqlagentdetails);
while($row = mysql_fetch_array($rowresult, MYSQL_ASSOC))
{
$pc1 = $row['pc1'];
$pc2 = $row['pc2'];
$pc3 = $row['pc3'];
$pc4 = $row['pc4'];
$emailAgent = $row['user_email'];
$user_id = $row['id'];
$myQuery = mysql_query("
SELECT *
FROM user_property upr
WHERE (postcode = '$pc1' OR
postcode = '$pc2' OR
postcode = '$pc3' OR
postcode = '$pc4') AND
datediff(CURDATE(), upr.creation_date) <= 7 AND
NOT EXISTS(SELECT ofr.property_id
FROM offers ofr
WHERE ofr.property_id = upr.property_id AND
ofr.agent_id IN(SELECT id
FROM users
WHERE company_name !=''
)
)
ORDER BY property_id DESC");
while($row = mysql_fetch_array($myQuery)){
// more data are goes to here...
}
}
1) How do i run this query to ONE query ?
You don't want it to run as one query. It's usually better to have lots of small simple queries instead of one complicated query. In fact I would suggest you update your code to have even more queries, for example the contents of the "not exists()" should not be done as a subquery, it should be a completely separate query to improve performance.
2) Following query is showing data successfully. So how do i save it as .csv file ?
There are two parts, first you need to send the correct HTTP headers to trigger a CSV download:
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="export.csv";' );
Then just print out the data in CSV format:
while ($row = mysql_fetch_array($myQuery)) {
$first = true;
foreach ($row as $cell) {
if ($first)
$first = false;
else
print ',';
print '"' . addslashes($cell) . '"';
}
print "\n";
}
Note: CSV is a bad format, and this will only work in some editions of Microsoft Excel. Depending where the user lives (eg: Europe) it might not work properly. For most editions of Excel the above will work however. There is no good solution except to avoid using CSV.

How to get FGETCSV to read entire Japanese cell

I have a large study conducted with about 50 questions and 70,000 entries, so manually editing or using pivot tables just won't really work, I need to upload the data into a database. I can't get the Japanese characters to be read with any accuracy while using fgcsv(). I've tried setting the locale to UTF-8 and SJIS, but neither one seem to want to read all of the Japanese characters. I read somewhere this might be a bug, but I don't know..
The data looks like this:
Q-004 必須回答 あなたは、以下のどちらにお住まいですか? S/A
1 北海道 Hokkaido
2 青森県 Aomori
3 岩手県 Iwate
4 宮城県 Miyagi
5 秋田県 Akita
Here is my code:
setlocale(LC_ALL, 'ja_JP.SJIS');
$fp = fopen($_POST["filename"],'r') or die("can't open file");
$csv_line = fgetcsv($fp,1024);
$query = "";
$count = 0;
$question = false;
while($csv_line = fgetcsv($fp,1024)) {
if (!$question && strpos($csv_line[0],"Q-")!== false)
{
echo "Found a question: ".$csv_line[2] . "<br>";
$question = true;
}
else if($question && strlen($csv_line[0])==0)
{
echo "<hr>";
$question = false;
}
else if($question && intval($csv_line[0])>0)
{
echo $csv_line[0]. " has value ". $csv_line[2]." - ".$csv_line[3]. "<br>";
}
$count++;
}
echo "$count records read successfully";
fclose($fp) or die("can't close file");
Here is the result:
Found a question: A以下のどちらにお住まいですか?
1 has value k海道 - Hokkaido
2 has value X県 - Aomori
3 has value - Iwate
4 has value {城県 - Miyagi
5 has value H田県 - Akita
When it comes to reading a CSV in PHP, I would say... don't do it, and use an SQL database instead, wherein you can set a collation such as ujis_japanese_ci in MySQL.
You should be able to easily import your CSV into a MySQL database using phpMyAdmin, if that is what you have, and then render the data from the MySQL database instead of reading a CSV file.
It is a work-around, granted, but my general experience is that CSV + foreign/special characters == problems.
I believe it is at least worth the try. Good luck

Categories