-1

Lets say, i want to import/upload excel file to mysql from PHP

My HTML is like below

<form enctype="multipart/form-data" method="post" role="form" action="do.php">
    <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>

PHP code is like below

<?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= '123';
    $db= 'dev'; // 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");
        //$sql_data = "SELECT * FROM prod_list_1 ";
        while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
        {
            //print_r($emapData);
            //exit();
            $sql = "INSERT INTO `prayer` (`id` ,`country` ,`city` ,`day` ,`no` ,`datem` ,`fajer` ,`shrok` ,`dohor` ,`aser` ,`maghreb` ,`eshaa`) values ('','ksa','riyadh','$emapData[0]','$emapData[1]','$emapData[2]','$emapData[3]','$emapData[4]','$emapData[5]','$emapData[6]','$emapData[7]','$emapData[8]')";
            mysql_query($sql);
        }
        fclose($file);
        echo 'CSV File has been successfully Imported';
        //header('Location: index.php');
    }
    else
        echo 'Invalid File:Please Upload CSV File';
}
?>

I want to import the values into my table. Its import each row in one field '$emapData[0]' Please help me how can i solve it or give me any resource.

Edit:-

i check my $emapData content before Array ( [0] => 1;28/6;03:36;05:06;11:57; 3:18;06:47;08:47 ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) CSV File has been successfully Imported

my file like this

https://i.stack.imgur.com/mCJlH.jpg

user3576352
  • 1
  • 1
  • 4
  • 1
    A little google would help – zeddarn May 07 '14 at 09:32
  • @Adarsh M Pallickal no, my post is deffernt – user3576352 May 07 '14 at 10:05
  • Here comes the pony. Please don't duplicate only you've got a computer problem. Compare with your own question: [Import from csv to MySQL](http://stackoverflow.com/q/23515665/367456) – hakre May 07 '14 at 10:52
  • This question appears to be off-topic because it is a debugging request for some personal scripting needs. – hakre May 07 '14 at 10:54
  • Here comes a horde of the ponies: Never-Ever duplicate you own questions only you didn't get an answer so far: [PHP: Import data from csv to database \[on hold\]](http://stackoverflow.com/q/23501736/367456) – hakre May 07 '14 at 11:01

3 Answers3

0

you should parse your data

please show about your $emapData

for exemple you can try:

$data = explode(",",$emapData); 

check $emapData content before :)

pietro
  • 902
  • 10
  • 22
0

This should work.

$file_handle = fopen($filenamesv, "r");

while (!feof($file_handle) ) {

    $line_of_text = fgetcsv($file_handle, 1024);

    print $line_of_text[0] . $line_of_text[1]. $line_of_text[2] . "<BR>";

}

fclose($file_handle);
zeddarn
  • 302
  • 2
  • 14
0

To process Excel File we need to use external php library. Php don't have internal class to do that . you are using fgetcsv() that function is to read the CSV file. For excel file see this link

http://allitstuff.com/read-excel-file-in-php-with-example/

venkatesh
  • 164
  • 1
  • 13