简体   繁体   中英

loop through records in excel and update specific rows in mysql database with values from the excel file in php

Hello am trying to update rows in my savings table but getting values from an excel file using a primary key as account_id which both the file and the table.My code does not execute like expected

   if(isset($_POST['import'])){
       $filename = explode(".", $_FILES["file"]["name"]);
       if ($filename[1]=="csv") {
           $file = $_FILES['file']['tmp_name'];
           $openFile = fopen($file,'r');

           while ($dataFile = fgetcsv($openFile,3000,",")) {
               $acc = $dataFile[0];
               $amount_deposited = $dataFile[2];
               $recovered_amount = $dataFile[3];

               /*update balances */
               $insert = "UPDATE savings SET balance = balance+'$amount_deposited' WHERE account_id ='$acc'";
               $run_insert = mysql_query($insert);

               $insert = "INSERT INTO transactions values('','$acc','Saving','$recovered_amount','',now(),'')";
               $run_insert = mysql_query($insert);
          }
          echo "<div id='alert'>Successfully ADDED</div>";
       } else {
          echo "You must choose a CSV file to import";
       }
    }

This worked,Thanks guys

              while ($dataFile = fgetcsv($openFile,3000,",")) {
               $acc = $dataFile[0];
                $amount_deposited = $dataFile[1];

               /*update balances */

               $insert = "UPDATE savings SET balance = balance+".intval($amount_deposited)." WHERE account_id ='$acc'";
               $run_insert = mysql_query($insert);

              $insert2 = "INSERT INTO trans values('','$amount_deposited',now(),'$acc')";
               $run_insert = mysql_query($insert2);
               }
              echo "<div id='alert'>Successfully ADDED</div>";
            }
            else{
              echo "You must choose a CSV file to import";
            }
          }

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