'PhpExcel inserts data more than once
I have a PHP script that opens and reads an excel file and then inserts to a MySQL database The trouble I am experiencing for a while now is that although the var_dump of the insert to db query displays the correct amount of records (which will not be more that 60 records) when I view the database tables there are sometimes 154 records? I have searched and searched for a few days and am not able to rectify the problem. Can anyone spot something in my code that could be causing this? I am a bit stumped as I say the var_dump of the insert query shows correct amount of records inserted...
<?PHP
include_once 'includes/functions/dbfunctions.php';
$link = dbConnect();
require_once "includes/Classes/PHPExcel.php";
$file= "flt_plan_input.xlsx";
$output = '';
$objPHPExcel = PHPExcel_IOFactory::load($file); // create object of PHPExcel library by using load() method and in load method define path of selected file
$output .= "<label class='text-success'>Data Inserted</label><br /><table width=\"100%\"class='table table-bordered' >";
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet)
{
$highestRow = $worksheet->getHighestRow();
for($row=2; $row<=$highestRow; $row++){
$output .= "<tr>";
$year = mysqli_real_escape_string($link, $worksheet->getCellByColumnAndRow(0, $row)->getFormattedValue());
$schedule = mysqli_real_escape_string($link, $worksheet->getCellByColumnAndRow(1, $row)->getFormattedValue());
$sector = mysqli_real_escape_string($link, $worksheet->getCellByColumnAndRow(2, $row)->getFormattedValue());
$type = mysqli_real_escape_string($link, $worksheet->getCellByColumnAndRow(3, $row)->getFormattedValue());
$reg = mysqli_real_escape_string($link, $worksheet->getCellByColumnAndRow(4, $row)->getFormattedValue());
$civalue = mysqli_real_escape_string($link, $worksheet->getCellByColumnAndRow(5, $row)->getFormattedValue());
$fltnum = mysqli_real_escape_string($link, $worksheet->getCellByColumnAndRow(6, $row)->getFormattedValue());
$airline = mysqli_real_escape_string($link, $worksheet->getCellByColumnAndRow(7, $row)->getFormattedValue());
$orig = mysqli_real_escape_string($link, $worksheet->getCellByColumnAndRow(8, $row)->getFormattedValue());
$dest = mysqli_real_escape_string($link, $worksheet->getCellByColumnAndRow(9, $row)->getFormattedValue());
$pax = mysqli_real_escape_string($link, $worksheet->getCellByColumnAndRow(10, $row)->getFormattedValue());
$cargo = mysqli_real_escape_string($link, $worksheet->getCellByColumnAndRow(11, $row)->getFormattedValue());
$date = mysqli_real_escape_string($link, $worksheet->getCellByColumnAndRow(12, $row)->getFormattedValue());
$deph = mysqli_real_escape_string($link, $worksheet->getCellByColumnAndRow(13, $row)->getFormattedValue());
$depm = mysqli_real_escape_string($link, $worksheet->getCellByColumnAndRow(14, $row)->getFormattedValue());
$steh = mysqli_real_escape_string($link, $worksheet->getCellByColumnAndRow(15, $row)->getFormattedValue());
$stem = mysqli_real_escape_string($link, $worksheet->getCellByColumnAndRow(16, $row)->getFormattedValue());
$taxiout = mysqli_real_escape_string($link, $worksheet->getCellByColumnAndRow(17, $row)->getFormattedValue());
$taxiin = mysqli_real_escape_string($link, $worksheet->getCellByColumnAndRow(18, $row)->getFormattedValue());
$units = mysqli_real_escape_string($link, $worksheet->getCellByColumnAndRow(19, $row)->getFormattedValue());
$oew= mysqli_real_escape_string($link, $worksheet->getCellByColumnAndRow(20, $row)->getFormattedValue());
$mzfw= mysqli_real_escape_string($link, $worksheet->getCellByColumnAndRow(21, $row)->getFormattedValue());
$mtow= mysqli_real_escape_string($link, $worksheet->getCellByColumnAndRow(22, $row)->getFormattedValue());
$mlw= mysqli_real_escape_string($link, $worksheet->getCellByColumnAndRow(23, $row)->getFormattedValue());
$max_fuel_capacity = mysqli_real_escape_string($link, $worksheet->getCellByColumnAndRow(24, $row)->getFormattedValue());
$passenger_weight= mysqli_real_escape_string($link, $worksheet->getCellByColumnAndRow(25, $row)->getFormattedValue());
$etops = mysqli_real_escape_string($link, $worksheet->getCellByColumnAndRow(26, $row)->getFormattedValue());
$query = "INSERT INTO flight_plan_fuel_input_data(year,schedule,sector,type,reg,civalue,fltnum,airline,orig,dest,pax,cargo,date,deph,depm,steh,stem,taxiout,taxiin,units,oew,mzfw,mtow,max_fuel_capacity,passenger_weight,etops,date_added)
VALUES('".$year."','".$schedule."','".$sector."','".$type."','".$reg."','".$civalue."','".$fltnum."','".$airline."','".$orig."','".$dest."','".$pax."','".$cargo."','".$date."','".$deph."','".$depm."','".$steh."','".$stem."','".$taxiout."','".$taxiin."','".$units."','".$oew."','".$mzfw."','".$mtow."','".$max_fuel_capacity."','".$passenger_weight."','".$etops."','".date("Y-m-d H:i:s")."')";
$result = mysqli_query($GLOBALS["___mysqli_ston"], $query) or die( "Error: " . ((is_object($link)) ? mysqli_error($link) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)) );
var_dump($query);
}
//$lastid = mysqli_insert_id($link);
//if($result){
//$sql = "DELETE FROM flight_plan_fuel_input_data where schedule = ''";
//$result = mysqli_query($GLOBALS["___mysqli_ston"], $sql) or die( "Error1: " . ((is_object($link)) ? mysqli_error($link) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)) );
//}
$output .= '</tr>';
$output .= '</table>';
}
header("Location: view_loaded_flights.php");
?>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
