I have the following code to insert records into a database via a csv file
$get_columns = $db_website->prepare("SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'mytable' AND TABLE_NAME = 'products'");
$get_columns->execute();
while ($row = $get_columns->fetch(PDO::FETCH_ASSOC)) {
$want[] = $row['COLUMN_NAME'];
}
$file = fopen($_FILES['filename']['tmp_name'], "r");
$counter = 0;
while (!feof($file)) {
if ($counter === 1)
break;
$have = fgetcsv ($file, 5000);
++$counter;
}
fclose ($file);
$map = array_intersect($have, $want);
$num_feilds = implode($map);
$fields = "`".implode("`,`",$map)."`";
if ($num_feilds != '') {
$file = fopen($_FILES['filename']['tmp_name'], "r");
$line = fgetcsv($file, 1000, ",");
while (($line = fgetcsv($file)) !== FALSE) {
$data = array_intersect_key($line, $map);
$implode = str_replace("'", ''', $data);
$implode = str_replace("£", '£', $implode);
$implode = "'".implode("','",$implode)."'";
$query = $db_website->prepare("SELECT p.stock_id
FROM products AS p
WHERE p.stock_id = :data");
$query->bindValue(':data', $data[0], PDO::PARAM_INT);
$query->execute();
$product_exists = $query->rowCount();
if ($product_exists == 0) {
$product_import = "INSERT INTO products ($fields, token, date_created) VALUES ($implode, :token, :date_created)";
$product_import = $db_website->prepare($product_import);
$product_import->execute(array(':token'=>$token, ':date_created'=>$todays_date_time));
$update_slug = "UPDATE products SET slug = LOWER(title),
slug = replace(slug, char(128), '')
WHERE token = :token";
$update_slug = $db_website->prepare($update_slug);
$update_slug->execute(array(':token'=>$token));
} else {
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
$stock_id = $row['stock_id'];
$product_import = "UPDATE products SET $this_is_the_variable_i_need_to_create_from_the_implode, token = :token, date_updated = :date_updated
WHERE stock_id = :stock_id";
$product_import = $db_website->prepare($product_import);
$product_import->execute(array(':stock_id'=>$stock_id, ':token'=>$token, ':date_updated'=>$todays_date_time));
}
$update_slug = "UPDATE products SET slug = LOWER(title),
slug = replace(slug, char(128), '')
WHERE token = :token";
$update_slug = $db_website->prepare($update_slug);
$update_slug->execute(array(':token'=>$token));
}
}
fclose($file);
}
My problems lies in that I want it to update existing products as well as create new ones.
In the code above I have begun by doing a query to check whether the stock id exists and if it doesn't insert the record with an else to say update if it does.
The part I am struggling on is how do I make it implode the COLUMN_NAME and the data that is sent in the csv file.
Any tip in the right direction would be greatly appreciated.
Thank you
Dan
If I'm understanding you correctly, you need to create a series of set clauses based on what's in the $data array (which is an array containing the values from a single line of your CSV). Excluding any kind of validation (either of the columns in your import file, or the data in your import file) you could do something like this:
$sets = array();
$update_values = array();
foreach( $data as $index => $val )
{
if(empty($have[ $index ]))
continue;
$field_name = $have[ $index ];
$update_values[] = $val;
$sets[] = "{$field_name} = ':val{$index}'";
}
if( $sets )
{
$update_values[] = $stock_id;
$set_clause = implode(',',$sets);
$product_import = $db_website->prepare("UPDATE products SET {$set_clause} WHERE stock_id = :stock_id");
$product_import->execute( $update_values );
}
Again, you're going to want validate your input, but this should give you the idea.
Thank you oliakaoil,
This is the code I used in the end for anybody else who may need it in the future
$sets = array();
$update_values = array();
foreach ($data as $index => $val) {
if (empty($have[$index]))
continue;
$field_name = $have[$index];
$update_values[] = $val;
$sets[] = "{$field_name} = '{$val}'";
}
if ($sets) {
$update_values[] = $stock_id;
$set_clause = implode(',',$sets);
$product_import = "UPDATE products SET {$set_clause}, token = :token
WHERE stock_id = :stock_id";
$product_import = $db_website->prepare($product_import);
$product_import->execute(array(':stock_id'=>$update_values[0], ':token'=>$token));
}
Related
In my wordpress functions file, I'm getting sql query results of only post IDs, then taking those IDs to create an array that I send to my page.
This works perfectly but I want to re-order it based on one of the values I generate in the array process. I have a value of 'featured' and I want to make it so that any results where featured = 1 are first in the array.
$myresults = $wpdb->get_results( $sqlStatement );
$ress = array();
$cnt = 0;
$imgpath = '';
if(count($myresults)>0){
foreach ($myresults as $key => $value) {
$id = $value->ID;
if(get_post_meta($id,'_awpcp_extra_field[36]',true) !='' && get_post_meta($id,'_awpcp_extra_field[37]',true) != ''){
$ress[$cnt]['featured'] = get_post_meta($id,'_awpcp_is_featured',true);
$imgpath = get_the_post_thumbnail_url($id,'dailymag-featured');
if($imgpath){
$ress[$cnt]['imgs'] = $imgpath;
}else{
$ress[$cnt]['imgs'] = '/wp-content/uploads/2022/03/fairfax-4670d9c9.jpeg';
}
$cnt++;
}
}
echo json_encode($ress);
}
Is there a way to simply use conditions in order to put those values first in the array?
Using a function get_post_meta in an array is bad practice. I would advise first querying the database to extract data from custom fields.
However, I think there are two ways to answer your question:
Use 2 arrays and merge them together after iteration:
$myresults = $wpdb->get_results( $sqlStatement );
$ress1 = $ress2 = array();
$cnt = 0;
$imgpath = '';
if(count($myresults)>0){
foreach ($myresults as $key => $value) {
$id = $value->ID;
if(get_post_meta($id,'_awpcp_extra_field[36]',true) !='' && get_post_meta($id,'_awpcp_extra_field[37]',true) != ''){
$is_featured = get_post_meta($id,'_awpcp_is_featured',true);
$imgpath = get_the_post_thumbnail_url($id,'dailymag-featured');
$array_name = !empty($is_featured) ? 'ress1' : 'ress2';
$$array_name[$cnt]['featured'] = $is_featured;
if($imgpath){
$$array_name[$cnt]['imgs'] = $imgpath;
}else{
$$array_name[$cnt]['imgs'] = '/wp-content/uploads/2022/03/fairfax-4670d9c9.jpeg';
}
$cnt++;
}
}
$ress = array_merge($ress1, $ress2);
echo json_encode($ress);
}
Use 2 counters:
$myresults = $wpdb->get_results( $sqlStatement );
$ress = array();
$cnt1 = 0;
$cnt2 = 1000000;
$imgpath = '';
if(count($myresults)>0){
foreach ($myresults as $key => $value) {
$id = $value->ID;
if(get_post_meta($id,'_awpcp_extra_field[36]',true) !='' && get_post_meta($id,'_awpcp_extra_field[37]',true) != ''){
$is_featured = get_post_meta($id,'_awpcp_is_featured',true);
$imgpath = get_the_post_thumbnail_url($id,'dailymag-featured');
$counter_name = !empty($is_featured) ? 'cnt1' : 'cnt2';
$ress[$$counter_name]['featured'] = get_post_meta($id,'_awpcp_is_featured',true);
$imgpath = get_the_post_thumbnail_url($id,'dailymag-featured');
if($imgpath){
$ress[$$counter_name]['imgs'] = $imgpath;
}else{
$ress[$$counter_name]['imgs'] = '/wp-content/uploads/2022/03/fairfax-4670d9c9.jpeg';
}
$$counter_name++;
}
}
$ress = array_values( $ress );
echo json_encode($ress);
}
I want to get data from csv file having header row. I want to use the data using header row index. But it is showing undefined index. The code is as follows:
$count = 0;
$total = 0;
if ( false === $handle = fopen('hw_product.csv', 'r') )
throw new Exception('File open failed.');
$headers = fgetcsv($handle);
while ( false !== $fields = fgetcsv($handle) ) {
$fields = array_combine($headers, $fields);
$total++;
$p_id = $fields['id_product'];
$sku = $fields['sku'];
$url = $fields['url_key'];
$query = "UPDATE hw_product_lang SET link_rewrite = '$url' WHERE id_product = $p_id AND id_lang = 1";
if(mysqli_query($link,$query)){
$count++;
}
}
fclose($handle);
echo $count." rows processed out of ".$total;
Please help me what i am missing. Thanks
I am trying to import a csv file into the data base without defining any of the rows as it will be automatic when the page loads -
$file = '../csv/file.csv';
$table = 'table_name';
// get structure from csv and insert db
ini_set('auto_detect_line_endings',TRUE);
$handle = fopen($file,'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 = strtolower(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, 255);
$field_count++;
$fields[] = $f.' VARCHAR(255)';
}
}
$sql = "CREATE TABLE $table (" . implode(', ', $fields) . ')';
$conn->query($sql);
while ( ($data = fgetcsv($handle) ) !== FALSE ) {
$fields = array();
for($i=0;$i<$field_count; $i++) {
$fields[] = '\''.addslashes($data[$i]).'\'';
}
$sql = "Insert into $table values(" . implode(', ', $fields) . ')';
$conn->query($sql);
}
fclose($handle);
ini_set('auto_detect_line_endings',FALSE);
It loads the data in the page if I echo it and it creates the table correctly just doesn't load the data into the table and I cant find out why..
Thanks!
UPDATED
Here is the first row I am getting when I echo $sql -
Insert into 1001_inventory values('New', '581613', '88888888888888888', '2016', 'Toyota')
So I found that the varchar(255) was to small for some of the fields so I expanded it and the problem is solved, Thanks for all the help!
I'm trying to parse a small CSV file and insert the records into MySQL.
The problem I'm having is that the CSV has 150 rows, but only 2 seem to be inserted:
$file = new SplFileObject($uploadedFile);
$separator = ',';
$rowCounter = 1;
$errors = array();
$fh = fopen($uploadedFile, 'r');
if(!$fh) die('File no good!');
// Get headings
$headings = fgetcsv($fh, 0, ',');
$num_cols = count($headings);
$num_rows = 1;
// Read the file as csv
while ($row = $file->fgetcsv($separator)) {
//missed product if num columns in this row not the same as num headings
if (count($row) != $num_cols) {
$row = array_pad($row, $num_cols, NULL);
}
for ($i=0; $i<count($headings); $i++) {
$raw_prod[$headings[$i]] = $row[$i];
}
$item = new Item();
$item->name = $raw_prod['NAME'];
$item->age = $raw_prod['AGE'];
$item->location = $raw_prod['LOCATION'];
$item->save();
}
If I var_dump($item) I get all 150 records, but only 2 ever get inserted.
I just don't understand why this is happening
Thanks
This line
while ($row = $file->fgetcsv($separator)) {
should be
while (($row = $file->fgetcsv($separator)) !== false) {
I here have a codes that inputs excel into my table Biometrics. Its working but i cant find a way to import it to other table which has only 3 fields. in my CSV I have 5 columns. I only want to get in my CSV 1 column and put it in my User_dummy table can you help me with
`
mysql_select_db("hris_db",$link) or die ("Cannot select the database!");
// Set your CSV feed
$feed = 'excel/SampleLogs.csv';
//$uploadid = 2;
// Arrays we'll use later
$keys = array();
$newArray = array();
// Function to convert CSV into associative array
function csvToArray($file, $delimiter) {
if (($handle = fopen($file, 'r')) !== FALSE) {
$i = 0;
while (($lineArray = fgetcsv($handle, 0, $delimiter, '"')) !== FALSE) {
for ($j = 0; $j < count($lineArray); $j++) {
$arr[$i][$j] = $lineArray[$j];
}
$i++;
}
fclose($handle);
}
return $arr;
}
// Do it
$data = csvToArray($feed, ',');
// Set number of elements (minus 1 because we shift off the first row)
$count = count($data);
//Use first row for names
$labels = array('empno','date_created','time_created','status','device');
foreach ($labels as $label) {
$keys[] = trim($label);
}
// Bring it all together
for ($j = 0; $j < $count; $j++) {
$d = array_combine($keys, $data[$j]);
$newArray[$j] = array_map('trim',$d);
}
//count number of rows in database
$q = "SELECT * FROM biometrics";
$res = mysql_query($q);
$numrows = mysql_num_rows($res);
$slicearray = array_slice($newArray,$numrows);
echo $numrows;
var_dump($slicearray);
//$reverse = array_reverse($newArray,true);
//var_dump($reverse);
//$uniqueid = uniqid();
foreach($slicearray as $key=>$value){
$implodearray = "'" . implode($value, "','") . "'";
$keysString = implode(",", array_keys($value));
$keylower = strtolower(str_replace(str_split(" '-/"),'_',$keysString));
$sql = "INSERT INTO biometrics ($keylower)
SELECT * FROM (SELECT '".$value['empno']."','".$value['date_created']."','".$value['time_created']."','".$value['status']."' as status,'".$value['device']."' as device) As tmp
WHERE NOT EXISTS (SELECT $keylower FROM biometrics WHERE empno = '".$value['empno']."' AND date_created = '".$value['date_created']."' AND time_created = '".$value['time_created']."' AND status = '".$value['status']."' AND device = '".$value['device']."')";
mysql_query($sql) or die(mysql_error());
//echo $sql;
//var_dump($keylower);
//var_dump($value);
}
?>`
yes this is very simple
http://imgur.com/pZTzVbj
use this link image and you get idea how to import csv in database
or if you want to this by query then use it
LOAD DATA LOCAL INFILE '/your_file.csv'
INTO TABLE your_table
FIELDS TERMINATED BY ','
LINES TERMINATED BY ','
(field1, filed2, field3);