Here are some links to relevant sections of the php manual.  What you want
to do is not too hard at all.

I'm assuming that you want to "upload" the csv file via a web page.  This
is much less trouble for the user than fooling around with ftp.  They
get to browse their computer for the file and then upload
with a single click.

1. Uploading a file

2. Opening the uploaded file

3. Reading fields from the uploaded file

4. Updating the table
It depends what database you are using as to how you do this.  The obvious
approach is to SELECT for each row in your csv file, and then either UPDATE
it or INSERT a new one, depending on whether you found it. 

So putting it altogether you want something like this.  You'll have to check
the syntax, add error checks etc., and this is based on mysql as the database:-

// refreshdata.php

if ($mycsvfile != '')
   $fp = fopen($mycsvfile);


   while (list($keyfield,$datafield) = fgetcsv($fp))
       $cur_table = mysql_query("select datafield from mytable where keyfield 
       if (mysql_num_rows($cur_table) == 0)
           $ins_table = mysql_query("insert into mytable (keyfield,datafield) values
       } else { 
           $upd_table = mysql_query("update mytable set datafield = '".$datafield."' 
where keyfield
= '".$keyfield."');


<FORM enctype=multipart/form-data method=post>

CSV file : <INPUT TYPE=FILE NAME=mycsvfile>


Coenraad Steenkamp wrote:
> I need to compare a csv file to a table in the database but only one field
> in the database with
> one field in the CSV file! Comparing only one field will make it much
> easier! When there is any change in the Database compared to the CSV file ,
> the database must then be updated or if there are no such a field it must be
> added to the table!
> I am new in php Please help!

PHP General Mailing List (
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to