Parse .txt to csv and insert into database in php - php

code:
<?php
if(isset($_POST['submit']))
{
$filename = $_FILES['field']['name'];
$path = "attandance_file/";
$move = move_uploaded_file($_FILES['field']['tmp_name'],$path.$_FILES['field']['name']);
$path_file = "attandance_file/".basename($_FILES['field']['name']);
$fileObj = fopen( $path_file, "rt" );
while (($line = fgets( $fileObj )))
{
$members = explode("\n", $line);
foreach($members as $row)
{
$rowss = explode(" ",$row);
foreach($rowss as $data)
{
$query = "insert into attandance(No,TMNo,EnNo,Name,INOUT,Mode,DateTime)values('".$data."')";
echo $query;
}
}
}
}
?>
In this code I have upload a .txt file and then click on submit then read .txt file which is readable after that I used explode function to show view on new line but when I insert data into table and then print query it look like:
insert into attandance(No,TMNo,EnNo,Name,INOUT,Mode,DateTime)values('No')
insert into attandance(No,TMNo,EnNo,Name,INOUT,Mode,DateTime)values('TMNo')
insert into attandance(No,TMNo,EnNo,Name,INOUT,Mode,DateTime)values('EnNo')
insert into attandance(No,TMNo,EnNo,Name,INOUT,Mode,DateTime)values('Name')
insert into attandance(No,TMNo,EnNo,Name,INOUT,Mode,DateTime)values('INOUT')
insert into attandance(No,TMNo,EnNo,Name,INOUT,Mode,DateTime)values('Mode')
insert into attandance(No,TMNo,EnNo,Name,INOUT,Mode,DateTime)values('DateTime')
But I want this
insert into attandance(No,TMNo,EnNo,Name,INOUT,Mode,DateTime)values('No','TMNo','EnNo','Name','INOUT','Mode','DateTime')
How can I do this ?Please help me.
Thank You

The way to do it is to use a database driver directly, and done so very easily.
In your code remove this:
foreach($rowss as $data){
$query = "insert into attandance(No,TMNo,EnNo,Name,INOUT,Mode,DateTime)values('".$data."')";
}
And change it to the following:
$sql = "insert into attandance(No,TMNo,EnNo,Name,INOUT,Mode,DateTime)values(".trim(str_repeat('?,', count($rowss)),',').")";
Now your $sql string is ready to be used as a prepared statement in PDO or MySQLi and parse $rowss to the execute parameter like so:
$stmt = $pdo->prepare($sql);
if(!$stmt->execute($rowss)){
echo "Failed to execute query.";
}

Are you sure you have all needed value of records?
$rowss = explode(" ",$row);
foreach($rowss as $data)
{
$query = "insert into attandance(No,TMNo,EnNo,Name,INOUT,Mode,DateTime)values('".$data."')";
echo $query;
}
change to:
$query = "insert into attandance(No,TMNo,EnNo,Name,INOUT,Mode,DateTime)values('".str_replace(" ", "','", $row)."')";
echo $query;

Related

CVS result shows in one column

I have this csv export code.
the export I working well but my only problem is that the result is showing only in one column
the problem happens only when I try to convert the file to UTF-8,
is there another way around this?
$link = mysqli_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PWD, MYSQL_DB);
$link->set_charset("utf8");
$sql = "SELECT * FROM weekevents";
$query = mysqli_query($link,$sql);
$col = "col1,col2,col3,col4 \n";//Column headers
foreach ($query as $record){
$cam = explode(';', $record['cam']);
$col .= $record['weekday'].','.$record['week']
;
}
$filename = date("m-Y");
$csv_handler = fopen ('C:/xampp/htdocs/rge/main-rtl/uploads/שיבוץ חודש -'.$filename.'.csv','w');
fwrite ($csv_handler, $col);
fclose ($csv_handler);
echo 'Data saved to csvfile.csv';

inserting 4 different files into 1 SQL table row in php

