importsql.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. require_once "mdie/connect.php";
  3. /* Recycled code
  4. Thanks to,
  5. https://www.webslesson.info/2017/06/how-to-import-sql-file-in-mysql-database-using-php.html
  6. */
  7. $message = '';
  8. if(isset($_POST["import"]))
  9. {
  10. if($_FILES["database"]["name"] != '')
  11. {
  12. $array = explode(".", $_FILES["database"]["name"]);
  13. $extension = end($array);
  14. if($extension == 'sql')
  15. {
  16. $connect = mysqli_connect("$servername", "$username", "$password", "$dbname");
  17. $output = '';
  18. $count = 0;
  19. $file_data = file($_FILES["database"]["tmp_name"]);
  20. foreach($file_data as $row)
  21. {
  22. $start_character = substr(trim($row), 0, 2);
  23. if($start_character != '--' || $start_character != '/*' || $start_character != '//' || $row != '')
  24. {
  25. $output = $output . $row;
  26. $end_character = substr(trim($row), -1, 1);
  27. if($end_character == ';')
  28. {
  29. if(!mysqli_query($connect, $output))
  30. {
  31. $count++;
  32. }
  33. $output = '';
  34. }
  35. }
  36. }
  37. if($count > 0)
  38. {
  39. $message = '<label class="text-danger">There is an error in Database Import</label>';
  40. }
  41. else
  42. {
  43. $message = '<label class="text-success">Database Successfully Imported</label>';
  44. }
  45. }
  46. else
  47. {
  48. $message = '<label class="text-danger">Invalid File</label>';
  49. }
  50. }
  51. else
  52. {
  53. $message = '<label class="text-danger">Please Select Sql File</label>';
  54. }
  55. }
  56. ?>
  57. <!DOCTYPE html>
  58. <html>
  59. <head>
  60. <script src="jquery.min.js"></script>
  61. <script src="bootstrap.min.js"></script>
  62. </head>
  63. <body>
  64. <p>Step 2</p><hr />
  65. <p>1) Import coinlist.sql from the sql/ directory. <br />2) Import exchanges.sql from the sql/ directory.<br /><br />After both files are imported, proceed to step 3</p>
  66. <br /><br />
  67. <div class="container" style="width:700px;">
  68. <br />
  69. <div><?php echo $message; ?></div>
  70. <form method="post" enctype="multipart/form-data">
  71. <p><label>Select SQL File</label>
  72. <input type="file" name="database" /></p>
  73. <br />
  74. <input type="submit" name="import" value="Import" />
  75. </form>
  76. </div>
  77. <p><a href="?step=3">Continue to Step 3</a></p>
  78. </body>
  79. </html>