Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've downloaded PHPExcel and I typed a basic code which gives me nothing but blank page when I run it. Any advices?

What I have tried:

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
</head>

<body>
	<?php
     $conn = MySQLi( 'localhost', 'omri', '1234', 'omri' );
        if( $conn === false ) {
            echo "Could not connect.\n";
            die( print_r( sqlsrv_errors(), true));
        }
        include "Classes/PHPExcel.php";
        include "Classes/PHPExcel/IOFactory.php";
        //load excel file using PHPExcel's IOfactory
        $excel = PHPExcel_IOFactory::load('omri.xlsx');
        //set active sheet to first sheet
        $excel->setActiveSheetIndex(0);
        echo "<table>";
        echo "
                <tr>
                    <th>Name</th>
                    <th>ID</th>
                    <th>Citizenship</th>
                    <th>Expires</th>
                </tr>
                ";
        //first row of data series
        $i = 1 ;
        //loop until the end of data series(cell contains empty string)
        while($excel->getActiveSheet()->getcell('A'.$i)->getValue() != ""){
            //get cells value
            $name     = $excel->getactiveSheet()->getCell('A'.$i)->getValue();
            $id  = $excel->getactiveSheet()->getCell('B'.$i)->getValue();
            $citizenship = $excel->getactiveSheet()->getCell('C'.$i)->getValue();
            $expires    = $excel->getactiveSheet()->getCell('D'.$i)->getValue();
                $q = "INSERT INTO omri (name,id,citizenship,expires) VALUES ('$name','$id','$citizenship','$expires')";
                $d = sqlsrv_query($conn,$q);
            //echo
            echo "
                <tr>
                    <td>".$name."</td>
                    <td>".$id."</td>
                    <td>".$citizenship."</td>
                    <td>".$expires."</td>
                </tr>
            ";
            //and DON'T FORGET to increment the row pointer ($i)
            $i++;
        }
        echo "</table>";
        //echo error
        if( $d === false ) {
                if( ($errors = sqlsrv_errors() ) != null) {
                    foreach( $errors as $error ) {
                        echo "</br>SQLSTATE: ".$error[ 'SQLSTATE']."<br />";
                        echo "code: ".$error[ 'code']."<br />";
                        echo "message: ".$error[ 'message']."<br />";
                    }
                }
            }
	?>
</body>
</html>
Posted
Updated 21-Jul-18 3:23am

1 solution

PHP
$q = "INSERT INTO omri (name,id,citizenship,expires) VALUES ('$name','$id','$citizenship','$expires')";

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]

Secondary problem, it is impossible to know what is exactly your query because it depend on the values of the parameters.
On the debugger can show what is the real query.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900