As the title says, i want to parse 4 different files and insert them into 1 sql row. I have parsed the files so i have the information, however its not inserting into my table.
This is the code I have. I eliminated the actual sql names and values to save time. My two main questions are 1) is this the right way to do this and 2) is there a better way.
<?php
$connect = mysqli_connect("reserve1", "root", "","server_31");
$dir = "/Users/Administrator/Desktop/Reserve1";
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
foreach(glob("*.json") as $filename) {
$data = file_get_contents($filename);
$array = json_decode($data, true);
foreach($array[0]as $row) {
$sql = "INSERT INTO servers_updated (---)
VALUES ('---)";
$connect->query($sql);
}
foreach(glob("*_processor.json") as $filename) {
$data = file_get_contents($filename);
$info = json_decode($data, true);
foreach($info[1] as $row) {
$sql = "INSERT INTO servers_updated
(--- ) VALUES (---)";
$connect->query($sql);
}
foreach(glob("*_drives.json") as $filename) {
$data = file_get_contents($filename);
$info = json_decode($data, true);
foreach($info[1] as $row) {
$sql=" INSERT INTO servers_updated (---) VALUES (---)";
$connect->query($sql);
}
foreach(glob("*_memory.json") as $filename) {
$data = file_get_contents($filename);
$stuff = json_decode($data, true);
foreach($stuff[1] as $row) {
$sql =" INSERT INTO servers_updated
(--- ) VALUES (----)";
$connect->query($sql);
}
}
}
}
}
}
}
?>

PHP - How to skip first row when importing csv file?

