Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

2 Stron V   1 2 >  
Reply to this topicStart new topic
> Baza danych - niechciane pomnoże 1 rekordu, Jak wyżej
Nidan23
post 26.04.2019, 23:25:42
Post #1





Grupa: Zarejestrowani
Postów: 101
Pomógł: 2
Dołączył: 26.04.2019

Ostrzeżenie: (10%)
X----


Otóż tak, buduję aplikację gromadząca dane o graczach z którymi gram. Jest ona oparta o API udostępnione przez grę. Mam połączenie z bazą, skrypt/funkcja odpowiadająca za wprowadzanie rekordów śmiga doskonale (poza 1 szczegółem). A ta druga aktualizuje. Odwrotnie są napisane w kodzie, ale za późno na poprawki. Jednak gdy po zresetowaniu tabeli odświeżam stronę to wprowadza rekordy, to git, jednak za każdym kolejnym razem rekord o ID 1 jest aktualizowany i mnożony x40, pozostałe rekordy są tylko aktualizowane. Jakieś pomysły?

Poniżej wklejam kod:

  1. <?php
  2. foreach ($members as $member) {
  3.  
  4. // odbieramy dane z formularza
  5. $id = $member["clanRank"];
  6. $liga = $member["league"]["name"];
  7. $lvl = $member["expLevel"];
  8. $tag = $member["tag"];
  9. $nick = $member["name"];
  10. $ranga = $member["role"];
  11. $donated = $member["donations"];
  12. $received = $member["donationsReceived"];
  13. $puchary = $member["trophies"];
  14. $datan = date("Y-m-d");
  15. $czas = date("H:i");
  16. $ratio = $donated - $received;
  17.  
  18. if($id and $liga and $lvl and $tag and $nick and $ranga and $donated and $received and $puchary) {
  19.  
  20. // łączymy się z bazą danych
  21. $connection = @mysql_connect($servername, $username, $password)
  22. or die('Brak połączenia z serwerem MySQL');
  23. $db = @mysql_select_db($dbname, $connection)
  24. or die('Nie mogę połączyć się z bazą danych');
  25.  
  26. if (mysql_num_rows(mysql_query("SELECT tag FROM klan WHERE tag = '$tag';")) >= 1)
  27. {
  28.  
  29. $ins = @mysql_query("UPDATE klan SET miejsce='$id', liga='$liga', poziom='$lvl', tag='$tag', nick='$nick', ranga='$ranga', donated='$donated', received='$received', roznica='$ratio', puchary='$puchary', data='$datan', aktualizacja='$czas'");
  30.  
  31. }
  32. else {
  33.  
  34.  
  35. $inst = @mysql_query("INSERT INTO klan SET miejsce='$id', liga='$liga', poziom='$lvl', tag='$tag', nick='$nick', ranga='$ranga', donated='$donated', received='$received', roznica='$ratio', puchary='$puchary', data='$datan', aktualizacja='$czas'");
  36.  
  37. }
  38. }
  39. }
  40. if($ins) echo "Zaktualizowano bazę danych<br>";
  41. else echo "Błąd nie udało się zaktualizować<br>";
  42. if($inst) echo "Wprowadzono dane";
  43. else echo "Błąd nie udało się wprowdzić";
  44.  
  45.  
  46.  
  47. ?>
Go to the top of the page
+Quote Post
javafxdev
post 27.04.2019, 08:18:59
Post #2





Grupa: Zarejestrowani
Postów: 95
Pomógł: 7
Dołączył: 27.10.2015

Ostrzeżenie: (0%)
-----


"...ale za późno na poprawki..." czemu jest za późno na poprawki?
Go to the top of the page
+Quote Post
Nidan23
post 28.04.2019, 12:46:57
Post #3





Grupa: Zarejestrowani
Postów: 101
Pomógł: 2
Dołączył: 26.04.2019

Ostrzeżenie: (10%)
X----


Cytat(javafxdev @ 27.04.2019, 09:18:59 ) *
"...ale za późno na poprawki..." czemu jest za późno na poprawki?


Pisałem na telefonie ten post i nie chciało mi się poprawiać treści tematu, bo to czasochłonne, więc napisałem, że jest w odwrotnej kolejności.
Go to the top of the page
+Quote Post
javafxdev
post 28.04.2019, 22:46:42
Post #4





Grupa: Zarejestrowani
Postów: 95
Pomógł: 7
Dołączył: 27.10.2015

Ostrzeżenie: (0%)
-----


