Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [MySQL][PHP]Problem z edycją danych w bazie mysql z poziomu phpa
Forum PHP.pl > Forum > Przedszkole
Dejna
Jestem początkującym "programistą" php. Pisze system newsów opartym na mysqlu. Jak dotąd nie było problemu z Dodawaniem treści i jej usuwaniem, ale nie mogę dojść dlaczego nie działa w moim skrypcie edycja recordu. Działa mi odczytanie danych do formularza ale po wysłaniu jego zmienionej zawartości nic sie nie dzieje ani nie ma błedu phpa ani nie aktualizują się rekordy w mysqlu. Pewnie to jest jakaś błachostka ale już kompletnie nie wiem gdzie szukać problemu.

Problem jest z ostatnią pętlą która nie chce działać chociaż wartości zmiennych są poprawnie wysyłane przez formularz "ndex.php?sk=managenews&id=21&a=edit&b=save"

Proszę o pomoc i jakieś wskazówki jak to naprawić

  1. <html>
  2. <title>System newsóws</title>
  3. <head>
  4. <link href="style.css" rel="stylesheet" type="text/css" />
  5. <meta http-equiv="content-type" content="text/html; charset=iso-8859-2">
  6. </head>
  7. <body>
  8. <div style='margin-left:50px;margin-right:50px;margin-top:10px;margin-bottom:10px'>
  9.  
  10. <?php
  11. $host = "localhost";
  12. $user = "admin";
  13. $pass = "zaq1@WSX";
  14. $conn = mysql_connect($host, $user, $pass);
  15. $db = "news";
  16. @mysql_connect($host, $user, $pass) or di ("Błąd połączenia z bazą MYSQL");
  17. @mysql_select_db($db) or die("Błąd wybierania bazy danych");
  18. echo "<div style='float:right'>";
  19. echo "<div style='border:1px solid silver;background:#ffffcc;'>";
  20. echo "<div style='margin-left:5px;margin-right:5px;margin-top:3px;margin-bottom:3px;'>";
  21. echo "<div style='border: 1px solid silver;background:#cccccc'>";
  22. echo "<div style='margin-left:5px;margin-right:5px;margin-top:1px;margin-bottom:2px;'>";
  23. echo "<b>Panel</b>";
  24. echo "</div>";
  25. echo "</div>";
  26.  
  27. echo "<div style='margin-top:5px;'></div>";
  28. echo "<li><a href='index.php'>Strona Główna</a></li>";
  29. echo "<li><a href='index.php?sk=addnews'>Dodawanie newsa</a></li>";
  30. echo "<li><a href='index.php?sk=managenews'>Zarządzanie newsami</a></li>";
  31. echo "</div>";
  32. echo "</div>";
  33. echo "</div>";
  34. echo "</div>";
  35. echo "<div style='float:left;width:50%;'>";
  36. if (!isset($_GET['sk'])){
  37. echo "<div style='border:1px solid silver;background:#ffffcc;'>";
  38. echo "<div style='margin-left:5px;margin-right:5px;margin-top:3px;margin-bottom:3px;'>";
  39. echo "<div style='border: 1px solid silver;background:#cccccc'>";
  40. echo "<div style='margin-left:5px;margin-right:5px;margin-top:1px;margin-bottom:2px;'>";
  41. echo "<b>Newsy</b>";
  42. echo "</div>";
  43. echo "</div>";
  44. echo "<div style='margin-top:5px;'></div>";
  45.  
  46. $newsQuery = mysql_query("select * from news order by id desc") or die("Bład zapytania");
  47. if (mysql_num_rows($newsQuery)>0){
  48. while ( $n = mysql_fetch_assoc($newsQuery)){
  49. echo "<table border='0' width='100%' cellpadding='1' cellspacing='2' style='font-size='13px''>";
  50. echo "<tr>";
  51. echo "<td style='width:20%;'> <a href='index.php?sk=shownews&id=".$n['id']."'> ".$n['namenews']."</a> </td>";
  52. echo "<td style='width:20%;'> <a href=''> ".$n['date']."</a> </td>";
  53. echo "</tr>";
  54.  
  55. echo "</table>";}
  56. }
  57. if (mysql_num_rows($newsQuery) <= 0){
  58. echo "Brak newsów";
  59. }
  60.  
  61. echo "</div>";
  62. echo "</div>";
  63.  
  64. }
  65.  
  66. else if ($_GET['sk']=='shownews'){
  67. $id = $_GET['id'];
  68. echo "<div style='border:1px solid silver;background:#ffffcc;'>";
  69. echo "<div style='margin-left:5px;margin-right:5px;margin-top:3px;margin-bottom:3px;'>";
  70. echo "<div style='border: 1px solid silver;background:#cccccc'>";
  71. echo "<div style='margin-left:5px;margin-right:5px;margin-top:1px;margin-bottom:2px;'>";
  72. echo "<b>Przeglądanie newsa</b>";
  73. echo "</div>";
  74. echo "</div>";
  75. echo "<div style='margin-top:5px;'></div>";
  76.  
  77. $newsQuery = mysql_query("select * from news where id='$id'") or die("Bład zapytania");
  78. if (mysql_num_rows($newsQuery)>0){
  79. while ( $n = mysql_fetch_assoc($newsQuery)){
  80. echo "<table border='0' width='100%' cellpadding='1' cellspacing='2' style='font-size='13px''>";
  81. echo "<tr>";
  82. echo "<td style='width:20%;'> <a href='index.php?sk=shownews&id=".$n['id']."'> ".$n['namenews']."</a> </td>";
  83. echo "<td style='width:20%;'> <a href=''> ".$n['date']."</a> </td>";
  84. echo "</tr>";
  85. echo "</table>";
  86.  
  87. echo "<table border='0' width='100%' cellpadding='1' cellspacing='2' style='font-size='13px''>";
  88. echo "<tr>";;
  89. echo "<td>".$n['content']."</td>";
  90. echo "</tr>";
  91.  
  92. echo "</table>";}
  93. }
  94. }
  95. else if($_GET['sk']=='addnews'){
  96.  
  97. echo "<div style='border:1px solid silver;background:#ffffcc;'>";
  98. echo "<div style='margin-left:5px;margin-right:5px;margin-top:3px;margin-bottom:3px;'>";
  99. echo "<div style='border: 1px solid silver;background:#cccccc'>";
  100. echo "<div style='margin-left:5px;margin-right:5px;margin-top:1px;margin-bottom:2px;'>";
  101. echo "<b>Dodawania news</b>";
  102. echo "</div>";
  103. echo "</div>";
  104. echo "<div style='margin-top:5px;'></div>";
  105.  
  106. if (!isset($_GET['a'])){
  107.  
  108. echo "<form action='index.php?sk=addnews&a=insert' method='post'>";
  109. echo "<table border='0' width='100%' cellpadding='1' cellspacing='2' style='font-size='13px''>";
  110. echo "<tr>";
  111. echo "<td>Tytuł</td>";
  112. echo "<td><input type='text' name='namenews' id='namenews' style='border:1px solid silver;width:245px;'/></td>";
  113. echo "</tr>";
  114. echo "<tr>";
  115. echo "<td>Treść</td>";
  116. echo "<td> <textarea rows='5' cols='30' name='content' id='content' style='border:1px solid silver;' > </textarea></td>";
  117. echo "</tr>";
  118. echo "<td><input type='submit' style='border:1px solid silver;' value='Dodaj' </td>";
  119. echo "</tr>";
  120. echo "</tr>";
  121.  
  122. echo "</table>";
  123. echo "</form>";
  124.  
  125. }
  126.  
  127. if(isset($_GET['a'])=='insert'){
  128. $namenews= $_POST['namenews'];
  129. $content= nl2br($_POST['content']);
  130. $date = date("d.m.Y");
  131. mysql_query("insert into news set namenews='$namenews' , content='$content' , date='$date'") or die("Błąd zapytania");
  132. echo "News został dodany";
  133. }
  134.  
  135. echo "<div></div>";
  136. }
  137. else if ($_GET['sk']=='managenews'){
  138. echo "<div style='border:1px solid silver;background:#ffffcc;'>";
  139. echo "<div style='margin-left:5px;margin-right:5px;margin-top:3px;margin-bottom:3px;'>";
  140. echo "<div style='border: 1px solid silver;background:#cccccc'>";
  141. echo "<div style='margin-left:5px;margin-right:5px;margin-top:1px;margin-bottom:2px;'>";
  142. echo "<b>Zarządzanie news</b>";
  143. echo "</div>";
  144. echo "</div>";
  145. echo "<div style='margin-top:5px;'></div>";
  146. if(!isset($_GET['a'])){
  147. $newsQuery = mysql_query("select * from news order by id desc") or die("Bład zapytania");
  148. if (mysql_num_rows($newsQuery)>0){
  149. while ( $n = mysql_fetch_assoc($newsQuery)){
  150. echo "<table border='0' width='100%' cellpadding='1' cellspacing='2' style='font-size='13px''>";
  151. echo "<tr>";
  152. echo "<td style='width:20%;'> [<a href='index.php?sk=managenews&id=".$n['id']."&a=del'>DEL</a>] [<a href='index.php?sk=managenews&id=".$n['id']."&a=edit'>EDIT</a>] </td>";
  153. echo "<td style='width:20%;'> <a href='index.php?sk=shownews&id=".$n['id']."'> ".$n['namenews']."</a> </td>";
  154.  
  155. echo "</tr>";
  156.  
  157. echo "</table>";}
  158. }
  159. if (mysql_num_rows($newsQuery) <= 0){
  160. echo "Brak newsów";
  161. }}
  162. else if ($_GET['a']=='del'){
  163. $id = $_GET['id'];
  164. mysql_query("delete from news where id='$id'") or die("Błąd zapytania");
  165. echo "News został usunięty";
  166. }
  167.  
  168. else if ($_GET['a']=='edit'){
  169.  
  170. if (!isset($_GET['b'])){
  171. $id = $_GET['id'];
  172. $newsQuery = mysql_query("select * from news where id='$id'") or die("Błąd zapytania");
  173.  
  174. if(mysql_num_rows($newsQuery)>0){
  175. $id = $_GET['id'];
  176. $n = mysql_fetch_assoc($newsQuery);
  177. echo "<form action='index.php?sk=managenews&id=".$n['id']."&a=edit&b=save' method='post'>";
  178. echo "<table border='0' width='100%' cellpadding='1' cellspacing='2' style='font-size='13px''>";
  179. echo "<tr>";
  180. echo "<td>Tytuł</td>";
  181. echo "<td><input type='text' name='namenews' id='namenews' style='border:1px solid silver;width:245px;' value=".$n['namenews']."/></td>";
  182. echo "</tr>";
  183. echo "<tr>";
  184. echo "<td>Treść</td>";
  185. echo "<td> <textarea rows='5' cols='30' name='content' id='content' style='border:1px solid silver;' >".$n['content']." </textarea></td>";
  186. echo "</tr>";
  187. echo "<td><input type='submit' style='border:1px solid silver;' value='Zapisz' </td>";
  188. echo "</tr>";
  189. echo "</tr>";
  190.  
  191. echo "</table>";
  192. echo "</form>";
  193. echo mysql_ping($conn);
  194.  
  195. }
  196.  
  197. if ($_GET['b']=='save'){
  198. echo "<div style='border:1px solid silver;background:#ffffcc;'>";
  199. echo "<div style='margin-left:5px;margin-right:5px;margin-top:3px;margin-bottom:3px;'>";
  200. echo "<div style='border: 1px solid silver;background:#cccccc'>";
  201. echo "<div style='margin-left:5px;margin-right:5px;margin-top:1px;margin-bottom:2px;'>";
  202. echo "<b>Zarządzanie news</b>";
  203. echo "</div>";
  204. echo "</div>";
  205. echo "News został zaktualizowany";
  206.  
  207. }
  208.  
  209. }
  210.  
  211. }
  212. echo "<div></div>";
  213. }
  214.  
  215. ?>
  216. </div>
  217.  
  218.  
  219.  
  220. </div>
  221.  
  222.  
  223. </body>
  224. </html>
Wazniak96
Pytanie: Gdzie masz jakiekolwiek zapytanie mające zmienić daną wartość .? Po przesłaniu danych po edycji, nie zostaje wykonana żadna czynność w celu zmiany danych, a przynajmniej nie widzę thumbsdownsmileyanim.gif

PS: A co się stanie gdy dla zabawy ktoś zmieni sobie w linku np: a=sadsafasfsf ? smile.gif
Dejna
W formularzu masz actiona z wartością a błąd już znalazłem wiec temat do zamknięcia. Nawiaz był w złym miejscu
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.