Post Reply  Post Thread 
Professional Hosting fro Just Host
News system in PHP/MySQL
Author Message
07-28-2008, 09:40 PM
Post: #1
News system in PHP/MySQL
So you want a news system? This is a tutorial on how to make one. No ready-made script but a great (and handy) explanation.

1. Requirements
Basic knowledge of PHP and MySQL, a text editor (e.g. Notepad) and a browser.

2. Database Structure
The database will have to look something like :
table 'news'
- id INT AUTO_INCREMENT,
- author VARCHAR(xxx),
- title VARCHAR(xxx),
- message TEXT,
- date INT,
- PRIMARY KEY(id)

Notice that date is INT. That's because we'll be storing the value of php function time() which gives you a very big nummer that can be converted to dates.

2. Insert Script
The form to insert news should be only accesible by you (and/or assistents). That's why it's nice to include a small check, such as a password check. If you have a complete usersystem, you could include a usercheck to see if the user is e.g. an admin/moderator.
$password should be your desired password.
Code:
<?php
    $password = 'azerty';
    if(!empty($_POST['news_author']) || !empty($_POST['news_title']) || !empty($_POST['news_message']) || !empty($_POST['news_password']))
    {
        if(!empty($_POST['news_author']) && !empty($_POST['news_title']) && !empty($_POST['news_message']) && !empty($_POST['news_password']))
        {
            // You should check the password to see if it's correct.
            if($_POST['news_password'] == $password)
            {
                // We won't check for SQL Injection. That's something you have to do on your own. And it's not that important because the user already needed a correct password.
                $date = time();
                $query = "INSERT INTO news(id, author, title, message, date) VALUES('', '".$_POST['news_author']."', '".$_POST['news_title']."', '".$_POST['news_message']."', '".$date."'";
                // Insert the query into the database as you like.
            }
            else
            {
                echo '<p>incorect password</p>';
            }
        }
        else
        {
            echo '<p>please fill in all fields</p>';
        }
    }
<form action='insertnews.php' method='post'>
    <table>
        <tr>
            <td>Your name</td>
            <td><input type='text' name='news_author' /></td>
        </tr>
        <tr>
            <td>Title</td>
            <td><input type='text' name='news_title' /></td>
        </tr>
        <tr>
            <td>Message</td>
            <td><input type='text' name='news_message' /></td>
        </tr>
        <tr>
            <td>Password</td>
            <td><input type='password' name='news_password' /></td>
        </tr>
        <tr>
            <td colspan='2'><input type='submit' /></td>
        </tr>
    </table>
</form>
3. Show Script
Now you have to show the data.
Code:
<?php
    $shownews_query = "SELECT * FROM news ORDER BY date DESC";
    $shownews_row = mysql_fetch_array($shownews_query);
?>
<table>
    <tr>
        <td>Author</td>
        <td><?php echo $shownews_row['author']; ?></td>
    </tr>
    <tr>
        <td>Title</td>
        <td><?php echo $shownews_row['title']; ?></td>
    </tr>
    <tr>
        <td colspan='2'><?php echo $shownews_row['message']; ?></td>
</table>
And so on, on, on, on and on....





So now you have seen the basics of how to make a news system. You can add as many features as you like, such as categories, showing the last 10 news items, ...
Find all posts by this user
Quote this message in a reply
07-28-2008, 11:34 PM (This post was last modified: 07-28-2008 11:39 PM by yesar92.)
Post: #2
RE: News system in PHP/MySQL
This is a great tut. but I have no knowledge of database programing .... u deserve reputation marks for that! its great. but i understand nothing from the first step.

How much time does it take to create that script?
and can it be integrated into a php page can pictures be added to posts.

Find all posts by this user
Quote this message in a reply
07-28-2008, 11:57 PM (This post was last modified: 07-28-2008 11:58 PM by Destelbergen.)
Post: #3
RE: News system in PHP/MySQL
yesar92 Wrote:This is a great tut. but I have no knowledge of database programing .... u deserve reputation marks for that! its great. but i understand nothing from the first step.

How much time does it take to create that script?
and can it be integrated into a php page can pictures be added to posts.

The first step is how your database should look like. You can make databases in PHP, but it's easier in PhpMyAdmin.

If you have some knowledge of PHP, the script can be created in less then an hour. If you have a good knowledge of PHP you can do it in 30 minutes.

Offcourse it can be integrated, with some general knowledge of PHP you can do that without too much problems.

And pictures can be added using html.

