Thursday, 15 January 2015

php - inserting .csv values to database with an update insert query -



php - inserting .csv values to database with an update insert query -

i have .csv file info inserted database,but code has drawback

if .csv file contain row , inserted in db insert more row user should remove 1st row .csv , set new row value insert else duplicate key entry column in db have unique key.

my code inserting .csv mysql.

<?php //db connection goes here $csv_file="/var/www/html/141.csv"; $savefile="141.csv"; $handle = fopen($csv_file, "r"); $i=0; while (($data = fgetcsv($handle, 1000, ",")) !== false) { if($i>0){ $import="insert lead141(urn,phone)values('".addslashes($data[0])."','".addslashes($data[1])."')"; mysql_query($import) or die(mysql_error()); } $i=1; } $test="select * lead141 phone in (select phone dnc)"; $query=mysql_query($test); $testing=count($query); echo "matched rows".$testing; ?>

my mysql lead141table structure

create table `lead141` ( id int(100) not null auto_increment, urn int(100) not null, phone bigint(20) not null, primary key (`id`), unique key `phone` (`phone`) ) engine=innodb auto_increment=7 default charset=latin1

my 141.csv file

urn phone 141 20134325 141 20985675

if run code 141.csv inserting database when user adding new rows in 141.csv user should remove previous rows , add together new rows 141.csv inserting.

i need create code work insert new values 141.csv db evenif old values nowadays in .csv.i think if set update statement , insert trick.

am right path logic? please help

try utilize insert ... on duplicate key update syntax this:

$import="insert lead141(urn,phone)values('".addslashes($data[0])."','".addslashes($data[1])."') on duplicate key update phone= '".addslashes($data[1])."'";

because phone column unique key.

php mysql csv

No comments:

Post a Comment