inserting 4 different files into 1 SQL table row in php - 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);
}
}
}
}
}
}
}
?>

Related

Trying to read cluttered CSV file

I am trying to read CSV file using fgetcsv() php function but It doesn't fetch detail as it supposed to be. I found out that the CSV file is cluttered and has multiple irrelevant commas in starting and in-between. How do I make this CSV cleaner?
I used str_replace() php function to remove triplets of commas but the commas in starting are still giving me a problem. I tried ltrim() also but that didn't work too.
<?php
$file = "grid.csv";
$s = file_get_contents($file);
$s = str_replace(",,,", "", $s);
//$s = ltrim($s,",");
$f = "grid1.csv";
$handle = fopen($f, "w");
fwrite($handle, $s);
?>
I expect the output of this code to be a clean csv file. But I get multiple commas in starting now also in the new file.
This is the Main Code where I was trying to read the file using fgetcsv().
if(isset($_POST["submit"])){
echo "in submit</br>";
if($_FILES['csv_info']['name']){
echo "some file</br>";
$filename = explode(".", $_FILES['csv_info']['name']);
if(end($filename) == 'csv'){
echo "file is csv</br>".$_FILES['csv_info']['tmp_name'];
$handle = fopen($_FILES['csv_info']['tmp_name'],"r");
$sid = 0;
//$query = "select exists(select 1 from tblMarks)";
//$choice = mysqli_query($conn, $query);
while($data = fgetcsv($handle)){
if($sid == 0){
$sid = $sid + 1;
continue;
}
//echo $data;
$name = mysqli_real_escape_string($conn, $data[0]);
$physics = mysqli_real_escape_string($conn, $data[1]);
$maths = mysqli_real_escape_string($conn, $data[2]);
$chemistry = mysqli_real_escape_string($conn, $data[3]);
$bio = mysqli_real_escape_string($conn, $data[4]);
$sst = mysqli_real_escape_string($conn, $data[5]);
echo "</br>inserting sid".$sid." name=".$name." physics=".$physics." maths=".$maths." chemistry=".$chemistry." bio=".$bio." sst=".$sst."</br>";
//$query = "insert into tblMarks (sid, name, physics, maths, chemistry, bio, sst) values ('$sid', '$name', '$physics', '$maths','$chemistry','$bio','$sst') on duplicate key update name = '$name', physics = '$physics',maths = '$maths', chemistry = '$chemistry', bio = '$bio', sst ='$sst'";
//mysqli_query($conn, $query);
$sid = $sid + 1;
}
fclose($handle);
}
else{
$message = '<label class="text-danger">Please Select CSV File Only</lable>';
}
}
else{
$message = '<label class="text-danger">Please Select File</label>';
}
}
The output was this:
OUTPUT
here is the correct method to read CSV file row by row. There are many rows in the CSV files which have blank values, to remove theme array_filter has been used.
$temp = array();
if (($h = fopen("grid.csv", "r")) !== FALSE)
{
// Convert each line into the local $data variable
while (($data = fgetcsv($h, 1000, ",")) !== FALSE)
{
$data = array_filter($data);
if(count($data) > 0){
$temp[] = $data;
}
}
fclose($h);
}
//Write csv file
$fp = fopen('grid1.csv', 'w');
foreach ($temp as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);

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
}
}

Parse .txt to csv and insert into database in 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;

Import a single CSV file into multiple tables in MySQL