I'm want to skip the first row of my csv file for the use of header purposes.
Using a while loop, how can I make it skip the first row? Thank you in advanced guys.
<?php
$con = mysqli_connect('localhost','root','') or die (mysql_error());
mysqli_select_db($con, 'test');
if(isset($_POST['submit']))
{
$file = $_FILES['file']['tmp_name'];
$handle = fopen($file,"r");
while(($fileop = fgetcsv($handle,2000,",")) !==false)
{
$KeyAccount = $fileop[0];
$BatchNumber= $fileop[1];
$Product = $fileop[2];
$Quantity = $fileop[3];
$PO = $fileop[4];
$DateRequested = $fileop[5];
$DateDelivered = $fileop[6];
$Status = $fileop[7];
$Serial = $fileop[8];
$Voucher = $fileop[9];
$DateExpiry = $fileop[10];
$sql = mysqli_query($con, "INSERT INTO orders (KeyAccount,BatchNumber,Product,Quantity,PO,DateRequested,DateDelivered,Status,Serial,Voucher,DateExpiry) VALUES ('$KeyAccount','$BatchNumber','$Product','$Quantity','$PO','$DateRequested','$DateDelivered','$Status','$Serial','$Voucher','$DateExpiry')");
}
if($sql)
{
echo '<script language="javascript">';
echo 'alert("Successfully Inserted.")';
echo '</script>';
}
else{
echo "error";
}
}
?>
Just add one "junk" call:
$junk = fgetcsv($handle,2000,",");
before the while loop.
I took your code and wrapped the contents of the while () statement inside of an if () statement.
I added a flag (i.e., $num) to track if the first iteration was being processed.
The if () checks to see if $num equals 0 and if it does, it increments it.
Your while () code will not get processed for that iteration.
The next iteration, the $num flag will not equal 0 and process the contents of the while () statement.
<?php
$con = mysqli_connect('localhost','root','') or die (mysql_error());
mysqli_select_db($con, 'test');
if(isset($_POST['submit'])){
$file = $_FILES['file']['tmp_name'];
$handle = fopen($file,"r");
$num = 0;
while(($fileop = fgetcsv($handle,2000,",")) !==false){
if($num == 0){
$num++;
}
else{
$KeyAccount = $fileop[0];
$BatchNumber= $fileop[1];
$Product = $fileop[2];
$Quantity = $fileop[3];
$PO = $fileop[4];
$DateRequested = $fileop[5];
$DateDelivered = $fileop[6];
$Status = $fileop[7];
$Serial = $fileop[8];
$Voucher = $fileop[9];
$DateExpiry = $fileop[10];
$sql = mysqli_query($con, "INSERT INTO orders (KeyAccount,BatchNumber,Product,Quantity,PO,DateRequested,DateDelivered,Status,Serial,Voucher,DateExpiry) VALUES ('$KeyAccount','$BatchNumber','$Product','$Quantity','$PO','$DateRequested','$DateDelivered','$Status','$Serial','$Voucher','$DateExpiry')");
}
}
if($sql){
echo '<script language="javascript">';
echo 'alert("Successfully Inserted.")';
echo '</script>';
}
else{
echo "error";
}
}
?>
Here is an alternative solution to parsing CSV file. The only catch is that the CSV data is simple comma separated values, ie: Comma is purely separator and not part of the column data (eg: cannot work with one,two,"three,four",five), and the data does not contain line breaks.
First, sample data:
header1,header2,header3,header4,header5,header6,header7,header8,header9,header10,header11
KeyAccount1,BatchNumber1,Product1,Quantity1,PO1,DateRequested1,DateDelivered1,Status1,Serial1,Voucher1,DateExpiry1
KeyAccount2,BatchNumber2,Product2,Quantity2,PO2,DateRequested2,DateDelivered2,Status2,Serial2,Voucher2,DateExpiry2
KeyAccount3,BatchNumber3,Product3,Quantity3,PO3,DateRequested3,DateDelivered3,Status3,Serial3,Voucher3,DateExpiry3
Next, the relevant PHP code for parsing CSV file.
<?php
// Start with anything else you need here
$inputfile = '/tmp/data.csv';
// Check to make sure the file exists and is readable
if (file_exists($inputfile) && is_readable($inputfile))
{
// Read the whole contents of the file into an array instead of using fgetcsv
$data = file($inputfile);
// Remove the first line of data, optionally save it into $header
$header = array_shift ($data);
// Parse the rest of the data
foreach ($data as $record)
{
// Optional, trim spaces on each sides
$record = trim($record);
// Skip empty lines
if ($record == '') { continue; }
// Separate the records by comma using explode
// WARNING: May not give proper result if the record column contains comma character
list ($KeyAccount, $BatchNumber, $Product, $Quantity,
$PO, $DateRequested, $DateDelivered, $Status,
$Serial, $Voucher, $DateExpiry) = explode(',', $record);
echo "Record = $KeyAccount, $BatchNumber, $Product, $Quantity, $PO, $DateRequested, $DateDelivered, $Status, $Serial, $Voucher, $DateExpiry\n";
// Do anything else you need here
// $sql = mysqli_query($con, "INSERT INTO orders (KeyAccount,BatchNumber,Product,Quantity,PO,DateRequested,DateDelivered,Status,Serial,Voucher,DateExpiry) VALUES ('$KeyAccount','$BatchNumber','$Product','$Quantity','$PO','$DateRequested','$DateDelivered','$Status','$Serial','$Voucher','$DateExpiry')");
}
}
Sample result:
$ php so.php
Record = KeyAccount1, BatchNumber1, Product1, Quantity1, PO1, DateRequested1, DateDelivered1, Status1, Serial1, Voucher1, DateExpiry1
Record = KeyAccount2, BatchNumber2, Product2, Quantity2, PO2, DateRequested2, DateDelivered2, Status2, Serial2, Voucher2, DateExpiry2
Record = KeyAccount3, BatchNumber3, Product3, Quantity3, PO3, DateRequested3, DateDelivered3, Status3, Serial3, Voucher3, DateExpiry3
Note that as mentioned in the code comment, we're not using fgetcsv() to read and parse the CSV file. Instead, we're using file() to read the whole file as array and then parse them one by one.
Not sure how the performance is between fgetcsv() vs file(). However, we're using similar code in one of our production server and it seems to work properly so far.
Use this snippet to get the CSV data and unset the first row to remove the headers.
$csv_data = array_map('str_getcsv', file($_FILES["file"]["tmp_name"]));
if (!empty($csv_data)) {
unset($csv_data[0]); // skip the first row i.e the header
foreach ($csv_data as $csv_row) {
// do MySQL updation
}
}

Insert sql query result to a new query