Coś takiego sobie zrób
  1. <?php
  2. $conf = array(
  3. 'dbhost' => "localhost",
  4. 'username' => "",
  5. 'password' => "",
  6. 'dbname' => "test"
  7. );
  8.  
  9. class DB
  10. {
  11.  
  12. private $dbhost;
  13. private $username;
  14. private $password;
  15. private $dbname;
  16. private $conn;
  17.  
  18. public function __construct($conf)
  19. {
  20. $this->dbhost = $conf['dbhost'];
  21. $this->username = $conf['username'];
  22. $this->password = $conf['password'];
  23. $this->dbname = $conf['dbname'];
  24. }
  25.  
  26. private function init()
  27. {
  28. $this->conn = new mysqli($this->dbhost, $this->username, $this->password, $this->dbname);
  29. if ($this->conn->connect_error) {
  30. die("Connection failed: " . $this->conn->connect_error);
  31. }
  32. $this->conn->query("SET NAMES 'utf8'");
  33. }
  34.  
  35. private function close()
  36. {
  37. $this->conn->close();
  38. }
  39.  
  40.  
  41. function getClanByTag($tag)
  42. {
  43. $rows = array();
  44. $this->init();
  45. $result = $this->conn->query("SELECT tag FROM klan WHERE tag = '$tag'");
  46.  
  47. if (false === $result) {
  48. printf("error: %s\n", mysqli_error($this->conn));
  49. }
  50. if ($result->num_rows != 0) {
  51. while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
  52. $rows[] = $row;
  53. }
  54.  
  55. }
  56. $this->close();
  57. return $rows;
  58. }
  59.  
  60. }
  61.  
  62. $m1['clanRank'] = 'a';
  63. $m1['expLevel'] = '12';
  64. $members = array($m1);
  65.  
  66. $db = new DB($conf);
  67.  
  68. foreach ($members as $member) {
  69. // odbieramy dane z formularza
  70. $id = $member["clanRank"];
  71. $liga = $member["league"]["name"];
  72. $lvl = $member["expLevel"];
  73. $tag = $member["tag"];
  74. $nick = $member["name"];
  75. $ranga = $member["role"];
  76. $donated = $member["donations"];
  77. $received = $member["donationsReceived"];
  78. $puchary = $member["trophies"];
  79. $datan = date("Y-m-d");
  80. $czas = date("H:i");
  81. $ratio = $donated - $received;
  82.  
  83. if ($id and $liga and $lvl and $tag and $nick and $ranga and $donated and $received and $puchary) {
  84.  
  85. $clanExist = $db->getClanByTag($tag);
  86. if ($clanExist) {
  87. $ins = $db->updateKlan();
  88. if ($ins) {
  89. echo "Zaktualizowano bazę danych<br>";
  90. } else {
  91. echo "Błąd nie udało się zaktualizować<br>";
  92. }
  93. } else {
  94. $ins = $db->insertKlan();
  95. if ($ins) {
  96. echo "Zaktualizowano bazę danych<br>";
  97. } else {
  98. echo "Błąd nie udało się zaktualizować<br>";
  99. }
  100. }
  101. }
  102. }
  103. ?>
Go to the top of the page
+Quote Post
Nidan23
post 29.04.2019, 08:31:53
Post #5





Grupa: Zarejestrowani
Postów: 101
Pomógł: 2
Dołączył: 26.04.2019

Ostrzeżenie: (10%)
X----


Cytat(javafxdev @ 28.04.2019, 23:46:42 ) *
Coś takiego sobie zrób


Cudownie, dziękuję, jeszcze tylko mi powiedz gdzie mam wkleić dane, które ma aktualizować bądź wprowadzać, bo sql'u i php jestem świeżakiem.

Wkleiłem skrypt uzupełniony jak ten poniżej co wkleje, i strona nie reaguje, ani A, ani B, po prostu nic, a baza pusta
  1. <?php
  2. $conf = array(
  3. 'dbhost' => "sql.nidan.nazwa.pl",
  4. 'username' => "nidan_klan",
  5. 'password' => "pSp100321",
  6. 'dbname' => "nidan_klan",
  7. );
  8.  
  9. class DB
  10. {
  11.  
  12. private $dbhost;
  13. private $username;
  14. private $password;
  15. private $dbname;
  16. private $conn;
  17.  
  18. public function __construct($conf)
  19. {
  20. $this->dbhost = $conf['dbhost'];
  21. $this->username = $conf['username'];
  22. $this->password = $conf['password'];
  23. $this->dbname = $conf['dbname'];
  24. }
  25.  
  26. private function init()
  27. {
  28. $this->conn = new mysqli($this->dbhost, $this->username, $this->password, $this->dbname);
  29. if ($this->conn->connect_error) {
  30. die("Connection failed: " . $this->conn->connect_error);
  31. }
  32. $this->conn->query("SET NAMES 'utf8'");
  33. }
  34.  
  35. private function close()
  36. {
  37. $this->conn->close();
  38. }
  39.  
  40.  
  41. function getClanByTag($tag)
  42. {
  43. $rows = array();
  44. $this->init();
  45. $result = $this->conn->query("SELECT tag FROM klan WHERE tag = '$tag'");
  46.  
  47. if (false === $result) {
  48. printf("error: %s\n", mysqli_error($this->conn));
  49. }
  50. if ($result->num_rows != 0) {
  51. while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
  52. $rows[] = $row;
  53. }
  54.  
  55. }
  56. $this->close();
  57. return $rows;
  58. }
  59.  
  60. }
  61.  
  62. $m1['clanRank'] = 'a';
  63. $m1['expLevel'] = '12';
  64. $members = array($m1);
  65.  
  66. $db = new DB($conf);
  67.  
  68. foreach ($members as $member) {
  69. // odbieramy dane z formularza
  70. $id = $member["clanRank"];
  71. $liga = $member["league"]["name"];
  72. $lvl = $member["expLevel"];
  73. $tag = $member["tag"];
  74. $nick = $member["name"];
  75. $ranga = $member["role"];
  76. $donated = $member["donations"];
  77. $received = $member["donationsReceived"];
  78. $puchary = $member["trophies"];
  79. $datan = date("Y-m-d");
  80. $czas = date("H:i");
  81. $ratio = $donated - $received;
  82.  
  83. if ($id and $liga and $lvl and $tag and $nick and $ranga and $donated and $received and $puchary) {
  84.  
  85. $clanExist = $db->getClanByTag($tag);
  86. if ($clanExist) {
  87. $ins = $db->updateKlan(@mysql_query("UPDATE klan SET miejsce='$id', liga='$liga', poziom='$lvl', tag='$tag', nick='$nick', ranga='$ranga', donated='$donated', received='$received', roznica='$ratio', puchary='$puchary', data='$datan', aktualizacja='$czas'"));
  88. if ($ins) {
  89. echo "Zaktualizowano bazę danych<br>";
  90. } else {
  91. echo "Błąd nie udało się zaktualizować<br>";
  92. }
  93. } else {
  94. $ins = $db->insertKlan(@mysql_query("INSERT INTO klan SET miejsce='$id', liga='$liga', poziom='$lvl', tag='$tag', nick='$nick', ranga='$ranga', donated='$donated', received='$received', roznica='$ratio', puchary='$puchary', data='$datan', aktualizacja='$czas'"));
  95. if ($ins) {
  96. echo "Zaktualizowano bazę danych<br>";
  97. } else {
  98. echo "Błąd nie udało się zaktualizować<br>";
  99. }
  100. }
  101. }
  102. }
  103. ?>
