Narendra123

upload csv file into db php

Jul 12th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. <form enctype="multipart/form-data" method="post" role="form">
  2. <div class="form-group">
  3. <label for="exampleInputFile">File Upload</label>
  4. <input type="file" name="file" id="file" size="150">
  5. <p class="help-block">
  6. Only Excel/CSV File Import.
  7. </p>
  8. </div>
  9. <button type="submit" class="btn btn-default" name="Import" value="Import">Upload</button>
  10. </form>
  11. <?php
  12. if(isset($_POST["Import"]))
  13. {
  14. //First we need to make a connection with the database
  15. $host='localhost'; // Host Name.
  16. $db_user= 'root'; //User Name
  17. $db_password= '123';
  18. $db= 'excel'; // Database Name.
  19. $conn=mysql_connect($host,$db_user,$db_password) or die (mysql_error());
  20. mysql_select_db($db) or die (mysql_error());
  21. echo $filename=$_FILES["file"]["tmp_name"];
  22. if($_FILES["file"]["size"] > 0)
  23. {
  24. $file = fopen($filename, "r");
  25. //$sql_data = "SELECT * FROM prod_list_1 ";
  26. while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
  27. {
  28. //print_r($emapData);
  29. //exit();
  30. $sql = "INSERT into db(name,email) values ('$emapData[0]','$emapData[1]')";
  31. mysql_query($sql);
  32. }
  33. fclose($file);
  34. echo 'CSV File has been successfully Imported';
  35. //header('Location: index.php');
  36. }
  37. else
  38. echo 'Invalid File:Please Upload CSV File';
  39. }
  40. ?>
Add Comment
Please, Sign In to add comment