PHP News System


Ok, so you want to know how to update your site easily without editing the source code of your webpage all the time? Well heres an easier way to do it using php and a mysql database.

First you must create a database and enter this query;

Code:
CREATE TABLE `News` (
  id int(11) NOT NULL auto_increment,
  NewsTitle mediumtext NOT NULL,
  NewsText mediumtext NOT NULL,
  author VARCHAR(20),
  NewsDate INT,
  PRIMARY KEY  (`id`)
)
Once you have a table and database set up make a new file called news.php or anything else you would want to name it and put this code in it this is where you will enter or delete your news

PHP Code:
<HTML>
<HEAD>
<TITLE>Post News</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
</HEAD>
<BODY>

<?php
  
if (isset($addnews)):
?>

<FORM ACTION="<?php echo($PHP_SELF); ?>" METHOD=POST>
<p>Type the authors name here:<BR>
<input size="25" NAME="author">
<p>Type news title here:<BR>
<input size="25" NAME="newstitle">
<P>Type your news here:<BR>
<TEXTAREA NAME="newstext" ROWS=10 COLS=40 WRAP>
</TEXTAREA><BR>
<INPUT TYPE=SUBMIT NAME="submitnews" VALUE="SUBMIT">
</FORM>

<?php
  
else:

    
// Connect to the database
    
$dbcnx = @mysql_connect(
               
"localhost""YOUR USERNAME""YOUR PASSWORD");
    if (!
$dbcnx) {
      echo( 
"<P>Unable to connect to the " .
            
"database server at this time.</P>" );
      exit();
    }

    
// the news database
    
if (! @mysql_select_db("YOUR DATABASE") ) {
      echo( 
"<P>Unable to locate the news " .
            
"database at this time.</P>" );
      exit();
    }

    
    if (
"SUBMIT" == $submitnews) {
      
$sql "INSERT INTO News SET " .
             
"NewsText='$newstext', " .
         
"author='$author', " .
         
"NewsTitle='$newstitle', " .
             
"NewsDate=now()";
      if (
mysql_query($sql)) {
        echo(
"<P>Your news has been added.</P>");
      } else {
        echo(
"<P>Error adding submitted news: " .
             
mysql_error() . "</P>");
      }
    }


    if (isset(
$deletenews)) {
      
$sql "DELETE FROM News " .
             
"WHERE ID=$deletenews";
      if (
mysql_query($sql)) {
        echo(
"<P>The news has been deleted.</P>");
      } else {
        echo(
"<P>Error deleting news: " .
             
mysql_error() . "</P>");
      }
    }
  
    echo(
"<P> Here are all the news " .
         
"in our database: </P>");
  
    
$result mysql_query(
                
"SELECT ID, NewsText FROM News");
    if (!
$result) {
      echo(
"<P>Error performing query: " .
           
mysql_error() . "</P>");
      exit();
    }
  
    while ( 
$row mysql_fetch_array($result) ) {
      
$newsid $row["ID"];
      
$newstext $row["NewsText"];
      echo(
"<P>$newstext " .
           
"<A HREF='$PHP_SELF?deletenews=$newsid'>" .
           
"Delete this News</A></P>");
    } 

    echo(
"<P><A HREF='$PHP_SELF?addnews=1'>" .
         
"Add News!</A></P>");
  
  endif;
  
?>


</BODY>
</HTML>
Alot of stuff eh? I'll explain it all, first we have this;

PHP Code:
<HTML>
<HEAD>
<TITLE>Post News</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
</HEAD>
<BODY>

<?php
  
if (isset($addnews)):
?>

<FORM ACTION="<?php echo($PHP_SELF); ?>" METHOD=POST>
<p>Type news title here:<BR>
<input size="25" NAME="newstitle">
<P>Type your news here:<BR>
<TEXTAREA NAME="newstext" ROWS=10 COLS=40 WRAP>
</TEXTAREA><BR>
<INPUT TYPE=SUBMIT NAME="submitnews" VALUE="SUBMIT">
</FORM>
all this is really is html code for text boxes that you type the news into, next we have this;

PHP Code:
<?php
  
else:

    
// Connect to the database
    
$dbcnx = @mysql_connect(
               
"localhost""YOUR USERNAME""YOUR PASSWORD");
    if (!
$dbcnx) {
      echo( 
"<P>Unable to connect to the " .
            
"database server at this time.</P>" );
      exit();
    }

    
// the news database
    
if (! @mysql_select_db("YOUR DATABASE") ) {
      echo( 
"<P>Unable to locate the news " .
            
"database at this time.</P>" );
      exit();
    }
This is how you connect to the database, enter your databse name, username, and password here.

PHP Code:
    if ("SUBMIT" == $submitnews) {
      
$sql "INSERT INTO News SET " .
             
"NewsText='$newstext', " .
             
"NewsTitle='$newstitle', " .
             
"NewsDate=now()";
      if (
mysql_query($sql)) {
        echo(
"<P>Your news has been added.</P>");
      } else {
        echo(
"<P>Error adding submitted news: " .
             
mysql_error() . "</P>");
      }
    }


    if (isset(
$deletenews)) {
      
$sql "DELETE FROM News " .
             
"WHERE ID=$deletenews";
      if (
mysql_query($sql)) {
        echo(
"<P>The news has been deleted.</P>");
      } else {
        echo(
"<P>Error deleting news: " .
             
mysql_error() . "</P>");
      }
    } 
If you delete or add news, it connects to your database and adds/deletes your news

PHP Code:
    echo("<P> Here are all the news " .
         
"in our database: </P>");
  
    
$result mysql_query(
                
"SELECT ID, NewsText FROM News");
    if (!
$result) {
      echo(
"<P>Error performing query: " .
           
mysql_error() . "</P>");
      exit();
    }
  
    while ( 
$row mysql_fetch_array($result) ) {
      
$newsid $row["ID"];
      
$newstext $row["NewsText"];
      echo(
"<P>$newstext " .
           
"<A HREF='$PHP_SELF?deletenews=$newsid'>" .
           
"Delete this News</A></P>");
    } 

    echo(
"<P><A HREF='$PHP_SELF?addnews=1'>" .
         
"Add News!</A></P>");
  
  endif;
  
?> 
This will show all the news items currently in your database.

Now that you can add and delete your news, you need to display it, use this code where you want the news to be displayed

PHP Code:
                        <?php 
$db
=mysql_connect("localhost","YOUR DATABASE USERNAME","YOUR DATABASE PASSWORD") or die ("cant connect");
mysql_select_db("YOUR DATABASE NAME",$db) or die ("cant change");
$news=mysql_query("SELECT * FROM News ORDER BY NewsDate DESC LIMIT 8") or die ("cant get em");
while(
$rows=mysql_fetch_array($news)){
$title=$rows["NewsTitle"];
$content=$rows["NewsText"];
$date=date("F d, Y H:i ",$rows["date"]);
$author=$rows["author"];
$number="100%";
printf("<table border=1 cellspacing=0 bordercolor=000000 bgcolor=555555 style=overflow:auto>");

// the title of the news
echo "<td width=300 bgcolor=#ffffff class=first><center><b>$title</b></center></td>";

// the date
echo "<td align=right bgcolor=#ffffff class=first>$date</td><tr>";

//the news itself
echo "<td colspan=2 style=overflow:auto><i>by <A href=\"mailto:noneyet\">$author</a></i><br><center>$content</center></td></table><p>";

?>
You can edit this, first enter your database info again in the spots were its needed and you may also change the tables to match your site.

Very basic but i hope it helps

Quick Tutorial Search