Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Prosty system niusów oparty o mySQL.
Forum PHP.pl > Forum > PHP
Mazur_pl
Witam, jestem nowy na forum oraz początkującym programistą. guitar.gif
Napisałem prosty system niusów:
  1. <?php
  2.  
  3. DEFINE ('DB_USER', 'Mazur');
  4. DEFINE ('DB_PASSWORD', '###');
  5. DEFINE ('DB_HOST', 'localhost');
  6. DEFINE ('DB_NAME', 'data');
  7.  
  8. $dbc = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD);
  9. mysql_select_db (DB_NAME);
  10.  
  11. if (isset($_POST['submit'])) {
  12.  
  13. $text = $_POST['news_text'];
  14. $author = $_POST['news_author'];
  15. $topic = $_POST['news_topic'];
  16. $topic_mini = $_POST['news_topic_mini'];
  17.  
  18. if ($author && $text) {
  19.  
  20. $query = "INSERT INTO news (news_text, news_author, news_topic, news_topic_mini)
  21. VALUES
  22. ('$text', '$author', '$topic', '$topic_mini')";
  23.  
  24. $result = mysql_query($query);
  25.  
  26. if ($result) {
  27. echo 'News dodany!';
  28. } else {
  29. }
  30. }
  31. }
  32.  
  33.  
  34. $query = "SELECT * FROM news ORDER BY news_id DESC";
  35.  
  36. $result = mysql_query($query);
  37.  
  38. if ($result) {
  39. while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
  40. echo '<table width="900" align="center" border="1" bordercolor="black" style="border-collapse: collapse;">'."rn";
  41. echo ' <tr>'."rn";
  42. echo '  <td>Tytul: '.$row['news_topic'].'</td>'."rn";
  43. echo ' </tr>'."rn";
  44. echo ' <tr>'."rn";
  45. echo '  <td>Opis: '.$row['news_topic_mini'].'</td>'."rn";
  46. echo ' </tr>'."rn";
  47. echo ' <tr>'."rn";
  48. echo '  <td>Autor: '.$row['news_author'].'</td>'."rn";
  49. echo ' </tr>'."rn";
  50. echo ' <tr>'."rn";
  51. echo '  <td>'.$row['news_text'].'</td>'."rn";
  52. echo ' </tr>'."rn";
  53. echo '</table><br />'."rn";
  54. }
  55. mysql_free_result ($result);
  56. }
  57. ?>
  58. <br />
  59. <br />
  60. <br />
  61. <br />
  62. <form action="ee.php" method="post">
  63. Topic:<br />
  64. <input type="text" style="width:200px" size="30" maxleght="100" name="news_topic" /><br />
  65. Topic opis:<br />
  66. <input type="text" style="width:200px" size="30" maxleght="100" name="news_topic_mini" /><br />
  67. Nick:<br />
  68. <input type="text" style="width:200px" size="30" maxleght="100" name="news_author" /><br />
  69. Text:<br />
  70. <textarea class="post" name="news_text" maxlength="255" cols="50" rows="10">Text.......</textarea><br />
  71. <input type="submit" name="submit" value="Wyslij" />
  72. </form>

Wszystko , pięknie chodzi . Lecz mam pytanie. Jak zrobić aby po wpisaniu index.php?id=(na przykład 1)
wyskakiwała news którego id jest 1 ?
Proszę o pomoc smile.gif .
Darti
No takie zapytanie do bazy robisz:
  1. SELECT * FROM news WHERE news_id=1;
Mazur_pl
Dzięki, już wiem jakie id ma dany news. Na przykład mam news_id = 11 więc teraz co mam zrobić?