Go to the top of the page
+Quote Post
javafxdev
post 29.04.2019, 19:17:32
Post #6





Grupa: Zarejestrowani
Postów: 95
Pomógł: 7
Dołączył: 27.10.2015

Ostrzeżenie: (0%)
-----


Spróbuj tak:
  1. <?php
  2. ini_set('display_errors', 1);
  3. ini_set('display_startup_errors', 1);
  4.  
  5. $conf = array(
  6. 'dbhost' => "sql.nidan.nazwa.pl",
  7. 'username' => "nidan_klan",
  8. 'password' => "pSp100321",
  9. 'dbname' => "nidan_klan",
  10. );
  11.  
  12. class DB
  13. {
  14.  
  15. private $dbhost;
  16. private $username;
  17. private $password;
  18. private $dbname;
  19. private $conn;
  20.  
  21. public function __construct($conf)
  22. {
  23. $this->dbhost = $conf['dbhost'];
  24. $this->username = $conf['username'];
  25. $this->password = $conf['password'];
  26. $this->dbname = $conf['dbname'];
  27. }
  28.  
  29. private function init()
  30. {
  31. $this->conn = new mysqli($this->dbhost, $this->username, $this->password, $this->dbname);
  32. if ($this->conn->connect_error) {
  33. die("Connection failed: " . $this->conn->connect_error);
  34. }
  35. $this->conn->query("SET NAMES 'utf8'");
  36. }
  37.  
  38. private function close()
  39. {
  40. $this->conn->close();
  41. }
  42.  
  43.  
  44. function getClanByTag($tag)
  45. {
  46. $rows = array();
  47. $this->init();
  48. $result = $this->conn->query("SELECT * FROM klan WHERE tag = '$tag'");
  49.  
  50. if (false === $result) {
  51. printf("error: %s\n", mysqli_error($this->conn));
  52. }
  53. if ($result->num_rows != 0) {
  54. while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
  55. $rows[] = $row;
  56. }
  57.  
  58. }
  59. $this->close();
  60. return $rows;
  61. }
  62.  
  63. public function insertKlan()
  64. {
  65. $rows = array();
  66. $this->init();
  67. $result = $this->conn->query("INSERT INTO klan");
  68.  
  69. if (false === $result) {
  70. printf("error: %s\n", mysqli_error($this->conn));
  71. }
  72. if ($result->num_rows != 0) {
  73. while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
  74. $rows[] = $row;
  75. }
  76.  
  77. }
  78. $this->close();
  79. return $rows;
  80. }
  81.  
  82. public function updateKlan()
  83. {
  84. $rows = array();
  85. $this->init();
  86. $result = $this->conn->query("UPDATE klan");
  87.  
  88. if (false === $result) {
  89. printf("error: %s\n", mysqli_error($this->conn));
  90. }
  91. if ($result->num_rows != 0) {
  92. while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
  93. $rows[] = $row;
  94. }
  95.  
  96. }
  97. $this->close();
  98. return $rows;
  99. }
  100.  
  101.  
  102. }
  103.  
  104. $m1['clanRank'] = 'a';
  105. $m1['expLevel'] = '12';
  106. $m1["league"]["name"] = 'aa';
  107. $m1["expLevel"] = 'aa';
  108. $m1["tag"] = 'aa';
  109. $m1["name"] = 'aa';
  110. $m1["role"] = 'aa';
  111. $m1["donations"] = 'aa';
  112. $m1["donationsReceived"] = 'aa';
  113. $m1["trophies"] = 'aa';
  114. $members = array($m1);
  115.  
  116. $db = new DB($conf);
  117.  
  118. foreach ($members as $member) {
  119. // odbieramy dane z formularza
  120. $id = $member["clanRank"];
  121. $liga = $member["league"]["name"];
  122. $lvl = $member["expLevel"];
  123. $tag = $member["tag"];
  124. $nick = $member["name"];
  125. $ranga = $member["role"];
  126. $donated = $member["donations"];
  127. $received = $member["donationsReceived"];
  128. $puchary = $member["trophies"];
  129. $datan = date("Y-m-d");
  130. $czas = date("H:i");
  131. $ratio = $donated - $received;
  132.  
  133. if ($id and $liga and $lvl and $tag and $nick and $ranga and $donated and $received and $puchary) {
  134.  
  135. $clanExist = $db->getClanByTag($tag);
  136. if ($clanExist) {
  137. $ins = $db->updateKlan();
  138. if ($ins) {
  139. echo "Zaktualizowano bazę danych<br>";
  140. } else {
  141. echo "Błąd nie udało się zaktualizować<br>";
  142. }
  143. } else {
  144. $ins = $db->insertKlan();
  145. if ($ins) {
  146. echo "Zaktualizowano bazę danych<br>";
  147. } else {
  148. echo "Błąd nie udało się zaktualizować<br>";
  149. }
  150. }
  151. }
  152. }
  153. ?>
Go to the top of the page
+Quote Post
Nidan23
post 29.04.2019, 20:20:53
Post #7





Grupa: Zarejestrowani
Postów: 101
Pomógł: 2
Dołączył: 26.04.2019

Ostrzeżenie: (10%)
X----


Okey, tylko jak mówiłem, jestem ułomny i te wszystkie "m1" (zapomniałem wspomnieć), nie może być, bo na początku dokumentu (ten skrypt to końcówka), "members" zostało zdefiniowane jako "members = $data['clanMembers']". I dalej nie wiem gdzie wkleić funkcję sql'a. Dzięki z góry (i już dołu) za pomoc.
Go to the top of the page
+Quote Post
javafxdev
post 29.04.2019, 20:24:06
Post #8





Grupa: Zarejestrowani
Postów: 95
Pomógł: 7
Dołączył: 27.10.2015

Ostrzeżenie: (0%)
-----


linijki od 105 do 115 możesz wywalić
Go to the top of the page
+Quote Post
Nidan23
post 29.04.2019, 20:45:00
Post #9





