Hello,
I have made a program that can extract excel file then save it to mysql. However, is there a way to get confirmation first before adding it to the database?
Here is my simple code that I made. I just do not know how to please the confirmation button.
[php]<?php
require_once ‘Classes/PHPExcel/IOFactory.php’;
include ‘conn_db.php’;
$objPHPExcel = PHPExcel_IOFactory::load(‘includes/upload/’. $_FILES[“file”][“name”]);
$exists = $objPHPExcel->sheetNameExists(‘billing’);
if ($exists) {
$worksheet = $objPHPExcel->setActiveSheetIndexByName(‘billing’);
$worksheetTitle = $worksheet->getTitle();
$highestRow = $worksheet->getHighestRow(); // e.g. 10
$highestColumn = $worksheet->getHighestColumn(); // e.g 'F'
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
$nrColumns = ord($highestColumn) - 64;
echo "<br>The worksheet ".$worksheetTitle." has ";
echo $nrColumns . ' columns (A-' . $highestColumn . ') ';
echo ' and ' . $highestRow . ' row.';
echo '<br>Data: <table border="1"><br><tr>';
//Update batch table and increment/update the record.
$result = mysqli_query($con,"SELECT batch_no FROM batches");
$row = mysqli_fetch_array($result);
$batch_no = $row['batch_no'];
$batch_no += 1;
mysqli_query($con,"UPDATE batches SET batch_no='$batch_no'");
for ($row = 2; $row <= $highestRow; ++ $row) {
echo '<tr>';
for ($col = 0; $col < $highestColumnIndex; ++ $col) {
$cell = $worksheet->getCellByColumnAndRow($col, $row);
$val = $cell->getFormattedValue();//getValue();
$it[] = $val;
$dataType = PHPExcel_Cell_DataType::dataTypeForValue($val);
echo '<td>' . $val . '</td>'; //echo '<td>' . $val . '<br>(Typ ' . $dataType . ')</td>';
}
//Check if the next cell is empty
if ($val == '') {break;}
//add data into the database
$sql = "INSERT INTO `ExcelData`
(`batch_no`,`DELIVERY_DATE`,`CUSTOMER`,`PART_NUMBER`,`PO_NO`,`QTY`,`UP`,`TOTAL`,`SBU`)
VALUES
('$batch_no',STR_TO_DATE('$it[0]','%d-%b-%y'),'$it[1]','$it[2]','$it[3]','$it[4]','$it[5]','$it[6]','P1')";
// adding date link http://www.techonthenet.com/mysql/functions/str_to_date.php
$result = mysqli_query($con, $sql) or die('Error querying database.');
//echo "<td>Added</td>";
//reset the array
foreach ($it as $i => $value) {unset($it[$i]);}
$it = array_values($it);
echo '</tr>';
}
echo ‘’;
} else {
echo “
ERROR: billing sheet cannot be located on your Excel file! Please check
”;
}
?>[/php]