import excel file to php myadmin through file uploading usin

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jackgoddy123
Forum Newbie
Posts: 14
Joined: Sat Jan 11, 2014 8:10 am

import excel file to php myadmin through file uploading usin

Post by jackgoddy123 »

Hello,

Is it possible to upload excel file to my php myadmin. I know that all the field name of my phpmyadmin database table and my excel sheet table field should be same. But i am not finding the appropriate solution of this topic. The importing of my excel file code i am not finding out. And m confused in this.

Below is the code i tried out:

Code: Select all

<form action="upload.php">
        <input type="file" name="txtFile" id="eskal"  /></br>
<input type="submit" name="Import" value="Update Database" /> </b>
upload.php:

Code: Select all

<?php
if(isset($_POST["Import"]))
{
$host="localhost"; // Host name.
$db_user="root";
$db_password="";
$db='test'; // 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"];
//echo $ext=substr($filename,strrpos($filename,"."),(strlen($filename)-strrpos($filename,".")));


 if($_FILES["file"]["size"] > 0)
 {

  $file = fopen($filename, "r");
         while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
         {
            print_r($emapData);
            $sql = "INSERT into import(name,address,email,password) values('$emapData[0]','$emapData[1]')";
            mysql_query($sql);
         }
         fclose($file);
         echo "CSV File has been successfully Imported";
 }
 else
 echo "Invalid File:Please Upload CSV File";

}
?>
I had mistaken in my upload.php code. So, if some one could help me with the code here.
Any kind of help is appreciated.
Thanks in advance
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: import excel file to php myadmin through file uploading

Post by Christopher »

What error message do you get? Are you saving your spreadsheet as a CSV file before uploading?
(#10850)
jackgoddy123
Forum Newbie
Posts: 14
Joined: Sat Jan 11, 2014 8:10 am

Re: import excel file to php myadmin through file uploading

Post by jackgoddy123 »

Hello all,
I have found my solution. Now allz working perfectly fine. I am posting my full code.
upload.php:

Code: Select all

<html>
<body style="
    background-color: rgb(128, 151, 185);
">

<form action="import_file.php" method="post"
        enctype="multipart/form-data">
<table>
    <tr>
        <td>
            Filename:
        </td>
        <td>
            <input type="file" name="file" id="file">
        </td>
    </tr>
    <tr>
        <td colspan="2" align="right">
            <input type="submit" name="submit" value="Submit">
        </td>
    </tr>
</table>
</form>

</body>
</html>
import_file.php:

Code: Select all

<?php
if ($_FILES["file"]["error"] > 0)
{
    echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
else
{
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br>";
    //echo "Stored in: " . $_FILES["file"]["tmp_name"];
	$a=$_FILES["file"]["tmp_name"];
	//echo $a;
	
	$connect = mysql_connect('localhost','root','');
if (!$connect) {
die('Could not connect to MySQL: ' . mysql_error());
}	
//your database name
$cid =mysql_select_db('test',$connect);

// path where your CSV file is located
//define('CSV_PATH','C:/xampp/htdocs/');
//<!-- C:\xampp\htdocs -->
// Name of your CSV file
$csv_file = $a; 

if (($getfile = fopen($csv_file, "r")) !== FALSE) {
         $data = fgetcsv($getfile, 1000, ",");
   while (($data = fgetcsv($getfile, 1000, ",")) !== FALSE) {
     //$num = count($data);
	   //echo $num;
        //for ($c=0; $c < $num; $c++) {
            $result = $data;
        	$str = implode(",", $result);
        	$slice = explode(",", $str);
        
            $col1 = $slice[0];
            $col2 = $slice[1];
            $col3 = $slice[2];
			 $col4 = $slice[3];

$query = "INSERT INTO persons(id, name, email ,contacts) VALUES('".$col1."','".$col2."','".$col3."','".$col4."')";
$s=mysql_query($query, $connect );
}
}
echo "<script>alert('Record successfully uploaded.');window.location.href='edit_table.php';</script>";
//echo "File data successfully imported to database!!";
mysql_close($connect);
}
?>
The above is the full working code.
Thanks your rply guys.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: import excel file to php myadmin through file uploading

Post by Celauran »

You may want to consider collecting your values inside the while loop and running a single update once the loop ends rather than running the queries inside the loop. Also, mysql_ functions are deprecated and should not be used. Look at mysqli or, better, PDO.
Post Reply