www.webdeveloper.com
Results 1 to 3 of 3

Thread: Update into database instead of insert from csv

  1. #1
    Join Date
    Oct 2013
    Posts
    2

    Update into database instead of insert from csv

    Hi,

    I got a page that insert into database information from csv:

    PHP Code:
    <?php

    $databasehost 
    "host";
    $databasename "dbname";
    $databasetable "csvdata";
    $databaseusername ="dbusername";
    $databasepassword "dbpassword";
    $fieldseparator ";";
    $lineseparator "\n";
    $csvfile ="Item-file.csv";


    $addauto 0;

    $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";

    ?>
    But instead of inserting into the database I want see if the csv data change from the database data and update the database when it change.

    Thanks for your help

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,408
    If the data includes a column with a unique constraint in the database table, you could do an INSERT ... ON DUPLICATE KEY UPDATE ....
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Oct 2013
    Posts
    2
    I don't want to update an existing line, I want to see if there's a new line, if so, add the new line.

    Actually, if I run the page with the same csv file but with an extra line at the end, it add only the header of the csv file.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles