jump to navigation

Import excel file into mysql data base November 7, 2009

Posted by ashwinisingh in PHP.
Tags:
trackback

<?
i$db = mysql_connect(“localhost”, “root”, “”) or die(“Could not connect.”);

if(!$db)

die(“no db”);

if(!mysql_select_db(“test”,$db))

die(“No database selected.”);

if(isset($_POST[‘submit’]))

{

$filename=$_POST[‘filename’];

$handle = fopen(“$filename”, “r”);

while (($data = fgetcsv($handle, 1000, “,”)) !== FALSE)

{

$import=”INSERT into sample(name,email) values(‘$data[0]’,’$data[1]’)”;

mysql_query($import) or die(mysql_error());

}

fclose($handle);

print “Import done”;

}

else

{

print “<form action=’import.php’ method=’post’>”;

print “Type file name to import:<br>”;

print “<input type=’text’ name=’filename’ size=’20’><br>”;

print “<input type=’submit’ name=’submit’ value=’submit’></form>”;

}
?>

Comments»

No comments yet — be the first.

Leave a comment