Tuesday, 25 October 2016


<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
       <meta name="description" content="">
    <meta name="author" content="">
<!--- <link rel="icon" href="../../favicon.ico"> -->

    <title>Importing CSV File to MySQL using PHP with Bootstrap Templates</title>

    <!-- Bootstrap core CSS -->
    <link href="dist/css/bootstrap.min.css" rel="stylesheet">

    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
    <link href="assets/css/ie10-viewport-bug-workaround.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="dist/css/sticky-footer-navbar.css" rel="stylesheet">

    <!-- Just for debugging purposes. Don't actually copy these 2 lines! -->
    <!--[if lt IE 9]><script src="../../assets/js/ie8-responsive-file-warning.js"></script><![endif]-->
    <script src="assets/js/ie-emulation-modes-warning.js"></script>

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>

  <body>

    <!-- Fixed navbar -->
    <nav class="navbar navbar-default navbar-fixed-top">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">Sivasankar</a>
        </div>
        <div id="navbar" class="collapse navbar-collapse">
          <ul class="nav navbar-nav">
            <li class="active"><a href="http://sivasankarmsccs.blogspot.in/">Home</a></li>
            <li><a href="http://sivasankarmsccs.blogspot.in/">About</a></li>
            <li><a href="http://sivasankarmsccs.blogspot.in/">Contact</a></li>
     
          </ul>
        </div><!--/.nav-collapse -->
      </div>
    </nav>

    <!-- Begin page content -->
    <div class="container">
      <div class="page-header">

<h1>
Importing CSV File to MySQL using PHP <br /> with Bootstrap Templates.
</h1>

<div style="width:75%;">

  <div class="panel panel-primary">
    <div class="panel-heading" role="tab" id="headingOne">
      <h4 class="panel-title">
        <a role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
          Import User Account
        </a>
      </h4>
    </div>
    <div id="collapseOne" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne">
<div class="panel-body">
<form action="import_query.php" method="post" name="upload_excel" enctype="multipart/form-data">
<div>
<div class="col-xs-6">
<label>Import CSV/Excel File:</label>
<input type="file" class="form-control" multiple name="filename" id="filename">
<br />
<button type="submit" id="submit" class="btn btn-primary" name="submit" data-loading-text="Loading...">Upload</button>
</div>
</div>
</form>
</div>
    </div>
  </div>

<table class="table table-striped">
<tr>
<th>User Name</th>

<th>First Name</th>
<th>Last Name</th>
</tr>
<?php
include ('database.php');
$result= mysql_query("select * from user1 order by user_id ASC") or die (mysql_error());
while ($row= mysql_fetch_array ($result) ){
$id=$row['user_id'];
?>
<tr>
<td><?php echo $row['user_name']; ?></td>
<td><?php echo $row['first_name']; ?></td>
<td><?php echo $row['last_name']; ?></td>
</tr>
<?php } ?>
</table>
</div>

</div>

      </div>
    </div>

    <footer class="footer">
      <div class="container">
        <p class="text-muted">Copyright &copy; 2016, <a href="http://sivasankarmsccs.blogspot.in/">For More Refer this blog. </a> </p>
      </div>
    </footer>


    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script>window.jQuery || document.write('<script src="assets/js/vendor/jquery.min.js"><\/script>')</script>
    <script src="dist/js/bootstrap.min.js"></script>
    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
    <script src="assets/js/ie10-viewport-bug-workaround.js"></script>
  </body>
</html>


import_query.php


<?php
if (isset($_POST['submit']))
{
include('database.php');

//Import uploaded file to Database
$file_CSV = fopen($_FILES['filename']['tmp_name'], "r");

while (($data = fgetcsv($file_CSV, 1000, ",")) !== FALSE) {
mysql_query("INSERT into user1 (user_name, first_name, last_name, date_added)
values('$data[0]', '$data[1]', '$data[2]', NOW())");

}

fclose($file_CSV);

//print "Import done";
echo "<script type='text/javascript'>alert('Successfully Imported a CSV File for User!');</script>";
echo "<script>document.location='index.php'</script>";
//view upload form
}

?>

database.php

<?php
mysql_select_db('upload',mysql_connect('localhost','root','sagan'))or die(mysql_error());

?>

Output:




No comments:

Post a Comment