So the task is:
Do a query like Select * from table.
Take some cell value
Insert this value to a new query.
What do I have so far:
$Conn = odbc_connect("...");
$Result = odbc_exec("Select ...");
while($r = odbc_fetch_array($Result))
// showing result in a table
Here it looks like I should use the r array and insert data like
$var = r['some_field'];
$query = 'Select * from table where some_field = {$var}";
But how can I fill this array with values and how to make it available out of while loop?
Here I'm using odbc, but it doesn't matter, I need the algorithm. Thanks.
The whole code looks like:
<?php
$data = array();
$state = 'false';
if($_REQUEST['user_action']=='')
{
$Conn = odbc_connect("...");
$data = array();
if($_REQUEST['name']!='')
{
$Result = odbc_exec($Conn, "select ...");
//Showing result table
while($r = odbc_fetch_array(Result))
{
array_push($data, $r['cardgroup']);
$state = 'true';
}
// print_r($data); WORKS;
}
}
if ($_REQUEST['user_action'] == 'action1')
{
//I need to use $data HERE. Doesn't work
// $state = 'false' here...
}
?>
Define array outside while loop
$data = array();//defining
while($r = odbc_fetch_array($Result))
use array_push() inside while loop
array_push($data, $r['some_field']);
then try to print array of complete data outside loop
print_r($data);
Updates
Place $data = array(); at the top of first IF statement. Try this code:
$data = array();//at top
if($_REQUEST['user_action']=='')
{
$Conn = odbc_connect("...");
if($_REQUEST['name']!='')
{
$Result = odbc_exec($Conn, "select ...");
//Showing result table
while($r = odbc_fetch_array(Result))
{
array_push($data, $r['cardgroup']);
}
// print_r($data); WORKS;
}
}
if ($_REQUEST['user_action'] == 'action1')
{
//print_r($data) works here also
}
try something like this to store data in array
$allrows = array();
while($r = odbc_fetch_array( $result )){
$allrows[] = $r;
}
use foreach loop to print or use as per your choice
foreach($allrows as $singlerow) {
//use it as you want, for insert/update or print all key value like this
foreach($singlerow as $key => $value) {
//echo $key . '=='. $value;
}
}
You can try this as i understand your query hope this is your answer
$arr ='';
while($row = odbc_fetch_array($Result)) {
$arr .= '\''.$row['some_field'].'\',';
}
$arr = trim($arr, ",");
$query = "SELECT * from table where some_field IN ($arr)";
Your all task can be completed in single query
INSERT INTO table2 (col1, col2, ..., coln)
SELECT col1, col2, ..., coln
FROM table1

Upload CSV to MySQL table using PHP - Ignoring the header in the CSV