I would like to import a single CSV with 7 columns into 2 tables in MySQL.
Columns 1, 2 and 3 go into a single row in table1. Columns 4 and 5 go as a row in table2. Columns 6 and 7 go as a row again in same table2.
How can this be done using PHP or mysql directly?
First fetch all values from csv and store it in an array. Then maintain your array as per your requirement and then start looping and inserting.
<?php
$efected = 0;
$file_temp = $_FILES["file"]["tmp_name"];
$handle = fopen($file_temp, "r"); // opening CSV file for reading
if ($handle) { // if file successfully opened
$i=0;
while (($CSVrecord = fgets($handle, 4096)) !== false) { // iterating through each line of our CSV
if($i>0)
{
list($name, $gender, $website, $category, $email, $subcat) = explode(',', $CSVrecord); // exploding CSV record (line) to the variables (fields)
//print_r($subcat); sub_cat_ids
if($email!='')
{
$chk_sql = "select * from employees where employee_email='".$email."'";
$chk_qry = mysqli_query($conn, $chk_sql);
$chk_num_row = mysqli_num_rows($chk_qry);
$cat_array = array(trim($category));
$subcat_array = explode( ';', $subcat );
if(count($subcat_array)>0)
{
$subcat_array_new = array();
foreach($subcat_array as $key => $val)
{
$subcat_array_new[] = trim($val);
}
}
$cat_sub_cat_merge = array_merge($cat_array, $subcat_array_new);
$cat_subcat_comma = implode( "','", $cat_sub_cat_merge );
foreach($cat_array as $cat_key => $cat_val)
{
$chk_cat_sql = "select * from employee_cats where cats_name = '".$cat_val."'";
$chk_cat_qry = mysqli_query($conn, $chk_cat_sql);
$chk_cat_num_row = mysqli_num_rows($chk_cat_qry);
//$all_cat_array = array();
if($chk_cat_num_row == 0)
{
$new_cat_ins = "insert into employee_cats set cats_name = '".$cat_val."', parent_cat_id = '0' ";
$new_cat_ins_qry = mysqli_query($conn, $new_cat_ins);
}
}
foreach($subcat_array_new as $subcat_key => $subcat_val)
{
$chk_subcat_sql = "select * from employee_cats where cats_name = '".$subcat_val."'";
$chk_subcat_qry = mysqli_query($conn, $chk_subcat_sql);
$chk_subcat_num_row = mysqli_num_rows($chk_subcat_qry);
//$all_cat_array = array();
if($chk_subcat_num_row == 0 && trim($subcat_val)!='')
{
//$category
$get_catid_sql = "select * from employee_cats where cats_name = '".trim($category)."'";
$chk_catid_qry = mysqli_query($conn, $get_catid_sql);
$fetch_cat_info = mysqli_fetch_array($chk_catid_qry);
$fetch_cat_id = $fetch_cat_info['cats_id'];
$new_subcat_ins = "insert into employee_cats set cats_name = '".$subcat_val."', parent_cat_id = '".$fetch_cat_id."' ";
$new_subcat_ins_qry = mysqli_query($conn, $new_subcat_ins);
}
}
$get_cat_sql = "select * from employee_cats where cats_name in ('".$cat_subcat_comma."')";
$get_cat_qry = mysqli_query($conn, $get_cat_sql);
$get_cat_num_row = mysqli_num_rows($get_cat_qry);
$sub_category_ids = array();
if($get_cat_num_row>0)
{
while($fetch_cat_id = mysqli_fetch_array($get_cat_qry))
{
if($fetch_cat_id['parent_cat_id']==0)
{
$category_id = $fetch_cat_id['cats_id'];
}
else
{
$sub_category_ids[] = $fetch_cat_id['cats_id'];
}
}
$sub_cat_id_vals_comma = implode(",", $sub_category_ids);
}
else
{
$category_id = 0;
$sub_cat_id_vals_comma = "";
}
if($chk_num_row>0)
{
// and here you can easily compose SQL queries and map you data to the tables you need using simple variables
$update_sql = "update employees set
employee_name='".$name."',
employee_gender='".$gender."',
employees_website='".$website."',
employees_cat_id='".$category_id."',
sub_cat_ids='".$sub_cat_id_vals_comma."'
where employee_email='".$email."'";
$mysqli_qry = mysqli_query($conn, $update_sql);
}
else
{
// and here you can easily compose SQL queries and map you data to the tables you need using simple variables
$insert_sql = "insert into employees set
employee_name='".$name."',
employee_gender='".$gender."',
employees_website='".$website."',
employees_cat_id='".$category_id."',
sub_cat_ids='".$sub_cat_id_vals_comma."',
employee_email='".$email."'";
$mysqli_qry = mysqli_query($conn, $insert_sql);
}
$efected = 1;
}
}
$i++;
}
fclose($handle); // closing file handler
}
if($efected==1)
{
header('location:'.$site_url.'?import=success');
}
else
{
header('location:'.$site_url.'?import=failed');
}
?>

How can i insert data from a text file to mysql?

I have some data in JSON format in a text file as below. I need to insert this data into mysql using php but can't do it.
{"address":"+92 334 6629424","service_center":"Test Sending Sms","id":3,"locked":0,"person":0,"protocol":0,"read":0,"reply_path_present":2,"seen":0,"error_code":0,"status":1,"date":1873326412,"thread_id":1,"type":-1}
My PHP file has the code like this.
<?php $source_file = "SMS2012-05-21.txt"; $handle = fopen("SMS2012-05-21.txt", "r");
$col_names = implode(",", fgetcsv($handle)); // Getting comma separated list of col name
$link = mysql_connect('localhost', 'root', '');
mysql_select_db("messages");
while (($data = fgetcsv($handle)) !== FALSE) {
$values = "";
foreach($data as $key => $value) {
if ($key != 0) $values .= ", ";
$values .= "'".mysql_escape_string($value)."'";
}
mysql_query('INSERT INTO messages ('.$col_names.') VALUES ('.$values.')');
}
?>
I can't find any result nor any error. Could any one please help me in this regard that where i am wrong?
You should use json_decode function to manipulate json data.
<?php
$source_file = "SMS2012-05-21.txt";
$string = file_get_contents($source_file);
$json = json_decode($string,true);
//DB Conn Handling Stuff
$cols = array(); $values = array();
foreach($json as $key=>$value)
{
array_push($cols,'`' . $key . '`');
if(is_string($value))
{
array_push($values,'\''.$value.'\'');
}
else
{
array_push ($values, $value);
}
}
$col_name = implode(',',$cols);
$col_value = implode(',',$values);
$query = 'INSERT INTO messages('.$col_name.') VALUES ('.$col_value.')';
mysql_query($query,$connection) or die(echo mysql_error());
?>
Maybe I've missed something, you should use it in this way:
<?php $source_file = "SMS2012-05-21.txt";
$handle = fopen("SMS2012-05-21.txt", "r");
$data = fread($handle, filesize($source_file));
$jsonArray = json_decode($data, true);
$keys = implode(',', array_keys($jsonArray));
$values = "'" . implode("','", $jsonArray) . "'";
$link = mysql_connect('localhost', 'root', '');
mysql_select_db("messages");
mysql_query('INSERT INTO messages ('.$keys.') VALUES ('.$values.')');

Categories