Grupa: Zarejestrowani
Postów: 101
Pomógł: 2
Dołączył: 26.04.2019

Ostrzeżenie: (10%)
X----


Strona nie odpowiada po wklejeniu tego skrypt - nie może obsłużyć żądania...
Wkleje poniżej wszelkie skrypty i zmienne php jakie wystąpiły w tym kodzie, może to cokolwiek pomoże...

  1. <?php $clantag = "#28LJOYOLQ";
  2.  
  3. $clanurl = "https://api.clashofclans.com/v1/clans/" . urlencode($clantag);
  4.  
  5. $ch = curl_init($clanurl);
  6.  
  7. $headr = array();
  8. $headr[] = "Accept: application/json";
  9. $headr[] = "Authorization: Bearer ".$token;
  10.  
  11. curl_setopt($ch, CURLOPT_HTTPHEADER, $headr);
  12. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  13. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  14. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  15.  
  16. $res = curl_exec($ch);
  17. $data = json_decode($res, true);
  18. curl_close($ch);
  19.  
  20. if (isset($data["reason"])) {
  21. $errormsg = true;
  22. }
  23.  
  24. $members = $data["memberList"];
  25.  
  26. ?>
  27.  
  28.  
  29. <?php
  30. if (isset($errormsg)) {
  31. echo "<p>", "Failed: ", $data["reason"], " : ", isset($data["message"]) ? $data["message"] : "", "</p>";
  32.  
  33. }
  34. ?>
  35.  
  36.  
  37. <?php
  38. ini_set('display_errors', 1);
  39.  
  40. ini_set('display_startup_errors', 1);
  41.  
  42.  
  43.  
  44.  
  45. $conf = array(
  46.  
  47. 'dbhost' => "sql.nidan.nazwa.pl",
  48.  
  49. 'username' => "nidan_klan",
  50.  
  51. 'password' => "haslo",
  52.  
  53. 'dbname' => "nidan_klan",
  54.  
  55. );
  56.  
  57.  
  58.  
  59. class DB
  60.  
  61. {
  62.  
  63.  
  64.  
  65. private $dbhost;
  66.  
  67. private $username;
  68.  
  69. private $password;
  70.  
  71. private $dbname;
  72.  
  73. private $conn;
  74.  
  75.  
  76.  
  77. public function __construct($conf)
  78.  
  79. {
  80.  
  81. $this->dbhost = $conf['dbhost'];
  82.  
  83. $this->username = $conf['username'];
  84.  
  85. $this->password = $conf['password'];
  86.  
  87. $this->dbname = $conf['dbname'];
  88.  
  89. }
  90.  
  91.  
  92.  
  93. private function init()
  94.  
  95. {
  96.  
  97. $this->conn = new mysqli($this->dbhost, $this->username, $this->password, $this->dbname);
  98.  
  99. if ($this->conn->connect_error) {
  100.  
  101. die("Connection failed: " . $this->conn->connect_error);
  102.  
  103. }
  104.  
  105. $this->conn->query("SET NAMES 'utf8'");
  106.  
  107. }
  108.  
  109.  
  110.  
  111. private function close()
  112.  
  113. {
  114.  
  115. $this->conn->close();
  116.  
  117. }
  118.  
  119.  
  120.  
  121.  
  122.  
  123. function getClanByTag($tag)
  124.  
  125. {
  126.  
  127. $rows = array();
  128.  
  129. $this->init();
  130.  
  131. $result = $this->conn->query("SELECT * FROM klan WHERE tag = '$tag'");
  132.  
  133.  
  134.  
  135. if (false === $result) {
  136.  
  137. printf("error: %s\n", mysqli_error($this->conn));
  138.  
  139. }
  140.  
  141. if ($result->num_rows != 0) {
  142.  
  143. while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
  144.  
  145. $rows[] = $row;
  146.  
  147. }
  148.  
  149.  
  150.  
  151. }
  152.  
  153. $this->close();
  154.  
  155. return $rows;
  156.  
  157. }
  158.  
  159.  
  160.  
  161. public function insertKlan(@mysql_query("INSERT INTO klan SET miejsce='$id', liga='$liga', poziom='$lvl', tag='$tag', nick='$nick', ranga='$ranga', donated='$donated', received='$received', roznica='$ratio', puchary='$puchary', data='$datan', aktualizacja='$czas'"))
  162.  
  163. {
  164.  
  165. $rows = array();
  166.  
  167. $this->init();
  168.  
  169. $result = $this->conn->query("INSERT INTO klan");
  170.  
  171.  
  172.  
  173. if (false === $result) {
  174.  
  175. printf("error: %s\n", mysqli_error($this->conn));
  176.  
  177. }
  178.  
  179. if ($result->num_rows != 0) {
  180.  
  181. while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
  182.  
  183. $rows[] = $row;
  184.  
  185. }
  186.  
  187.  
  188.  
  189. }
  190.  
  191. $this->close();
  192.  
  193. return $rows;
  194.  
  195. }
  196.  
  197.  
  198.  
  199. public function updateKlan(@mysql_query("UPDATE klan SET miejsce='$id', liga='$liga', poziom='$lvl', tag='$tag', nick='$nick', ranga='$ranga', donated='$donated', received='$received', roznica='$ratio', puchary='$puchary', data='$datan', aktualizacja='$czas'"))
  200.  
  201. {
  202.  
  203. $rows = array();
  204.  
  205. $this->init();
  206.  
  207. $result = $this->conn->query("UPDATE klan");
  208.  
  209.  
  210.  
  211. if (false === $result) {
  212.  
  213. printf("error: %s\n", mysqli_error($this->conn));
  214.  
  215. }
  216.  
  217. if ($result->num_rows != 0) {
  218.  
  219. while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
  220.  
  221. $rows[] = $row;
  222.  
  223. }
  224.  
  225.  
  226.  
  227. }
  228.  
  229. $this->close();
  230.  
  231. return $rows;
  232.  
  233. }
  234.  
  235.  
  236.  
  237.  
  238.  
  239. }
  240.  
  241.  
  242. $db = new DB($conf);
  243.  
  244.  
  245.  
  246. foreach ($members as $member) {
  247.  
  248. // odbieramy dane z formularza
  249.  
  250. $id = $member["clanRank"];
  251.  
  252. $liga = $member["league"]["name"];
  253.  
  254. $lvl = $member["expLevel"];
  255.  
  256. $tag = $member["tag"];
  257.  
  258. $nick = $member["name"];
  259.  
  260. $ranga = $member["role"];
  261.  
  262. $donated = $member["donations"];
  263.  
  264. $received = $member["donationsReceived"];
  265.  
  266. $puchary = $member["trophies"];
  267.  
  268. $datan = date("Y-m-d");
  269.  
  270. $czas = date("H:i");
  271.  
  272. $ratio = $donated - $received;
  273.  
  274.  
  275.  
  276. if ($id and $liga and $lvl and $tag and $nick and $ranga and $donated and $received and $puchary) {
  277.  
  278.  
  279.  
  280. $clanExist = $db->getClanByTag($tag);
  281.  
  282. if ($clanExist) {
  283.  
  284. $ins = $db->updateKlan();
  285.  
  286. if ($ins) {
  287.  
  288. echo "Zaktualizowano bazę danych<br>";
  289.  
  290. } else {
  291.  
  292. echo "Błąd nie udało się zaktualizować<br>";
  293.  
  294. }
  295.  
  296. } else {
  297.  
  298. $ins = $db->insertKlan();
  299.  
  300. if ($ins) {
  301.  
  302. echo "Zaktualizowano bazę danych<br>";
  303.  
  304. } else {
  305.  
  306. echo "Błąd nie udało się zaktualizować<br>";
  307.  
  308. }
  309.  
  310. }
  311.  
  312. }
  313.  
  314. }
  315.  
  316.  
  317. ?>
  318.  


