1.Save your excel sheet in .csv format.
2.add a form to upload file.
addfile.php
<html>
<body>
<form action="uploadfile.php" method="post"
enctype="multipart/form-data">
<label for="file">Csv Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
3.add a configuration file for connection with database.
config.php
<?php
$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "root";
$mysql_database = "your database name";
$prefix = "";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
mysql_select_db($mysql_database, $bd) or die("Could not select database");
?>
4.Now finally the upload script
uploadfile.php
<?
include('myconfig.php');
if (($handle = fopen($_FILES['file']['tmp_name'], "r")) !== FALSE) {
$counter = 0;
while (($data = fgetcsv($handle, 100000, ',','"')) !== FALSE) {
$counter++;
if($counter==1){
continue;
}
// echo '<pre>';
// print_r($data) ;
$str = implode('\',\'',$data);
$str1 = '\''.$str.'\'';
$insertQuery="INSERT into tb_patient values($str1)";
// echo $insertQuery;
$q=mysql_query($insertQuery);
if($q)
{
echo "data entered successfully";
}
else
{
echo "query failed";
}
}
}
?>
2.add a form to upload file.
addfile.php
<html>
<body>
<form action="uploadfile.php" method="post"
enctype="multipart/form-data">
<label for="file">Csv Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
3.add a configuration file for connection with database.
config.php
<?php
$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "root";
$mysql_database = "your database name";
$prefix = "";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
mysql_select_db($mysql_database, $bd) or die("Could not select database");
?>
4.Now finally the upload script
uploadfile.php
<?
include('myconfig.php');
if (($handle = fopen($_FILES['file']['tmp_name'], "r")) !== FALSE) {
$counter = 0;
while (($data = fgetcsv($handle, 100000, ',','"')) !== FALSE) {
$counter++;
if($counter==1){
continue;
}
// echo '<pre>';
// print_r($data) ;
$str = implode('\',\'',$data);
$str1 = '\''.$str.'\'';
$insertQuery="INSERT into tb_patient values($str1)";
// echo $insertQuery;
$q=mysql_query($insertQuery);
if($q)
{
echo "data entered successfully";
}
else
{
echo "query failed";
}
}
}
?>
find in more detail on my new blog www.dynamic-coders.com
ReplyDelete