It's really worth the effort to learn some PHP Wink
Find all posts by this user
Quote this message in a reply
07-28-2008, 11:58 PM (This post was last modified: 07-29-2008 12:02 AM by darkfate.)
Post: #4
RE: News system in PHP/MySQL
That script would be about five minutes easy for me. Not even. It's not filtering out any HTML, so you can do anything you want there. Also, time() returns a UNIX timestamp which is only 10 numbers long; not that bad.

EDIT: SQL isn't that hard:
Code:
CREATE TABLE `news` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`author` VARCHAR( 200 ) NOT NULL ,
`title` VARCHAR( 200 ) NOT NULL ,
`message` TEXT NOT NULL ,
`date` INT NOT NULL ,
INDEX ( `date` )
);

Dave / Tumblr / Neutron Server Admin
CCSU '12 : Management Information Systems
Visit this user's website Find all posts by this user
Quote this message in a reply
07-29-2008, 12:21 AM (This post was last modified: 07-29-2008 12:22 AM by yesar92.)
Post: #5
RE: News system in PHP/MySQL
k so i create the database table , I now know how to do that using phpMyAdmin , but what about the script? Do I create a new php document then insert the code in step 3 (Insert Script) into that document and I call it news.php for instance.

Than integrate the show script code into my index.php page.

Am I doing well? or Am I missing somethings?

Find all posts by this user
Quote this message in a reply
07-29-2008, 12:27 AM (This post was last modified: 07-29-2008 12:31 AM by Destelbergen.)
Post: #6
RE: News system in PHP/MySQL
darkfate Wrote:That script would be about five minutes easy for me. Not even. It's not filtering out any HTML, so you can do anything you want there. Also, time() returns a UNIX timestamp which is only 10 numbers long; not that bad.

EDIT: SQL isn't that hard:
Code:
CREATE TABLE `news` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`author` VARCHAR( 200 ) NOT NULL ,
`title` VARCHAR( 200 ) NOT NULL ,
`message` TEXT NOT NULL ,
`date` INT NOT NULL ,
INDEX ( `date` )
);
I'd use
Code:
CREATE TABLE `news` (
`id` INT NOT NULL AUTO_INCREMENT,
`author` VARCHAR( 200 ) NOT NULL ,
`title` VARCHAR( 200 ) NOT NULL ,
`message` TEXT NOT NULL ,
`date` INT NOT NULL ,
PRIMARY KEY(id)
);
I'm not a PHP/SQL-guru, so I don't really know what the difference is between yours and mine.



yesar92 Wrote:k so i create the database table , I now know how to do that using phpMyAdmin , but what about the script? Do I create a new php document then insert the code in step 3 (Insert Script) into that document and I call it news.php for instance.

Than integrate the show script code into my index.php page.

Am I doing well? or Am I missing somethings?
Yes, you miss something. I didn't include a database connection. I never include ready-made scripts (point of a tutorial is to learn how to do something, not to use the copy-paste function once more)
Find all posts by this user
Quote this message in a reply
07-29-2008, 12:52 AM
Post: #7
RE: News system in PHP/MySQL
K thanx I will try to complete that script now.
Thanx

Find all posts by this user
Quote this message in a reply
« Next Oldest | Next Newest »
Post Reply  Post Thread 
Reseller Web Hosting

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  PHP/MySQL test server Destelbergen 10 1,005 11-12-2009 01:49 PM
Last Post: Freakstorm
  How To Submit Data To MySQL Database Ben 1 377 04-08-2008 07:43 AM
Last Post: Ben
  Computer System Basics marobend 0 348 03-29-2007 05:07 PM
Last Post: marobend
  [MySQL] Basic Usage Deth 2 537 03-28-2007 05:56 AM
Last Post: darkfate
  PHP (early beginner) - 1 Connecting to a MySQL database ldechent 5 566 12-10-2006 05:10 AM
Last Post: darkfate
  [php mysql]Connectiing to database Demonic 7 786 10-08-2006 02:23 PM
Last Post: iTz RaMpAgE
  [php mysql]SELECTING INFORMATION FROM Database Demonic 1 484 08-06-2006 11:16 PM
Last Post: darkfate
  [Database Tutorial] PHP and MySQL Fadel 0 394 07-07-2006 10:11 PM
Last Post: Fadel

View a Printable Version
Send this Thread to a Friend
Subscribe to this thread |
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5

Forum Jump:


Thanks to: Studio Rex Design | The major part of investor relations is to take the company penny stocks details and current status to the view of public. | Some of our members provide english to spanish translation services | Always watch the stock market for latest finance movements. | This is not a forum to buy essay or to get members of the community to write custom essay