Tuesday, 9 August 2016

PHP CODE EXAMPLE FOR IMPORTING CSV FILES

02:08

STEP 1:

create table myrecord
(id   int primary key auto_increment,
 name  varchar(200),
city     varchar(200)
);

STEP 2 :now write code for   import.php file:


 <?php
$hostname = "localhost";
$username = "root";
$password = "";
$database = "t";
$conn = mysql_connect("$hostname","$username","$password") or die(mysql_error());
mysql_select_db("$database", $conn) or die(mysql_error());
?>
<html>
<body>

<div >Import Excelsheet Data in mysql table<br><br>
</div>

<form name='import' method='post' enctype='multipart/form-data'>
<input type='file' name='file' /><br />
<input type='submit' name='submit' value='Submit' />
</form>
<?php
if(isset($_POST['submit']))
{
$file = $_FILES['file']['tmp_name'];
$handle = fopen($file, "r");
$c = 0;
while(($filesop = fgetcsv($handle, 1000,",")) !== false)
{
$name = $filesop[0];
$city = $filesop[1];

$sql = mysql_query("INSERT INTO myrecord (name,city) VALUES ('$name','$city')");
$c = $c + 1;
}

if($sql)
{
echo "You database has imported successfully. You have inserted ". $c ." recoreds";
}
else
{
echo "Sorry! There is some problem.";
}

}
?>

OUPUT:



Written by

We are Creative Blogger Theme Wavers which provides user friendly, effective and easy to use themes. Each support has free and providing HD support screen casting.

1 comments:

  1. I have read your blog its very attractive and impressive. I like it your blog. We also provide PHP training institute in delhi.. Thanks for sharing this nice post..!!

    ReplyDelete

PHP Training Mumbai, PHP MySQL Training Mumbai, PHP Course Mumbai, PHP Institute Mumbai

 

© 2013 PHP TRAINING IN MUMBAI BY OM SIR . All rights resevered. Designed by OM SIR

Back To Top