<html>
<?php 
include("config.php");
session_start();
if (!isset($_SESSION['username'])) {
 header('Location: form.php');
 exit;
}
?> 
<html lang="en">
 <head>
  <meta charset="utf-8">
  <title>Import Excel To Mysql</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
        <script src="js/main.js"></script>
 </head>
 <body style="align:center;"><link rel="stylesheet" type="text/css" href="css/main.css" media="screen" />
    <div align="right"><a class="button2"  href="view.php">View</a> <a class="logoutbutton" href="logout.php">Logout</a></div>    
 
 <!-- Navbar
    ================================================== -->
 
 <div class="navbar navbar-inverse navbar-fixed-top">
  <div class="navbar-inner">
   <div class="container"> 
    <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
     <span class="icon-bar"></span>
     <span class="icon-bar"></span>
     <span class="icon-bar"></span>
    </a>
    
   </div>
  </div>
 </div>
 
 <div id="wrap">
 <div class="container">
  <div class="row">
   <div class="span3 hidden-phone"></div>
   <div class="span6" id="form-login">
    <form class="form-horizontal well" action="importrx.php" method="post" name="upload_excel" id="upload_excel" enctype="multipart/form-data">
     <fieldset>
      <legend>Import CSV/Excel file</legend>
      <div class="control-group">
       <div class="control-label">
       <div class="ffmsg" style="color:red;"></div>
       <div class="smsg" style="color:green;"></div>
        <label>CSV/Excel File:</label>
       </div>
       <div class="controls">
        <input type="file" name="file" id="file" class="input-large">
        <div class="fmsg" style="color:red;"></div>
        <span style="color:red;">
        <?php if(isset($_SESSION['succ'])) {
                                      echo $_SESSION['succ'];
                                      }
                                      unset($_SESSION['succ']);
                                ?><?php if(isset($_SESSION['error'])) {
                                      echo $_SESSION['error'];
                                      }
                                      unset($_SESSION['error']);
                                ?>
        <?php if(isset($_SESSION['error_msg'])) {
                                      echo $_SESSION['error_msg'];
                                      }
                                      unset($_SESSION['error_msg']);
                                ?>
        <?php if(isset($_SESSION['ext_error'])) {
                                      echo $_SESSION['ext_error'];
                                      }
                                      unset($_SESSION['ext_error']);
                                ?>
        <?php if(isset($_SESSION['file_error'])) {echo $_SESSION['file_error'];}
    unset($_SESSION['file_error']);
       ?>
        </span>
       </div>
      </div>
 
      <div class="control-group">
       <div class="controls">
       <button type="submit" class="submitbutton" id="submit" name="Import" class="btn btn-primary button-loading" data-loading-text="Loading...">Upload</button>
        
 
       </div>
      </div>
     </fieldset>
    </form>
   </div>
   <div class="span3 hidden-phone"></div>
  </div>
 
  <table class="table table-bordered" width="700" align="center">
   <thead>
       <tr align='center'>
        <th>Id</th>
                        <th>CName</th>
                        <th>CDesc</th>
                        <th>Image</th>
                    </tr> 
   </thead>
   <?php
    $SQLSELECT = "SELECT * FROM form_data ";
    if($result_set = mysqli_query($db, $SQLSELECT)){
                if(mysqli_num_rows($result_set) > 0){
    while($row = mysqli_fetch_array($result_set)){
     ?>
 
     <tr align='center'>
      <td><?php echo $row['id'];?></td>
      <td><?php echo $row['categoryname']; ?></td>
      <td><?php echo $row['categorydesc']; ?></td>
      <?php $photo = $row['image']; 
      echo "<td>" ."<img src='upload/{$photo}' height='42' width='42'>"."</td>"; ?>
     </tr>
    <?php
    }}
     else {
     echo "You have no records to show...!";
}
    }
   ?>
  </table>
 </div>
 
 </div>
 
 </body>
</html>


<script>
$('#upload_excel').on('submit',(function(e) {
        e.preventDefault();
		var file=$('#file').val();
		var ext = $('#file').val().split('.').pop().toLowerCase();
		//alert(file);
	    if(file==''){
			$('.fmsg').text('Please select a file.');
			}
		else if($.inArray(ext, ['csv']) == -1) {
          $('.fmsg').text('Invalid extension..! CSV file only.');
        }
		 else {
        
		var formData = new FormData(this);
	
        $.ajax({
            type:'POST',
            url: $(this).attr('action'),
            data:formData,
            cache:false,
            contentType: false,
            processData: false,
            success:function(data){ 
				if(data){
						 jQuery(".smsg").html("Data was inserted successfully.");
					}else{
						 jQuery(".ffmsg").html("Data was unable to insert...!");
				   }
			
            },
			
            error: function(data){
			
                console.log("error");
                console.log(data);
            }
			
        });
		}
    }));
	
</script>
	
<?php 
session_start();
include("config.php");
if(!empty($_FILES["file"]["name"])){

//$name = basename($_FILES["file"]["name"]);
		
$tmp_name=$_FILES["file"]["tmp_name"];
 
		  $file = fopen($tmp_name, "r");
	      while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
	      {

		  $sql = "INSERT into form_data (categoryname, categorydesc, image) values('$emapData[0]','$emapData[1]','$emapData[2]')";
	         
	      if($result = mysqli_query( $db, $sql )){
          echo 1;
	     } else{
		  echo 0;
	     }
        }
}
?>
Please login or Register to submit your answer

Log In

Not the answer you are looking for? Browse other questions tagged or ask your own question.