I am using the following to upload CSV to a table in MYSQL database.
Question: How do I bypass the 1st field? - as in ignore the header - which is currently being saved in my table?
Code:
<?php
/********************************/
/* Code at http://legend.ws/blog/tips-tricks/csv-php-mysql-import/
/* Edit the entries below to reflect the appropriate values
/********************************/
$databasehost = "localhost";
$databasename = "test";
$databasetable = "sample";
$databaseusername ="test";
$databasepassword = "";
$fieldseparator = ",";
$lineseparator = "\n";
$csvfile = "filename.csv";
/********************************/
/* Would you like to add an ampty field at the beginning of these records?
/* This is useful if you have a table with the first field being an auto_increment integer
/* and the csv file does not have such as empty field before the records.
/* Set 1 for yes and 0 for no. ATTENTION: don't set to 1 if you are not sure.
/* This can dump data in the wrong fields if this extra field does not exist in the table
/********************************/
$addauto = 0;
/********************************/
/* Would you like to save the mysql queries in a file? If yes set $save to 1.
/* Permission on the file should be set to 777. Either upload a sample file through ftp and
/* change the permissions, or execute at the prompt: touch output.sql && chmod 777 output.sql
/********************************/
$save = 1;
$outputfile = "output.sql";
/********************************/
if(!file_exists($csvfile)) {
echo "File not found. Make sure you specified the correct path.\n";
exit;
}
$file = fopen($csvfile,"r");
if(!$file) {
echo "Error opening data file.\n";
exit;
}
$size = filesize($csvfile);
if(!$size) {
echo "File is empty.\n";
exit;
}
$csvcontent = fread($file,$size);
fclose($file);
$con = #mysql_connect($databasehost,$databaseusername,$databasepassword) or die(mysql_error());
#mysql_select_db($databasename) or die(mysql_error());
$lines = 0;
$queries = "";
$linearray = array();
foreach(split($lineseparator,$csvcontent) as $line) {
$lines++;
$line = trim($line," \t");
$line = str_replace("\r","",$line);
/************************************
This line escapes the special character. remove it if entries are already escaped in the csv file
************************************/
$line = str_replace("'","\'",$line);
/*************************************/
$linearray = explode($fieldseparator,$line);
$linemysql = implode("','",$linearray);
if($addauto)
$query = "insert into $databasetable values('','$linemysql');";
else
$query = "insert into $databasetable values('$linemysql');";
$queries .= $query . "\n";
#mysql_query($query);
}
#mysql_close($con);
if($save) {
if(!is_writable($outputfile)) {
echo "File is not writable, check permissions.\n";
}
else {
$file2 = fopen($outputfile,"w");
if(!$file2) {
echo "Error writing to the output file.\n";
}
else {
fwrite($file2,$queries);
fclose($file2);
}
}
}
echo "Found a total of $lines records in this csv file.\n";
?>
I has been did the same purpose at last week. Please refer this code. It may be help you.
if (isset($_POST['submit'])) {
//Path of Uploading file
$target = "upload_files/results/";
$filename = $_FILES['upload_result']['name'];
if(file_exists($target.$filename)) {
unlink($target.$filename);
}
move_uploaded_file($_FILES['upload_result']['tmp_name'], $target.$filename);
//Store the Detail into the Master table...[class_master]
$new_master = mysql_query("INSERT INTO result_master(classname,sectionname,examname,filename) Values('$class','$section','$examname','$filename')");
$filename1 = explode(".",$filename);
$file = preg_replace("/[^a-zA-Z0-9]+/", "", $filename1[0]);
// get structure from csv and insert db
ini_set('auto_detect_line_endings',TRUE);
$handle = fopen($target.$filename,'r');
// first row, structure
if ( ($data = fgetcsv($handle) ) === FALSE ) {
echo "Cannot read from csv $file";die();
}
$fields = array();
$field_count = 0;
for($i=0;$i<count($data); $i++) {
$f = trim($data[$i]);
if ($f) {
// normalize the field name, strip to 20 chars if too long
$f = substr(preg_replace ('/[^0-9a-z]/', '_', $f), 0, 20);
$field_count++;
$fields[] = $f.' VARCHAR(50)';
}
}
$drop = mysql_query("Drop table IF EXISTS $file;");
$sql = "CREATE TABLE $file (" . implode(', ', $fields) . ')';
echo $sql . "<br /><br />";
$db = mysql_query($sql);
while ( ($data = fgetcsv($handle) ) !== FALSE ) {
$fields = array();
for($i=0;$i<$field_count; $i++) {
$fields[] = '\''.addslashes($data[$i]).'\'';
}
$sql = "Insert into $file values(" . implode(', ', $fields) . ')';
echo $sql;
$db = mysql_query($sql);
}
fclose($handle);
ini_set('auto_detect_line_endings',FALSE);
}
You either assume that there is ALWAYS a header, or that any header will be commented by some character, I will show you the first. You can just wrap the insertion lines in a conditional block to check for this.
if( $lines != 0 ) // or $linemysql[0][0] == '#' (assuming # is a "comment")
{
if($addauto)
$query = "insert into $databasetable values('','$linemysql');";
else
$query = "insert into $databasetable values('$linemysql');";
}
That being said, PLEASE! Do not ever use the code you posted in any internet facing application, you are putting user-provided data directly into the database, so it would be trivial to make a csv file containing SQL injection attack and change your mysql password, steal your data, kill your cat or delete everything. You should also check the number of fields and such, what happens if the csv contains lines without the correct number of fields ?
Read up on http://en.wikipedia.org/wiki/SQL_injection
and also http://php.net/PDO
Use this code in removing the header in the csv file and upload the data in the database:
$header= 1;
if (($handle = fopen("data.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if($header== 1)
{
$header++;
continue;
}
//do your code here saving into the database
}
fclose($handle);
}

Categories