0

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;
    }
}
LondonRob
  • 73,083
  • 37
  • 144
  • 201
Akshay Vaghasiya
  • 1,597
  • 9
  • 36
  • 60
  • Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). Why not use PDO? – Jay Blanchard Jun 04 '15 at 11:49
  • 2
    You do realise that an Excel file isn't simply html markup... a real Excel file is either OfficeOpenXML format (.xlsx) or BIFF-format (.xls) and neither can be parsed directly using DOMDocument – Mark Baker Jun 04 '15 at 11:49
  • @Mark. Then suggest me any alternative solution with example. – Akshay Vaghasiya Jun 04 '15 at 11:51
  • @AkshayVaghasiya You are asking right person Mark Baker PHPExcel author but try to learn by googling over the net please before posting here – gvgvgvijayan Jun 04 '15 at 11:52
  • Start by looking at any of the available PHP libraries for reading Excel files, such as [PHPExcel](https://github.com/PHPOffice/PHPExcel), most of which have plenty of documentation and examples – Mark Baker Jun 04 '15 at 12:00
  • @Mark Barker, I have seen that libraries. But I can't get any idea that which file is for uploading excel file into MYSQL? – Akshay Vaghasiya Jun 04 '15 at 12:07
  • None of those files are for uploading a spreadsheet, and none of those files are for inserting into a MySQL database..... PHPExcel is a library for parsing the content of Excel files (or creating them), you'd need to write some additional code yourself for uploading a file, and for the database insertion - [this might provide a basis for you](http://stackoverflow.com/questions/9695695/how-to-use-phpexcel-to-read-data-and-insert-into-database) – Mark Baker Jun 04 '15 at 12:13
  • @mark Barker, Got it. – Akshay Vaghasiya Jun 04 '15 at 12:19
  • Grammar and readability updated – LondonRob Jun 04 '15 at 14:49

0 Answers0