0

what is the problem in the code below ? instead that the data is put into the row firstname and lastname the data is join into row firstname . i have sample screenshot in excel , and inserted data in the database , help would be appreciated. thanks

data in the databaseexcel file format

<form enctype="multipart/form-data" method="post" action="import.php" role="form">
    <div class="form-group">
        <label for="exampleInputFile">File Upload</label>
        <input type="file" name="file" id="file" size="150">
        <p class="help-block"> from excel save us .csv<p>
    </div>
    <button type="submit" class="btn btn-default" name="Import" value="Import">Upload</button>
</form>
<?php 
if(isset($_POST["Import"]))
{   
    //First we need to make a connection with the database
    $host='localhost'; // Host Name.
    $db_user= 'root'; //User Name
    $db_password= '';
    $db= 'testdatabase'; // Database Name.
    $conn=mysql_connect($host,$db_user,$db_password) or die (mysql_error());
    mysql_select_db($db) or die (mysql_error());
    echo $filename=$_FILES["file"]["tmp_name"];
    if($_FILES["file"]["size"] > 0)
    {
        $file = fopen($filename, "r");
        $count = 0;
        while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
        {
            $count++;
            if($count > 1) {
            $sql = "INSERT into person(firstname , lastname) values ('$emapData[0]','$emapData[1]')";
            mysql_query($sql);

        }
        }
        fclose($file);
        echo 'CSV File has been successfully Imported';
        header('Location: index.php');
        echo "Data was successfully added!";
    }
    else
        echo 'Invalid File:Please Upload CSV File';
}
?>
  • Can you show the raw CSV (in Notepad or similar) ? – dkasipovic Feb 02 '18 at 13:50
  • 2
    You are using bad delimiter in `fgetcsv` call. Lookin your data, they are colon `;` separated. – marcell Feb 02 '18 at 13:51
  • Can you comment what you mean Sir ? –  Feb 02 '18 at 13:52
  • i have provided the image of the excel above Sir –  Feb 02 '18 at 13:53
  • 2
    @TomonJoe see the `($emapData = fgetcsv($file, 10000, ",")` and note the `","` that you use as delimiter. Try with `";"`. – marcell Feb 02 '18 at 13:54
  • 2
    [Little Bobby](http://bobby-tables.com/) says **[you may be at risk for SQL Injection Attacks](https://stackoverflow.com/q/60174/)**. Learn about [Prepared Statements](https://en.wikipedia.org/wiki/Prepared_statement) with [parameterized queries](https://stackoverflow.com/a/4712113/5827005). I recommend `PDO`, which I [wrote a class for](https://github.com/GrumpyCrouton/GrumpyPDO) to make it extremely easy, clean, and more secure than using non-parameterized queries. Also, [This article](https://phpdelusions.net/pdo/mysqli_comparison) may help you choose between `MySQLi` and `PDO` – GrumpyCrouton Feb 02 '18 at 13:54
  • 3
    **Please**, don't use `mysql_*` functions for new code. They are no longer maintained and the community has begun the [deprecation process](http://news.php.net/php.internals/53799), and `mysql_*` functions have been officially removed in PHP 7. Instead you should learn about [prepared statements](https://en.wikipedia.org/wiki/Prepared_statement) and use either `PDO` or `mysqli_*`. If you can't decide, [this article will help to choose your best option](http://php.net/manual/en/mysqlinfo.api.choosing.php). – GrumpyCrouton Feb 02 '18 at 13:55
  • Can you save your CSV file and rename the extension to `.txt`. Then open the `.txt` file and show the contents. @TomonJoe – Ibrahim Hafiji Feb 02 '18 at 13:55
  • Thanks for all the help Sir i cant mention you all but thank you , i have fixed it. Thanks i have followed all of your advice thanks –  Feb 02 '18 at 14:00

1 Answers1

0

My guess is that your problem is there:

while (($emapData = fgetcsv($file, 10000, ";")) !== FALSE)

The problem with Excel creating a CSV file is the dependency with system configuration. I work a lot with french configuration wich is default setuped with ; as delimiter.

The best would be to ask the delemiter instead of hardcoding it in the php script, that's how I do it usually, or you could use the header the find what is between firstname and lastname, that would be your delimiter.

Alex Dupuis
  • 366
  • 3
  • 14