Home:ALL Converter>Upload excel file in MYSQL using PHP

Upload excel file in MYSQL using PHP

Ask Time:2015-06-04T19:47:50         Author:Akshay Vaghasiya

Json Formatter

I want to upload an Excel file into a MySQL table without using PDO, but just with a simple MySQL connection.

I want to load the Excel file using DOMDocument::load but can't get it to work.

Here's what I've tried so far:

mysql_connection("localhost", "root", "");
mysql_select_db("excel");

function add_data($abc, $pqr, $xyz) {
    $str = "INSERT INTO excel(abc, pqr, xyz) VALUES('$abc', '$pqr', '$xyz')";
    $qry = mysql_query($str);
}
if (isset($_FILES['file'])) {
    $dom = DOMDocument::load($_FILES['file']['tmp_name']);
    $rows = $dom->getElementsByTagName('Row');

    $first_row = true;
    foreach ($rows as $row) {
        if (!$first_row) {
            $abc = "";
            $pqr = "";
            $xyz = "";

            $index = 1;
            $cells = $row->getElementsByTagName('Cell');
            foreach ($cells as $cell) {
                $ind = $cell->getAttribute('Index');
                if ($ind != null)
                    $index = $ind;

                if ($index == 1)
                    $abc = $cell->nodeValue;
                if ($index == 2)
                    $pqr = $cell->nodeValue;
                if ($index == 3)
                    $xyz = $cell->nodeValue;

                $index += 1;
            }
            add_data($abc, $pqr, $xyz);
        }
        $first_row = false;
    }
}

Author:Akshay Vaghasiya,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/30643038/upload-excel-file-in-mysql-using-php
yy