Już sobie poradziłem ;] .
Mam pytanie zrobiłem to tak:
  1. <?php
  2.  
  3. DEFINE ('DB_USER', 'Mazur');
  4. DEFINE ('DB_PASSWORD', '###');
  5. DEFINE ('DB_HOST', 'localhost');
  6. DEFINE ('DB_NAME', 'data');
  7.  
  8. $dbc = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD);
  9. mysql_select_db (DB_NAME);
  10.  
  11. if($_GET['id'] == "") {
  12.  
  13. if (isset($_POST['submit'])) {
  14.  
  15. $text = $_POST['news_text'];
  16. $author = $_POST['news_author'];
  17. $topic = $_POST['news_topic'];
  18. $topic_mini = $_POST['news_topic_mini'];
  19.  
  20. if ($author && $text) {
  21.  
  22. $query = "INSERT INTO news (news_text, news_author, news_topic, news_topic_mini)
  23. VALUES
  24. ('$text', '$author', '$topic', '$topic_mini')";
  25.  
  26. $result = mysql_query($query);
  27.  
  28. if ($result) {
  29. echo 'News dodany!';
  30. } else {
  31. }
  32. }
  33. }
  34.  
  35.  
  36. $query = "SELECT * FROM news ORDER BY news_id DESC";
  37.  
  38. $result = mysql_query($query);
  39.  
  40. if ($result) {
  41. while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
  42. echo "rn".'<table width="900" align="center" border="1" bordercolor="black" style="border-collapse: collapse;">'."rn";
  43. echo ' <tr>'."rn";
  44. echo '  <td>Tytul: '.$row['news_topic'].'</td>'."rn";
  45. echo ' </tr>'."rn";
  46. echo ' <tr>'."rn";
  47. echo '  <td>Opis: '.$row['news_topic_mini'].'</td>'."rn";
  48. echo ' </tr>'."rn";
  49. echo ' <tr>'."rn";
  50. echo '  <td>Autor: '.$row['news_author'].'</td>'."rn";
  51. echo ' </tr>'."rn";
  52. echo ' <tr>'."rn";
  53. echo '  <td>'.$row['news_text'].'</td>'."rn";
  54. echo ' </tr>'."rn";
  55. echo '</table><br />'."rn";
  56. }
  57. mysql_free_result ($result);
  58. }
  59. ?>
  60. <br />
  61. <br />
  62. <br />
  63. <br />
  64. <form action="ee.php" method="post">
  65. Topic:<br />
  66. <input type="text" style="width:200px" size="30" maxleght="100" name="news_topic" /><br />
  67. Topic opis:<br />
  68. <input type="text" style="width:200px" size="30" maxleght="100" name="news_topic_mini" /><br />
  69. Nick:<br />
  70. <input type="text" style="width:200px" size="30" maxleght="100" name="news_author" /><br />
  71. Text:<br />
  72. <textarea class="post" name="news_text" maxlength="255" cols="50" rows="10">Text.......</textarea><br />
  73. <input type="submit" name="submit" value="Wyslij" />
  74. </form>
  75.  
  76. <?php
  77. } elseif($_GET['id'] == "11") {
  78.  
  79. $query = "SELECT * FROM news WHERE news_id = 11";
  80. $result = mysql_query($query);
  81.  
  82. if ($result) {
  83. while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
  84. echo "rn".'<table width="900" align="center" border="1" bordercolor="black" style="border-collapse: collapse;">'."rn";
  85. echo ' <tr>'."rn";
  86. echo '  <td>Tytul: '.$row['news_topic'].'</td>'."rn";
  87. echo ' </tr>'."rn";
  88. echo ' <tr>'."rn";
  89. echo '  <td>Opis: '.$row['news_topic_mini'].'</td>'."rn";
  90. echo ' </tr>'."rn";
  91. echo ' <tr>'."rn";
  92. echo '  <td>Autor: '.$row['news_author'].'</td>'."rn";
  93. echo ' </tr>'."rn";
  94. echo ' <tr>'."rn";
  95. echo '  <td>'.$row['news_text'].'</td>'."rn";
  96. echo ' </tr>'."rn";
  97. echo '</table><br />'."rn";
  98. }
  99. }
  100. } elseif($_GET['id'] == "12") {
  101.  
  102. $query = "SELECT * FROM news WHERE news_id = 12";
  103. $result = mysql_query($query);
  104.  
  105. if ($result) {
  106. while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
  107. echo "rn".'<table width="900" align="center" border="1" bordercolor="black" style="border-collapse: collapse;">'."rn";
  108. echo ' <tr>'."rn";
  109. echo '  <td>Tytul: '.$row['news_topic'].'</td>'."rn";
  110. echo ' </tr>'."rn";
  111. echo ' <tr>'."rn";
  112. echo '  <td>Opis: '.$row['news_topic_mini'].'</td>'."rn";
  113. echo ' </tr>'."rn";
  114. echo ' <tr>'."rn";
  115. echo '  <td>Autor: '.$row['news_author'].'</td>'."rn";
  116. echo ' </tr>'."rn";
  117. echo ' <tr>'."rn";
  118. echo '  <td>'.$row['news_text'].'</td>'."rn";
  119. echo ' </tr>'."rn";
  120. echo '</table><br />'."rn";
  121. }
  122. }
  123. }
  124. ?>

Czy można to zrobić jakoś tak aby bez ciągłego elseif tylko na przykład:
  1. <?php
  2. if($_GET['id'] == "id ktory wpiszemy") {
  3. }
  4. ?>

?

Zrobiłem coś takiego ale nie chodzi sad.gif .
  1. <?php
  2. $NewsID = (int)$_GET['id_news'];
  3.  
  4. $Query= "SELECT * FROM news WHERE news_id = ".$NewsID;
  5.  
  6. $QueryResult = mysql_query($Query);
  7. ?>
cinekz
[ot]
@Mazur_pl: Jak na twój jeden z pierwszych skryptów ten jest całkiem nieźle napisany.
Na przyszłość pisz DEFINE z małej litery[/ot]
Tak nawiązując do tematu, jaki błąd otrzymujesz? Czy to błąd parsera php, czy serwera MySQL?
Pozdrawiam.
Szpiegu
Nie wiem czy dobrze zrozumiałem o co Ci chodzi ale może spróbuj czegoś takiego:

  1. <?php
  2. if(!isset($_GET['id']) || !intval($_GET['id']))
  3. {
  4. ...
  5. }
  6.  
  7. ...
  8.  
  9. if(isset($_GET['id']) && intval($_GET['id']))
  10. {
  11.  $NewsId = intval($_GET['id']);
  12. // i tu wyswietlasz konkretnego newsa
  13. }
  14. ?>
Mazur_pl
Dzięki ;]. Wszystko już chodzi.
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.