Jak widać zrobiłem a strona dalej swoje... Dlaczego?questionmark.gif Ugh

Wkleiłem je, bo może wam coś one powiedzą te poprzednie fragmenty, ja wiem, że wszystko co moje działało poprawnie, tylko ten nieszczęsny kod na update bądź insert szedł się kochać. Jeżeli trzeba, to zaraz przywrócę poprzedni kod tej podstrony i dam link, abyście mogli sami zobaczyć, what ever, jestem początkujący, nie wiem czy coś to pomoże.
Go to the top of the page
+Quote Post
javafxdev
post 29.04.2019, 21:11:11
Post #10





Grupa: Zarejestrowani
Postów: 95
Pomógł: 7
Dołączył: 27.10.2015

Ostrzeżenie: (0%)
-----


źle wkleiłeś zobacz że w ostatnim moim poście funkcja updateKlan wygląda inaczej...
Go to the top of the page
+Quote Post
Nidan23
post 29.04.2019, 22:45:55
Post #11





Grupa: Zarejestrowani
Postów: 101
Pomógł: 2
Dołączył: 26.04.2019

Ostrzeżenie: (10%)
X----


Tak to powinno wyglądać?

  1. <?php
  2. ini_set('display_errors', 1);
  3.  
  4. ini_set('display_startup_errors', 1);
  5.  
  6.  
  7.  
  8.  
  9. $conf = array(
  10.  
  11. 'dbhost' => "*******",
  12.  
  13. 'username' => "******",
  14.  
  15. 'password' => "******",
  16.  
  17. 'dbname' => "nidan_klan",
  18.  
  19. );
  20.  
  21.  
  22.  
  23. class DB
  24.  
  25. {
  26.  
  27.  
  28.  
  29. private $dbhost;
  30.  
  31. private $username;
  32.  
  33. private $password;
  34.  
  35. private $dbname;
  36.  
  37. private $conn;
  38.  
  39.  
  40.  
  41. public function __construct($conf)
  42.  
  43. {
  44.  
  45. $this->dbhost = $conf['dbhost'];
  46.  
  47. $this->username = $conf['username'];
  48.  
  49. $this->password = $conf['password'];
  50.  
  51. $this->dbname = $conf['dbname'];
  52.  
  53. }
  54.  
  55.  
  56.  
  57. private function init()
  58.  
  59. {
  60.  
  61. $this->conn = new mysqli($this->dbhost, $this->username, $this->password, $this->dbname);
  62.  
  63. if ($this->conn->connect_error) {
  64.  
  65. die("Connection failed: " . $this->conn->connect_error);
  66.  
  67. }
  68.  
  69. $this->conn->query("SET NAMES 'utf8'");
  70.  
  71. }
  72.  
  73.  
  74.  
  75. private function close()
  76.  
  77. {
  78.  
  79. $this->conn->close();
  80.  
  81. }
  82.  
  83.  
  84. function getClanByTag($tag)
  85.  
  86. {
  87.  
  88. $rows = array();
  89.  
  90. $this->init();
  91.  
  92. $result = $this->conn->query("SELECT * FROM klan WHERE tag = '$tag''");
  93.  
  94.  
  95.  
  96. if (false === $result) {
  97.  
  98. printf("error: %s\n", mysqli_error($this->conn));
  99.  
  100. }
  101.  
  102. if ($result->num_rows != 0) {
  103.  
  104. while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
  105.  
  106. $rows[] = $row;
  107.  
  108. }
  109.  
  110.  
  111.  
  112. }
  113.  
  114. $this->close();
  115.  
  116. return $rows;
  117.  
  118. }
  119.  
  120.  
  121.  
  122. public function insertKlan()
  123.  
  124. {
  125.  
  126. $rows = array();
  127.  
  128. $this->init();
  129.  
  130. $result = $this->conn->query("INSERT INTO klan SET miejsce='$id', liga='$liga', poziom='$lvl', tag='$tag', nick='$nick', ranga='$ranga', donated='$donated', received='$received', roznica='$ratio', puchary='$puchary', data='$datan', aktualizacja='$czas");
  131.  
  132.  
  133.  
  134. if (false === $result) {
  135.  
  136. printf("error: %s\n", mysqli_error($this->conn));
  137.  
  138. }
  139.  
  140. if ($result->num_rows != 0) {
  141.  
  142. while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
  143.  
  144. $rows[] = $row;
  145.  
  146. }
  147.  
  148.  
  149.  
  150. }
  151.  
  152. $this->close();
  153.  
  154. return $rows;
  155.  
  156. }
  157.  
  158.  
  159.  
  160. public function updateKlan()
  161.  
  162. {
  163.  
  164. $rows = array();
  165.  
  166. $this->init();
  167.  
  168. $result = $this->conn->query("UPDATE klan SET miejsce='$id', liga='$liga', poziom='$lvl', tag='$tag', nick='$nick', ranga='$ranga', donated='$donated', received='$received', roznica='$ratio', puchary='$puchary', data='$datan', aktualizacja='$czas");
  169.  
  170.  
  171.  
  172. if (false === $result) {
  173.  
  174. printf("error: %s\n", mysqli_error($this->conn));
  175.  
  176. }
  177.  
  178. if ($result->num_rows != 0) {
  179.  
  180. while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
  181.  
  182. $rows[] = $row;
  183.  
  184. }
  185.  
  186.  
  187.  
  188. }
  189.  
  190. $this->close();
  191.  
  192. return $rows;
  193.  
  194. }
  195.  
  196.  
  197.  
  198.  
  199.  
  200. }
  201.  
  202.  
  203.  
  204. $db = new DB($conf);
  205.  
  206.  
  207.  
  208. foreach ($members as $member) {
  209.  
  210. $id = $member["clanRank"];
  211.  
  212. $liga = $member["league"]["name"];
  213.  
  214. $lvl = $member["expLevel"];
  215.  
  216. $tag = $member["tag"];
  217.  
  218. $nick = $member["name"];
  219.  
  220. $ranga = $member["role"];
  221.  
  222. $donated = $member["donations"];
  223.  
  224. $received = $member["donationsReceived"];
  225.  
  226. $puchary = $member["trophies"];
  227.  
  228. $datan = date("Y-m-d");
  229.  
  230. $czas = date("H:i");
  231.  
  232. $ratio = $donated - $received;
  233.  
  234.  
  235.  
  236. if ($id and $liga and $lvl and $tag and $nick and $ranga and $donated and $received and $puchary) {
  237.  
  238.  
  239.  
  240. $clanExist = $db->getClanByTag($tag);
  241.  
  242. if ($clanExist) {
  243.  
  244. $ins = $db->updateKlan();
  245.  
  246. if ($ins) {
  247.  
  248. echo "Zaktualizowano bazę danych<br>";
  249.  
  250. } else {
  251.  
  252. echo "Błąd nie udało się zaktualizować<br>";
  253.  
  254. }
  255.  
  256. } else {
  257.  
  258. $ins = $db->insertKlan();
  259.  
  260. if ($ins) {
  261.  
  262. echo "Zaktualizowano bazę danych<br>";
  263.  
  264. } else {
  265.  
  266. echo "Błąd nie udało się zaktualizować<br>";
  267.  
  268. }
  269.  
  270. }
  271.  
  272. }
  273.  
  274. }
  275.  
  276.  
  277. ?>
  278.  
