Advertisement
Guest User

Untitled

a guest
Feb 26th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.53 KB | None | 0 0
  1. if (isset($_POST['submit'])) {
  2. if (is_uploaded_file($_FILES['filename']['tmp_name'])) {
  3. echo "<h1>" . "File ". $_FILES['filename']['name'] ." uploaded successfully." . "</h1>";
  4. echo "<h2>Displaying contents:</h2>";
  5. readfile($_FILES['filename']['tmp_name']);
  6. }
  7.  
  8. //Import uploaded file to Database
  9. $handle = fopen($_FILES['filename']['tmp_name'], "r");
  10.  
  11. while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
  12. $import="INSERT into tictoc(employee,taskname,tasktime,sessiontime,sessionstart,sessionend,sessionnotes) values('".$userinfo['first_name']." ".$userinfo['last_name']."','$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]')";
  13.  
  14. mysql_query($import) or die(mysql_error());
  15. }
  16.  
  17. fclose($handle);
  18.  
  19. print "Import done";
  20.  
  21. if (isset($_POST['submit'])) {
  22. $i=0; //so we can skip first row
  23.  
  24. if (is_uploaded_file($_FILES['filename']['tmp_name'])) {
  25. echo "<h1>" . "File ". $_FILES['filename']['name'] ." uploaded successfully." . "</h1>";
  26. echo "<h2>Displaying contents:</h2>";
  27. readfile($_FILES['filename']['tmp_name']);
  28. }
  29.  
  30. //Import uploaded file to Database
  31. $handle = fopen($_FILES['filename']['tmp_name'], "r");
  32.  
  33. while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
  34. if($i>0) {
  35. $import="INSERT into tictoc(employee,taskname,tasktime,sessiontime,sessionstart,sessionend,sessionnotes) values('".$userinfo['first_name']." ".$userinfo['last_name']."','$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]')";
  36.  
  37. mysql_query($import) or die(mysql_error());
  38. }
  39. $i++;
  40. }
  41.  
  42. fclose($handle);
  43.  
  44. print "Import done";
  45. }
  46.  
  47. $i=0;
  48. while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
  49. $i++;
  50. if($i==1) continue;
  51.  
  52. $import="INSERT into tictoc(employee,taskname,tasktime,sessiontime,sessionstart,sessionend,sessionnotes) values('".$userinfo['first_name']." ".$userinfo['last_name']."','$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]')";
  53.  
  54. mysql_query($import) or die(mysql_error());
  55. }
  56.  
  57. array_shift($data);
  58.  
  59. <?php
  60. ini_set('memory_limit','512M');
  61. $dbhost = "localhost";
  62. $dbname = "excel_import";
  63. $dbuser = "root";
  64. $dbpass = "";
  65.  
  66. $conn=mysql_connect ($dbhost, $dbuser, $dbpass) or die ("I cannot connect to the database because: " . mysql_error());
  67. mysql_select_db($dbname) or die("Unable to select database because: " . mysql_error());
  68.  
  69.  
  70. require_once 'CSV/DataSource.php';
  71.  
  72.  
  73. $filename = "users.csv";
  74. $ext = explode(".",$filename);
  75. $path = "uploads/".$filename;
  76.  
  77. $dbtable = $ext[0];
  78.  
  79. import_csv($dbtable, $path);
  80.  
  81.  
  82. function import_csv($dbtable, $csv_file_name_with_path)
  83. {
  84. $csv = new File_CSV_DataSource;
  85. $csv->load($csv_file_name_with_path);
  86.  
  87. $csvData = $csv->connect();
  88.  
  89. $res='';
  90. foreach($csvData as $key)
  91. {
  92. $myKey ='';
  93. $myVal='';
  94. foreach($key as $k=>$v)
  95. {
  96. $myKey .=$k.',';
  97. $myVal .="'".$v."',";
  98. }
  99.  
  100. $myKey = substr($myKey, 0, -1);
  101. $myVal = substr($myVal, 0, -1);
  102. $query="insert into ".$dbtable." ($myKey)values($myVal)";
  103. $res= mysql_query($query);
  104.  
  105. }
  106.  
  107. if($res ==1)
  108. {
  109.  
  110. echo "record successfully Import.";
  111.  
  112. }else{
  113.  
  114. echo "record not successfully Import.";
  115. }
  116. }
  117.  
  118. $dbh = new PDO('mysql:dbname='.$dbname, $username, $password);
  119.  
  120. if (is_uploaded_file($_FILES['filename']['tmp_name'])) {
  121. $qry = $dbh->prepare('
  122. LOAD DATA INFILE :filepath INTO TABLE tictoc
  123. FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
  124. IGNORE 1 LINES
  125. (tasktime, sessiontime, sessionstart, sessionend, sessionnotes)
  126. SET employee = :employee, taskname = :taskname
  127. ');
  128.  
  129. $qry->execute(array(
  130. ':filepath' => $_FILES['filename']['tmp_name'],
  131. ':employee' => $userinfo['first_name'],
  132. ':taskname' => $userinfo['last_name']
  133. ));
  134. }
  135.  
  136. $headers = fgetcsv($handle, 1000, ",");
  137.  
  138. if (isset($_POST['submit'])) {
  139. if (is_uploaded_file($_FILES['filename']['tmp_name'])) {
  140. echo "<h1>" . "File ". $_FILES['filename']['name'] ." uploaded successfully." . "</h1>";
  141. echo "<h2>Displaying contents:</h2>";
  142. readfile($_FILES['filename']['tmp_name']);
  143. }
  144.  
  145. //Import uploaded file to Database
  146. $handle = fopen($_FILES['filename']['tmp_name'], "r");
  147.  
  148. //Grab the headers before doing insertion
  149. $headers = fgetcsv($handle, 1000, ",");
  150.  
  151. while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
  152. $import="INSERT into tictoc(employee,taskname,tasktime,sessiontime,sessionstart,sessionend,sessionnotes) values('".$userinfo['first_name']." ".$userinfo['last_name']."','$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]')";
  153.  
  154. mysql_query($import) or die(mysql_error());
  155. }
  156.  
  157. fclose($handle);
  158.  
  159. print "Import done";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement