PHPOffice / PHPExcel

ARCHIVED
Other
11.46k stars 4.19k forks source link

Not Inserting into a Database #1299

Open mugiwara05 opened 7 years ago

mugiwara05 commented 7 years ago

I'm Using PHPExcel where I need to upload the excel to the database for less hassle of work but No errors show up and If I click Insert I will be able to see the information of that excel see the below picture for more information https://i.stack.imgur.com/gq1x3.png This is my table where the books will put this after import but nothing happens.

https://i.stack.imgur.com/o0dAl.png this is the excel that i want to import

`$dbcon = mysqli_connect('localhost', 'root', '', 'sti_lms'); if(isset($_POST["import"])) {

$file_name = $_FILES["excel"]["name"]; $array = explode('.', $file_name); $extension = end($array); // For getting Extension of selected file $allowed_extension = array("xls", "xlsx", "csv"); //allowed extension if(in_array($extension, $allowed_extension)) //check selected file extension is present in allowed extension array { $file = $_FILES["excel"]["tmp_name"]; // getting temporary source of excel file include("Classes/PHPExcel/IOFactory.php"); // Add PHPExcel Library in this code $html = "

"; $objPHPExcel = PHPExcel_IOFactory::load($file); // create object of PHPExcel library by using load() method and in load method define path of selected file

foreach ($objPHPExcel->getWorksheetIterator() as $worksheet)
    {
        $highestRow = $worksheet->getHighestRow();
            for($row=0; $row<=$highestRow; $row++)
                {
                        $html.="<tr>";
                    $accession = mysqli_real_escape_string($dbcon, $worksheet->getCellByColumnAndRow(0, $row)->getValue());
                    $book_title = mysqli_real_escape_string($dbcon, $worksheet->getCellByColumnAndRow(1, $row)->getValue());
                    $author = mysqli_real_escape_string($dbcon, $worksheet->getCellByColumnAndRow(2, $row)->getValue());
                    $publisher = mysqli_real_escape_string($dbcon, $worksheet->getCellByColumnAndRow(3, $row)->getValue());
                    $isbn = mysqli_real_escape_string($dbcon, $worksheet->getCellByColumnAndRow(4, $row)->getValue());
                    $year = mysqli_real_escape_string($dbcon, $worksheet->getCellByColumnAndRow(5, $row)->getValue());
                    $sql="INSERT INTO book (book_id,book_title,author,publisher,isbn,year) VALUES('".$accession."','".$book_title."','".$author."','".$publisher."','".$isbn."','".$year."')";    
                    mysqli_query($dbcon,$sql);
                    $html.= '<td>'.$accession.'</td>';
                    $html.= '<td>'.$book_title.'</td>';
                    $html.= '<td>'.$author.'</td>';
                    $html.= '<td>'.$publisher.'</td>';
                    $html.= '<td>'.$isbn.'</td>';
                    $html.= '<td>'.$year.'</td>';
                    $html.= "</tr>";    

                }
            }                       
    $html.='</table>';
    echo $html;
    echo '<br />Data Inserted';

     }
     else
     {

     }
    }`