Powód edycji: [nospor]:
Go to the top of the page
+Quote Post
nospor
post 30.04.2019, 08:52:23
Post #12





Grupa: Moderatorzy
Postów: 36 447
Pomógł: 6292
Dołączył: 27.12.2004




Kurcze, chlopie, nie podawaj danych do bazy danych.... Twoj post moderuje. A nastepnym razem sam tego pilnuj.

ps: naprawde kazda linijke kodu musisz oddzielac linia pusta?


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
Nidan23
post 30.04.2019, 09:22:23
Post #13





Grupa: Zarejestrowani
Postów: 101
Pomógł: 2
Dołączył: 26.04.2019

Ostrzeżenie: (10%)
X----


Fakt, mogłem więcej danych ukryć, nie tylko hasło, ale mam kilka baz, więc to nie jest problem. Staram się oddzielać jedną linią wolnego, żeby także na telefonie kod był czytelny...
Go to the top of the page
+Quote Post
javafxdev
post 30.04.2019, 16:56:27
Post #14





Grupa: Zarejestrowani
Postów: 95
Pomógł: 7
Dołączył: 27.10.2015

Ostrzeżenie: (0%)
-----


ogólnie to chyba bez sensu zrobić '*****' na hało w ostatnim poście jak w każdym innym nadal są te dane widoczne?

a jaki teraz błąd dostajesz przy wywołaniu, czy nadal pusta strona?
Go to the top of the page
+Quote Post
Nidan23
post 2.05.2019, 01:47:24
Post #15





Grupa: Zarejestrowani
Postów: 101
Pomógł: 2
Dołączył: 26.04.2019

Ostrzeżenie: (10%)
X----


Dostawałem błąd 500, strona nie mogła obsłużyć żądania, więc gdzieś mogłeś zrobić literówkę. Tylko problem jak się okazało nie leży w moim skrypcie. Użyłem funkcji "REPLACE" i generalnie śmiga, ale urywa połowę rekordów, więc przywróciłem ten skrypt, który jest tematem tematu(?) i działał dokładnie tak samo, ale robił rekord o miejscu 2 (1 w bazie) x40 a także urywał większość rekordów. Więc reasumując nie tu leży błąd, tylko może w ich bazie danych czy coś, jednak dla pewności wrzucę tu ten fragment kodu.

  1. <?php
  2. foreach ($members as $member) {
  3.  
  4. // odbieramy dane z formularza
  5. $id = $member["clanRank"];
  6. $liga = $member["league"]["name"];
  7. $lvl = $member["expLevel"];
  8. $tag = $member["tag"];
  9. $nick = $member["name"];
  10. $ranga = $member["role"];
  11. $donated = $member["donations"];
  12. $received = $member["donationsReceived"];
  13. $puchary = $member["trophies"];
  14. $datan = date("Y-m-d");
  15. $czas = date("H:i");
  16. $ratio = $donated - $received;
  17.  
  18. if($id and $liga and $lvl and $tag and $nick and $ranga and $donated and $received and $puchary) {
  19.  
  20. // łączymy się z bazą danych
  21. $connection = @mysql_connect($servername, $username, $password)
  22. or die('Brak połączenia z serwerem MySQL');
  23. $db = @mysql_select_db($dbname, $connection)
  24. or die('Nie mogę połączyć się z bazą danych');
  25.  
  26. $ins = @mysql_query("REPLACE klan SET miejsce='$id', liga='$liga', poziom='$lvl', tag='$tag', nick='$nick', ranga='$ranga', donated='$donated', received='$received', roznica='$ratio', puchary='$puchary', data='$datan', aktualizacja='$czas'");
  27.  
  28. }
  29. }
  30. if($ins) echo "Zaktualizowano bazę danych<br>";
  31. else echo "Błąd nie udało się zaktualizować<br>";
  32.  
  33.  
  34.  
  35. ?>


