Advertisement
Guest User

Untitled

a guest
Mar 27th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. *<?php
  2. if(isset($_POST['submit']))
  3. {
  4. $success = $_POST['success'];
  5. $dates = $_POST['dates'];
  6. $datee = $_POST['datee'];
  7. /*** mysql hostname ***/
  8. $hostname = 'localhost';
  9. $dbname = '*******';
  10. /*** mysql username ***/
  11. $username = 'root';
  12. /*** mysql password ***/
  13. $password = '*******';
  14. try {
  15. $dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
  16. $tablename = 'login_attempts';
  17. $sql = 'SHOW COLUMNS FROM `'.$tablename.'`';
  18. $fields = array();
  19. $csv = array();
  20.  
  21. $stmt = $dbh->query($sql);
  22. while($row = $stmt->fetch(PDO::FETCH_ASSOC))
  23. {
  24. array_push($fields, $row['Field']);
  25. }
  26.  
  27. array_push($csv, $fields);
  28. $success = mysql_real_escape_string($success);
  29. $sql = "SELECT * FROM $tablename WHERE success = '".$success."' AND attempted >='".$dates."' AND attempted <='".$datee."'";
  30. $stmt = $dbh->query($sql);
  31. $stmt->execute();
  32.  
  33. $csv = array();
  34.  
  35. while($row = $stmt->fetch(PDO::FETCH_NUM))
  36. {
  37. array_push($csv, $row);
  38. }
  39.  
  40. $fp = fopen('file.csv', 'w');
  41.  
  42. foreach ($csv as $row) {
  43. fputcsv($fp, $row);
  44. }
  45.  
  46. fclose($fp);
  47. header("Content-type: application/csv");
  48. header("Content-Disposition: attachment; filename=export.csv");
  49. header("Pragma: no-cache");
  50. header("Expires: 0");
  51. readfile('file.csv');
  52. $dbh = null;
  53. } catch(PDOException $e) {
  54. echo $e->getMessage();
  55. }
  56. exit();}
  57. ?>
  58. <html>
  59. <head>
  60. <title>csv with criteria</title>
  61. </head>
  62. <body>
  63.  
  64. <form action="csv2.php" method="post" enctype="multipart/form-data">
  65. Select data range
  66. <br>
  67. <input type="date" name="dates" id="dates"> Starting date
  68. <br>
  69. <input type="date" name="datee" id="datee"> Ending date
  70. <br>
  71. Select what data you'd like
  72. <br>
  73. <input type="radio" name="success" value="1" checked> Yes<br>
  74. <input type="radio" name="success" value="0"> No<br>
  75. <input type="submit" value="show" name="submit">
  76. <br>
  77. </form>
  78. </body>
  79. </html>*
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement