简体   繁体   English

使用php将excel中的ang检索数据导出到数据库

[英]exporting ang retrieving data from excel to database using php

I am trying to create a feature in my program where in you could upload data to the database by uploading data from excel (uploading excel file) i have provided the code below but the data in the database is encoded(i provided a screenshot). 我正在尝试在程序中创建一个功能,在该功能中,您可以通过从excel上传数据(上传excel文件)将数据上传至数据库(我提供了以下代码,但对数据库中的数据进行了编码(我提供了屏幕截图))。 is there something wrong with the code? 代码有问题吗?

Excel format: Excel格式:

firstname lastname
_____________________
amir       kumar
jhon         doee

But this is what the data looks like when inserted into the database: 但这是将数据插入数据库后的样子:

在此处输入图片说明

This is my index.php: 这是我的index.php:

 <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">Only Excel/CSV File Import.</p>
        </div>
        <button type="submit" class="btn btn-default" name="Import" value="Import">Upload</button>
    </form>

import.php import.php

<?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;
        $sql_data = "SELECT * FROM person";
        echo $sql_data;
        while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
        {
            //print_r($emapData);
            //exit();
            $count++;
            if($count > 1) {
            $sql = "INSERT into person(firstname , lastname) values ('$emapData[0]','$emapData[1]')";
            mysql_query($sql);
            echo "success";

        }
        }
        fclose($file);
        echo 'CSV File has been successfully Imported';
        header('Location: index.php');
    }
    else
        echo 'Invalid File:Please Upload CSV File';
}
?>

Don't use the Excel file as is. 不要按原样使用Excel文件。 You see what gibberish you get. 你看得到什么胡言乱语。

Instead, do one of these: 而是,请执行以下操作之一:

  • "Export" the data from Excel into a .csv file, then LOAD DATA into MySQL. 将数据从Excel“导出”到.csv文件,然后将LOAD DATA到MySQL。

  • Use a PHP API that lets you fetch data from Excel. 使用PHP API,可让您从Excel中获取数据。 Then INSERT the data into a MySQL table. 然后INSERT数据放入一个MySQL表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM