ReadQuestionsToDbFromCsvFile.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. $HOST = 'nordh.xyz';
  3. $USERNAME = 'narKampen';
  4. $PASSWORD = '9Bq.6[AcTc2ADwN-';
  5. $DATABASE = 'narKampen';
  6. $PORT = '3306';
  7. if (!isset($argv[1]) || !str_contains($argv[1], '.csv')) {
  8. echo 'Needs to supply a .csv with questions and answers';
  9. die();
  10. } else {
  11. $category = substr($argv[1],strripos($argv[1], '\\')+1,-4);
  12. $category = str_replace("_", " ", $category);
  13. }
  14. try {
  15. $dbh = new PDO(
  16. 'mysql:host=' . $HOST . ';dbname=' . $DATABASE,
  17. $USERNAME,
  18. $PASSWORD
  19. );
  20. } catch (PDOException $e) {
  21. echo '<h1>An error har occured. </h1><pre>', $e->getMessage(), '</pre>';
  22. die();
  23. }
  24. $sth = $dbh->query("SELECT id FROM Category WHERE name = '$category'");
  25. $sth->setFetchMode(PDO::FETCH_ASSOC);
  26. $result = $sth->fetchAll();
  27. if (count($result) > 0) {
  28. foreach ($result as $r) {
  29. echo "Category Id: " . $r['id'] . "\n";
  30. }
  31. }
  32. $realHash = md5($_GET['name'] . $_GET['score'] . $secretKey);
  33. if ($realHash == $hash) {
  34. $sth = $dbh->prepare('INSERT INTO scores VALUES (null, :name, :score)');
  35. try {
  36. $sth->execute($_GET);
  37. } catch (Exception $e) {
  38. echo '<h1>An error has ocurred.</h1><pre>', $e->getMessage(), '</pre>';
  39. }
  40. }
  41. ?>