To jest wszystko. Zmienne których tu używam są zdefiniowane poprawnie (może jednak pętla jest źle zrobiona? Powyżej jak mówiłem masz adres skąd biorę dane. Chociaż tej samej pętli używam do wyświetlenia danych i wszystko jest cacy...), bo wprowadza jakiekolwiek rekordy + wyświetla na stronie wszystko z ich serwerów. Te fragmenty kodu co dałem powyżej się nie zmieniły, tylko ten do SQL'a.


Jakieś pomysły?

EDIT. Jednak nie podmienia rekordów tylko wprowadza nowe.

Dorzucam poniżej schemat bazy, naprawdę już nie mam pojęcia...

  1. CREATE TABLE `klan` (
  2. `miejsce` int(11) DEFAULT NULL,
  3. `liga` varchar(25) DEFAULT NULL,
  4. `poziom` int(11) DEFAULT NULL,
  5. `tag` varchar(10) DEFAULT NULL,
  6. `nick` varchar(25) DEFAULT NULL,
  7. `ranga` varchar(10) DEFAULT NULL,
  8. `donated` int(11) DEFAULT NULL,
  9. `received` int(11) DEFAULT NULL,
  10. `roznica` int(11) DEFAULT NULL,
  11. `puchary` int(11) DEFAULT NULL,
  12. `aktualizacja` varchar(100) DEFAULT NULL,
  13. `data` varchar(100) DEFAULT NULL


Jedyny błąd jaki dostaję po wyświetleniu błędów to to:

"Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in /var/www/html/klan.php on line 333"

Ale i tak ten parametr wprowadza

EDIT2. Strona z Twoim skryptem działa, ale nie robi kompletnie nic.
Go to the top of the page
+Quote Post
javafxdev
post 2.05.2019, 18:40:15
Post #16





Grupa: Zarejestrowani
Postów: 95
Pomógł: 7
Dołączył: 27.10.2015

Ostrzeżenie: (0%)
-----


spróbuj tak:

bazadanych:
  1. CREATE TABLE `klan` (
  2. `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  3. `miejsce` INT(11) DEFAULT NULL,
  4. `liga` VARCHAR(25) DEFAULT NULL,
  5. `poziom` INT(11) DEFAULT NULL,
  6. `tag` VARCHAR(10) DEFAULT NULL,
  7. `nick` VARCHAR(25) DEFAULT NULL,
  8. `ranga` VARCHAR(10) DEFAULT NULL,
  9. `donated` INT(11) DEFAULT NULL,
  10. `received` INT(11) DEFAULT NULL,
  11. `roznica` INT(11) DEFAULT NULL,
  12. `puchary` INT(11) DEFAULT NULL,
  13. `aktualizacja` VARCHAR(100) DEFAULT NULL,
  14. `data` VARCHAR(100) DEFAULT NULL
  15. );


Kod php:
  1. <?php
  2. ini_set('display_errors', 1);
  3. ini_set('display_startup_errors', 1);
  4.  
  5. $conf = array(
  6. 'dbhost' => "localhost",
  7. 'username' => "root",
  8. 'password' => "",
  9. 'dbname' => "test",
  10. );
  11.  
  12. class DB
  13. {
  14.  
  15. private $dbhost;
  16. private $username;
  17. private $password;
  18. private $dbname;
  19. private $conn;
  20.  
  21. public function __construct($conf)
  22. {
  23. $this->dbhost = $conf['dbhost'];
  24. $this->username = $conf['username'];
  25. $this->password = $conf['password'];
  26. $this->dbname = $conf['dbname'];
  27. }
  28.  
  29. private function init()
  30. {
  31. $this->conn = new mysqli($this->dbhost, $this->username, $this->password, $this->dbname);
  32. if ($this->conn->connect_error) {
  33. die("Connection failed: " . $this->conn->connect_error);
  34. }
  35. $this->conn->query("SET NAMES 'utf8'");
  36. }
  37.  
  38. private function close()
  39. {
  40. $this->conn->close();
  41. }
  42.  
  43.  
  44. function getClanByTag($tag)
  45. {
  46. $rows = array();
  47. $this->init();
  48. $result = $this->conn->query("SELECT * FROM klan WHERE tag = '$tag'");
  49.  
  50. if (false === $result) {
  51. printf("error: %s\n", mysqli_error($this->conn));
  52. }
  53. if ($result->num_rows != 0) {
  54. while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
  55. $rows[] = $row;
  56. }
  57.  
  58. }
  59. $this->close();
  60. return $rows;
  61. }
  62.  
  63. public function insertKlan($miejsce, $liga, $poziom, $tag, $nick, $ranga, $donated, $received, $roznica, $puchary, $aktualizacja, $data)
  64. {
  65. $this->init();
  66. $query = "INSERT INTO klan (miejsce, liga, poziom, tag, nick , ranga, donated, received, roznica, puchary, aktualizacja, data) values ('$miejsce' ,'$liga' ,'$poziom' ,'$tag' ,'$nick ' ,'$ranga' ,'$donated' ,'$received' ,'$roznica' ,'$puchary' ,'$aktualizacja' ,'$data')";
  67. $result = $this->conn->query($query);
  68.  
  69. if (false === $result) {
  70. printf("error: %s\n", mysqli_error($this->conn));
  71. }
  72. $last_insert_id = mysqli_insert_id($this->conn);
  73. $this->close();
  74. return $last_insert_id;
  75. }
  76.  
  77. public function updateKlan($miejsce, $liga, $poziom, $tag, $nick, $ranga, $donated, $received, $roznica, $puchary, $aktualizacja, $data)
  78. {
  79. $this->init();
  80. $query = "UPDATE klan set miejsce = '$miejsce' where tag = '$tag'";
  81. $result = $this->conn->query($query);
  82.  
  83. if (false === $result) {
  84. printf("error: %s\n", mysqli_error($this->conn));
  85. }
  86. $this->close();
  87. return true;
  88. }
  89.  
  90.  
  91. }
  92.  
  93. $m1['clanRank'] = 'a';
  94. $m1['expLevel'] = '12';
  95. $m1["league"]["name"] = 'aa';
  96. $m1["expLevel"] = 'aa';
  97. $m1["tag"] = 'aa';
  98. $m1["name"] = 'aa';
  99. $m1["role"] = 'aa';
  100. $m1["donations"] = 'aa';
  101. $m1["donationsReceived"] = 'aa';
  102. $m1["trophies"] = 'aa';
  103. $members = array($m1);
  104.  
  105. $db = new DB($conf);
  106.  
  107. foreach ($members as $member) {
  108. // odbieramy dane z formularza
  109. $id = $member["clanRank"];
  110. $liga = $member["league"]["name"];
  111. $lvl = $member["expLevel"];
  112. $tag = $member["tag"];
  113. $nick = $member["name"];
  114. $ranga = $member["role"];
  115. $donated = $member["donations"];
  116. $received = $member["donationsReceived"];
  117. $puchary = $member["trophies"];
  118. $datan = date("Y-m-d");
  119. $czas = date("H:i");
  120. $ratio = $donated - $received;
  121.  
  122. if ($id and $liga and $lvl and $tag and $nick and $ranga and $donated and $received and $puchary) {
  123.  
  124. $clanExist = $db->getClanByTag($tag);
  125. if ($clanExist) {
  126. $ins = $db->updateKlan("34", $liga, $lvl, $tag, $nick, $ranga, $donated, $received, "", $puchary, "", "");
  127. if ($ins) {
  128. echo "Zaktualizowano bazę danych<br>";
  129. } else {
  130. echo "Błąd nie udało się zaktualizować<br>";
  131. }
  132. } else {
  133. $ins = $db->insertKlan("miejsce", $liga, $lvl, $tag, $nick, $ranga, $donated, $received, "", $puchary, "", "");
  134. if ($ins) {
  135. echo "Zaktualizowano bazę danych<br>";
  136. } else {
  137. echo "Błąd nie udało się zaktualizować<br>";
  138. }
  139. }
  140. }
  141. }
  142. ?>


I daj znać czy działa
Go to the top of the page
+Quote Post
Nidan23
post 3.05.2019, 11:08:37
Post #17





Grupa: Zarejestrowani
Postów: 101
Pomógł: 2
Dołączył: 26.04.2019

Ostrzeżenie: (10%)
X----


Dostaję mój ulubiony biały ekran z "HTTP Error 500"...

Coraz bardziej podejrzewam, że to błąd leży po stronie serwera gry aniżeli mojej, bo przetestowałem tyle skryptów i nic...
Go to the top of the page
+Quote Post
viking
post 3.05.2019, 11:53:43
Post #18





Grupa: Zarejestrowani
Postów: 6 365
Pomógł: 1114
Dołączył: 30.08.2006

Ostrzeżenie: (0%)
-----


500 zawiera informacje o błędzie np w logach. Nie ma co strzelać tylko trzeba przeczytać ten błąd.


--------------------
Go to the top of the page
+Quote Post
Nidan23
post 3.05.2019, 16:04:34
Post #19





Grupa: Zarejestrowani
Postów: 101
Pomógł: 2
Dołączył: 26.04.2019

Ostrzeżenie: (10%)
X----


Dzięki za info, poszukam, ale ten błąd jest tylko z tym skryptem. Z moim śmiga, ale jak pisałem niedawno ucina mi ponad połowę rekordów. Myślisz, że wyjaśnienie też mogę znaleźć w logach?


EDIT. W logach nie ma odpowiedzi na pytanie dlaczego urywa kod, ani mariadblog ani w errorlogu httpd ani access logu.

Cytat(javafxdev @ 2.05.2019, 19:40:15 ) *
spróbuj tak:

I daj znać czy działa



Dostaję taki zwrot:

Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the
date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier.
We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in /var/www/html/klan.php on line 406



Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case
you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set
date.timezone to select your timezone. in /var/www/html/klan.php on line 407


Notice: Undefined variable: db in /var/www/html/klan.php on line 412


Fatal error: Call to a member function getClanByTag() on a non-object in /var/www/html/klan.php on line 412


Gdy poprawiłem błąd, skrypt działa, ale nadal wprowadza 22 rekordy z chwili obecnej (zmienia się) 38.

Ten post edytował Nidan23 3.05.2019, 15:56:50
Go to the top of the page
+Quote Post
viking
post 3.05.2019, 16:09:40
Post #20





Grupa: Zarejestrowani
Postów: 6 365
Pomógł: 1114
Dołączył: 30.08.2006

Ostrzeżenie: (0%)
-----


Kluczowe są dwa ostatnie. Strzelam że nawet połączenia z bazą nie masz. Usuń wszystkie @ a jeżeli masz php7 to funkcji mysql_ już nie ma.


--------------------
Go to the top of the page
+Quote Post

2 Stron V   1 2 >
Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 19.04.2024 - 19:34