开发者

MySQL/PHP News System

开发者 https://www.devze.com 2023-03-19 08:58 出处:网络
I have a PHP/MySQL News system which displayes the newest news article on the home page and a full list on a news page.

I have a PHP/MySQL News system which displayes the newest news article on the home page and a full list on a news page.

The newest article bit works but, my problem is that whenever i try to echo all the news article on the news page it either repeats the same or outputs one and nothing else.

**MySQL Information**

id INT AUTO_INCREMENT, 
author VARCHAR(xxx), 
title VARCHAR(xxx), 
message TEXT, 
date TEXT,
time TEXT,
PRIMARY KEY(id)

This is the insertion page (news_center.php)

<form action='/?module=admin&n=news_center_ac' method='post'>
<table align="center" width="68%">
    <tr>
          <td>Title</td>
          <td><input style="width:100%;" type='text' name='news_title' /></td>
    </tr>
    <tr>
          <td height="57">Message</td>
          <td><input style="width:100%; height:100%;" type='text' name='news_message' /></td>
    </tr>
    <tr>
          <td colspan='2'><input type='submit' /></td>
    </tr>
</table>

This is news_center_ac.php

<?php
$conn = mysql_connect(*Connection Information*) or die(mysql_error());
$db = mysql_select_db( "db372357229")or die(mysql_error());

$author == $_SESSION['name'];

if(!empty($_POST['news_title']) || !empty($_POST['news_message']))
{
    if(!empty($_POST['news_title']) && !empty($_POST['news_message']))
    {

            $date = date( 'jS F Y' );
            $time = date( 'H:i' );
            $query = "INSERT INTO news (id, author, title, content, date, time) VALUES('', '".$author."', '".$_POST['news_title']."', '".$_POST['news_message']."', '".$date."', '".$time."')" or die(mysql_error());
            $insert = mysql_query($query) or die(mysql_error());
            echo '<p>Successful Ne开发者_C百科ws Update&nbsp;&nbsp;&ldquo;'.$_POST['news_title'].'&rdquo;';

    }
    else
    {
        echo '<p>Please fill in all fields</p>';
    }
}
?>

This is the Output on the news page (/news/index.php)

<?php
$conn = mysql_connect("*CONNECTION INFORMATION*") or die(mysql_error());
$db = mysql_select_db( "db372357229")or die(mysql_error());
$news = mysql_query("SELECT * FROM news ORDER BY date DESC,id DESC");
$output = mysql_fetch_array($news);
?>

*CONTENT*

<?php
foreach ($output as $value) {
echo "<p> &ldquo;" .$output['content']; 
echo "&rdquo;";
echo "Posted:" .$output['date'];
echo "&nbsp;" .$output['time']; 
}
?>

I just want it to output each news article in turn i can sort out the formatting later once it works.


You are misusing mysql_fetch_array(). It needs to be called in a loop, as it only returns one row at a time.

$conn = mysql_connect("*CONNECTION INFORMATION*") or die(mysql_error());
$db = mysql_select_db( "db372357229")or die(mysql_error());
$news = mysql_query("SELECT * FROM news ORDER BY date DESC,id DESC");

EDIT Added htmlentities() calls to convert html special characters

while ($row = mysql_fetch_array($news)) {
  echo "<p> &ldquo;" . htmlentities($row['content']); 
  echo "&rdquo;";
  echo "Posted:" . htmlentities($row['date']);
  echo "&nbsp;" . htmlentities($row['time']);  
}


Rewrite it this way:

<?php
while($output = mysql_fetch_array($news)) {
  echo "<p> &ldquo;" .$output['content']; 
  echo "&rdquo;";
  echo "Posted:" .$output['date'];
  echo "&nbsp;" .$output['time']; 
}
?>

When you call output first, it is only returning one value, this will loop through all.


try /news/index.php

<?php
$conn = mysql_connect("*CONNECTION INFORMATION*") or die(mysql_error());
$db = mysql_select_db( "db372357229")or die(mysql_error());
$news = mysql_query("SELECT * FROM news ORDER BY date DESC,id DESC");


while($output = mysql_fetch_assoc($news)) {
echo "<p> &ldquo;" .$output['content']; 
echo "&rdquo;";
echo "Posted:" .$output['date'];
echo "&nbsp;" .$output['time']; 
}
?>


Shouldn't the last foreach loop use $value, ie

<?php
foreach ($output as $value) {
echo "<p> &ldquo;" .$value['content']; 
echo "&rdquo;";
echo "Posted:" .$value['date'];
echo "&nbsp;" .$value['time']; 
}
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号