简体   繁体   中英

Search for specific row in database and remove matching row in CSV in PHP

I'm looking to upload a CSV, compare the first column with a database and remove if it matches and output a new CSV.

Example of CSV:

tel,name,email
07777777777,Harry,harry@gmail.com
07777777788,Paul,paul@gmail.com

Example of database:

tel         name    email
07777777777 Harry   Harry,harry@gmail.com

End result of CSV file:

tel,name,email
07777777788,Paul,paul@gmail.com

I found example on here, but tried to amend it. Code so far:

if(isset($_POST['submit'])){
    require_once('db-config.php');
    
    $input_filename = $_FILES['file']['tmp_name'];
    $output_filename = 'output.csv';
    
    $input_file = fopen($input_filename, 'r');
    $output_file = fopen($output_filename, 'w');
    
    $tels = array();
    $query = "SELECT tel FROM people";
    $result = mysqli_query($connect, $query);
    while ($row = mysqli_fetch_array($result)) {
        $tels[] = $row['tels'];
    }
    
    // Read the header
    $headers = fgetcsv($input_file, 10000);
    fputcsv($output_file, $headers);
    
    // Deleted rows counter
    $rows_deleted = 0;
    // Read every row
    while($row = fgetcsv($input_file, 10000) !== FALSE) {
        $tel = $row[$headers['tel']];
        // Do we already have this tel?
        if(isset($tels, $tel)){
            // row skipped - therefore it is deleted
            $rows_deleted++;
            continue;
        }
    
        // Mark this tel as being found
        $tels[$tel]= true;
        // Write it to the output
        fputcsv($output_file, $row);
    }
    
    fclose($input_file);
    fclose($output_file);
        
    // Now we should move output file to input one
    echo "Deleted: " . $rows_deleted;
}

Your $tels array is a list of telehone numbers so the isset() wont work as the array will look like this for example, so replace that with in_array()

[0] 07777777777
[1] 07777777779

Also the fgetcsv() returns numeric array of the comman seperated content of a line so the telephone will be $row[0]

    while( ($row = fgetcsv($input_file, 10000) ) !== FALSE) {
        // change below
        $tel = $row[0];
    
        // Do we already have this tel?
        if(in_array($tel, $tels)){
            // row skipped - therefore it is deleted
            $rows_deleted++;
            continue;
        }

        // this tel does not exist, so write to new csv
        fputcsv($output_file, $row);
    

        // this tel wont exist in the $tels array? 
        // so not sure what you are doing this for ??
        // and of course the array does not look like that the keys are
        // numeric increments

        // Mark this tel as being found
        //$tels[$tel]= true;      
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM