login  Naam:   Wachtwoord: 
Registreer je!
 Forum

newsscript werkt niet! help!

Offline Fulcrum - 02/04/2008 20:25 (laatste wijziging 02/04/2008 21:57)
Avatar van FulcrumNieuw lid Hallo,

Kan iemand mij helpen met mn newsscript? De browsers geven een error 500 en ik kan de fout niet vinden.

Dit is het het newsscript (index.php):
  1. <?php
  2. /* user config variables */
  3. $max_items = 5; /* max number of news items to show */
  4.  
  5. /* make database connection */
  6. $db = mysql_connect ('localhost','*****','****');
  7. mysql_select_db ('test',$db);
  8.  
  9. function displayNews($all = 0) {
  10. /* bring in two variables
  11.   * $db is our database connection
  12.   * $max_items is the maximum number
  13.   * of news items we want to display */
  14. global $db, $max_items;
  15.  
  16. /* query for news items */
  17. if ($all == 0) {
  18. /* this query is for up to $max_items */
  19. $query = "SELECT id,title,newstext," .
  20. "DATE_FORMAT(postdate, '%Y-%m-%d') as date " .
  21. "FROM news ORDER BY postdate DESC LIMIT $max_items";
  22. } else {
  23. /* this query will get all news */
  24. $query = "SELECT id,title,newstext," .
  25. "DATE_FORMAT(postdate, '%Y-%m-%d') as date " .
  26. "FROM news ORDER BY postdate DESC";
  27. }
  28. $result = mysql_query ($query);
  29. while ($row = mysql_fetch_assoc ($result)) {
  30. /* display news in a simple table */
  31. echo "&lt;TABLE border=\"1\" width=\"300\"&gt;\n";
  32.  
  33. /* place table row data in
  34.   * easier to use variables.
  35.   * Here we also make sure no
  36.   * HTML tags, other than the
  37.   * ones we want are displayed */
  38. $date = $row['date'];
  39. $title = htmlentities ($row['title']);
  40. $news = nl2br (strip_tags ($row['newstext'], '&lt;a&gt;&lt;b&gt;&lt;i&gt;&lt;u&gt;'));
  41.  
  42. /* display the data */
  43. echo "&lt;TR&gt;&lt;TD&gt;&lt;b&gt;$title&lt;/b&gt; posted on $date&lt;/TD&gt;&lt;/TR&gt;\n";
  44. echo "&lt;TR&gt;&lt;TD&gt;$news&lt;/TD&gt;&lt;/TR&gt;\n";
  45.  
  46. /* get number of comments */
  47. $comment_query = "SELECT count(*) FROM news_comments " .
  48. "WHERE news_id={$row['id']}";
  49. $comment_result = mysql_query ($comment_query);
  50. $comment_row = mysql_fetch_row($comment_result);
  51.  
  52. /* display number of comments with link */
  53. echo "&lt;TR&gt;&lt;TD&gt;&lt;a href=\"{$_SERVER['PHP_SELF']}" .
  54. "?action=show&amp;id={$row['id']}\"&gt;Comments&lt;/a&gt;" .
  55. "($comment_row[0]}&lt;/TD&gt;&lt;/TR&gt;\n";
  56.  
  57. /* finish up table*/
  58. echo "&lt;/TABLE&gt;\n";
  59. echo "&lt;BR&gt;\n";
  60. }
  61.  
  62. /* if we aren't displaying all news,
  63.   * then give a link to do so */
  64. if ($all == 0) {
  65. echo "&lt;a href=\"{$_SERVER['PHP_SELF']}" .
  66. "?action=all\"&gt;View all news&lt;/a&gt;\n";
  67. }
  68. }
  69.  
  70. function displayOneItem($id) {
  71. global $db;
  72.  
  73. /* query for item */
  74. $query = "SELECT * FROM news WHERE id=$id";
  75. $result = mysql_query ($query);
  76.  
  77. /* if we get no results back, error out */
  78. if (mysql_num_rows ($result) == 0) {
  79. echo "Bad news id\n";
  80. return;
  81. }
  82. $row = mysql_fetch_assoc($result);
  83. echo "&lt;TABLE border=\"1\" width=\"300\"&gt;\n";
  84.  
  85. /* easier to read variables and
  86.   * striping out tags */
  87. $title = htmlentities ($row['title']);
  88. $news = nl2br (strip_tags ($row['newstext'], '&lt;a&gt;&lt;b&gt;&lt;i&gt;&lt;u&gt;'));
  89.  
  90. /* display the items */
  91. echo "&lt;TR&gt;&lt;TD&gt;&lt;b&gt;$title&lt;/b&gt;&lt;/TD&gt;&lt;/TR&gt;\n";
  92. echo "&lt;TR&gt;&lt;TD&gt;$news&lt;/TD&gt;&lt;/TR&gt;\n";
  93.  
  94. echo "&lt;/TABLE&gt;\n";
  95. echo "&lt;BR&gt;\n";
  96.  
  97. /* now show the comments */
  98. displayComments($id);
  99. }
  100.  
  101. function displayComments($id) {
  102. /* bring db connection variable into scope */
  103. global $db;
  104.  
  105. /* query for comments */
  106. $query = "SELECT * FROM news_comments WHERE news_id=$id";
  107. $result = mysql_query ($query);
  108. echo "Comments:&lt;BR&gt;&lt;HR width=\"300\"&gt;\n";
  109.  
  110. /* display the all the comments */
  111. while ($row = mysql_fetch_assoc ($result)) {
  112. echo "&lt;TABLE border=\"1\" width=\"300\"&gt;\n";
  113.  
  114. $name = htmlentities ($row['name']);
  115. echo "&lt;TR&gt;&lt;TD&gt;&lt;b&gt;by: $name&lt;/b&gt;&lt;/TD&gt;&lt;/TR&gt;\n";
  116.  
  117. $comment = strip_tags ($row['comment'], '&lt;a&gt;&lt;b&gt;&lt;i&gt;&lt;u&gt;');
  118. $comment = nl2br ($comment);
  119. echo "&lt;TR&gt;&lt;TD&gt;$comment&lt;/TD&gt;&lt;/TR&gt;\n";
  120.  
  121. echo "&lt;/TABLE&gt;\n";
  122. echo "&lt;BR&gt;\n";
  123. }
  124.  
  125. /* add a form where users can enter new comments */
  126. echo "&lt;HR width=\"300\"&gt;";
  127. echo "&lt;FORM action=\"{$_SERVER['PHP_SELF']}" .
  128. "?action=addcomment&amp;id=$id\" method=POST&gt;\n";
  129. echo "Name: &lt;input type=\"text\" " .
  130. "width=\"30\" name=\"name\"&gt;&lt;BR&gt;\n";
  131. echo "&lt;TEXTAREA cols=\"40\" rows=\"5\" " .
  132. "name=\"comment\"&gt;&lt;/TEXTAREA&gt;&lt;BR&gt;\n";
  133. echo "&lt;input type=\"submit\" name=\"submit\" " .
  134. "value=\"Add Comment\"\n";
  135. echo "&lt;/FORM&gt;\n";
  136.  
  137. }
  138.  
  139. function addComment($id) {
  140. global $db;
  141.  
  142. /* insert the comment */
  143. $query = "INSERT INTO news_comments " .
  144. "VALUES('',$id,'{$_POST['name']}'," .
  145. "'{$_POST['comment']}')";
  146. mysql_query($query);
  147.  
  148. echo "Comment entered. Thanks!&lt;BR&gt;\n";
  149. echo "&lt;a href=\"{$_SERVER['PHP_SELF']}" .
  150. "?action=show&amp;id=$id\"&gt;Back&lt;/a&gt;\n";
  151. }
  152.  
  153. /* this is where the script decides what do do */
  154.  
  155. echo "&lt;CENTER&gt;\n";
  156. switch($_GET['action']) {
  157.  
  158. case 'show':
  159. displayOneItem($_GET['id']);
  160. break;
  161. case 'all':
  162. displayNews(1);
  163. break;
  164. case 'addcomment':
  165. addComment($_GET['id']);
  166. break;
  167. default:
  168. displayNews();
  169. }
  170. echo "&lt;/CENTER&gt;\n";
  171. ?>

Alvast bedankt!!! 

1 antwoord

Gesponsorde links
Offline Ibrahim - 02/04/2008 21:40
Avatar van Ibrahim PHP expert Moet je wel melden dat het niet nodig is om je database variabele te globaliseren.
Gesponsorde links
Dit onderwerp is gesloten.
Actieve forumberichten
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.202s