i had done this task a month ago.I was given was to import csv file to mysql DB.
Step 1
Easily and good approach is to save the csv file as xml doc as option is present to save it as xml doc.
Step 2
Then import the xml doc to mysql.
Here is code below
index1.php
<html>
<head>
<style type="text/css">
body{
font-family:"Lucida Grande", "Lucida Sans Unicode", Verdana, Arial, Helvetica, sans-serif;
font-size:12px;
}
p, h1, form, button{border:0; margin:0; padding:0;}
.spacer{clear:both; height:1px;}
/* ----------- My Form ----------- */
.myform{
margin:0 auto;
width:400px;
padding:14px;
}
/* ----------- stylized ----------- */
#stylized{
border:solid 2px #B77DDF2;
background:#ebf4bf;
}
#stylized h1 {
font-size:14px;
font-weight:bold;
margin-bottom:8px;
}
#stylized p{
font-size:11px;
color:#666666;
margin-bottom:20px;
border-bottom:solid 1px #b7ddf2;
padding-bottom:10px;
}
#stylized label{
display:block;
font-weight:bold;
text-align:right;
width:140px;
float:left;
}
#stylized .small{
color:#666666;
display:block;
font-size:11px;
font-weight:normal;
text-align:right;
width:140px;
}
#stylized input{
float:left;
font-size:12px;
padding:4px 2px;
border:solid 1px #aacfe4;
width:200px;
margin:2px 0 20px 10px;
}
#stylized button{
clear:both;
margin-left:150px;
width:125px;
height:31px;
background:#666666 url(img/button.png) no-repeat;
text-align:center;
line-height:31px;
color:#FFFFFF;
font-size:11px;
font-weight:bold;
}
</style>
</head>
<body>
<div id="stylized" class="myform">
<form enctype="multipart/form-data"
action="import2.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
<h1>Sign-up form</h1>
<p></p>
<label>File
<span class="small">Brows CSV file</span>
</label>
<input type="file" name="file" id="file" />
<button type="submit">Upload</button>
<div class="spacer"></div>
</form>
</div>
</body>
</html>
Here is import file
import2.php
<?php
// error_reporting(0);
$data = array();
function add_person( $Name, $EmailAddress, $Company, $City, $Country, $Status ) //change here
{
global $data;
mysql_connect("localhost","root","") or die(mysql_error()); // Change here
mysql_select_db("n"); //Change here
$data []= array(
'Name' => $Name, // <---////////
'EmailAddress' => $EmailAddress, //
'Company' => $Company, // Changes here
'City' => $City, //
'Country' => $Country, //
'Status' => $Status // <----//////
);
}
if ( $_FILES['file']['tmp_name'] )
{
$dom = DOMDocument::load( $_FILES['file']['tmp_name'] );
$rows = $dom->getElementsByTagName( 'Row' );
$first_row = true;
foreach ($rows as $row)
{
if ( !$first_row )
{
$Name = ""; // <---////////
$EmailAddress = ""; //
$Company = ""; //
$City = ""; // Changes here
$Country = ""; //
$Status = ""; // <----//////
$index = 1;
$cells = $row->getElementsByTagName( 'Cell' );
foreach( $cells as $cell )
{
$ind = $cell->getAttribute( 'Index' );
if ( $ind != null ) $index = $ind;
if ( $index == 1 ) $Name = $cell->nodeValue;
if ( $index == 2 ) $EmailAddress = $cell->nodeValue;
if ( $index == 3 ) $Company = $cell->nodeValue;
if ( $index == 4 ) $City = $cell->nodeValue;
if ( $index == 5 ) $Country = $cell->nodeValue;
if ( $index ==6 ) $Status = $cell->nodeValue;
$index += 1;
}
add_person( $Name, $EmailAddress, $Company, $City, $Country, $Status );
}
$first_row = false;
}
}
?>
<html>
<body>
These records have been added to the database:
<table>
<?php foreach( $data as $row ) { ?>
<tr>
<td><?php ( $name1 = $row['Name'] ); //echo $name1 ?></td>
<td><?php ( $name2 = $row['EmailAddress'] ); //echo $name2?></td>
<td><?php ($name3 = $row['Company'] ); //echo $name3?></td>
<td><?php ($name4 = $row['City'] ); //echo $name4?></td> // Changes here
<td><?php ($name5 = $row['Country'] ); //echo $name5?></td>
<td><?php ($name6 = $row['Status'] ); //echo $name6?></td>
</tr>
<?php
$sql = mysql_query("INSERT INTO tblclients (Name,EmailAddress,Company,City,Country,Status) VALUES ('$name1','$name2','$name3','$name4','$name5','$name6')"); // Changes here
} ?>
</table>
</body>
</html>
Hope this help