PHP News System Part 2 Comments (7)
by Wez Pyke - July 30, 2008 in PHP, Text Tutorials

If you haven't already seen part 1 of this tutorial then watch it here.

For the second part of this tutorial you will learn how to create a cookie to log the admin user in and create the tables for your news system.

Ok so first of all we are going to modify the file from the first tutorial.

Modify:

if($_POST['username == $username AND $_POST['password'] == $password)
{
echo '<a href="newpost.php">New Post</a>';
}

To

if($_POST['username == $username AND $_POST['password'] == $password)
{
setcookie("username", $username, time()+3600);
setcookie("password", $password, time()+3600);
echo '<a href="newpost.php">New Post</a>';
}

The two new bits of code that we add are setcookie("username", $username, time()+3600); and setcookie("password", $password, time()+3600);. What we are doing here is creating a cookie called username and assigning the value of $username to it and time()+3600 will make the cookie expire in 1 hour.

Create the news table

Insert the following MySQL code into phpMyAdmin or any kind of MySQL software that you are using

CREATE TABLE `news_system` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`author` VARCHAR( 50 ) NOT NULL ,
`date` VARCHAR( 20 ) NOT NULL ,
`title` VARCHAR( 100 ) NOT NULL ,
`post` TEXT NOT NULL
) ENGINE = MYISAM ;

Connect To The Database

Create a file called dbconnect.php in the parent directory to the admin folder.

Enter the following code into dbconnect.php

<?php
$host = "localhost";
$username = "username";
$password = "password";
$db = "news_system";

$conn = mysql_connect($host, $username, $password);
mysql_select_db($db);
?>

Change the variables $host, $username, $password and $db.
Most of the time localhost is what the host will be so if you're unsure then don't change this.

Create News Submission Form

Create a file called newpost.php and insert the following code below into the newpost.php file.

Make the form

Now that are tables have been created in our database the next thing to do is to add a HTML form and PHP code to process the form.

Our HTML Form:

<form method="POST">
Author: <input type="text" name="author"><br>
Title: <input type="text" name="title"><br>
Post:<br>
<textarea name="post" cols="30" rows="7"> </textarea><br>
<input type="submit" name="submit" value="Publish Post">
</form>

PHP Code:

<?php
require("../dbconnect.php");
//if submit button has been pressed
if($_POST['submit']){
//retrieve form text fields
$author = $_POST['author'];
$title = $_POST['title'];
$post = $_POST['post'];

//get the date
$date = date('d/m/Y');

//post information to database
$query = mysql_query("INSERT INTO news_system VALUES('','$author','$date','$title','$post')");

if($query){
echo "Post was successfully added to the database";
}
else {
echo "Post was not added";
}
}

?>

In the next post you will learn how to display the latest 5 posts.

If something isn't clear or you would like something to be included in the next post then please leave your comments.

Bookmark and Share
Share and Enjoy:
  • Print this article!
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • E-mail this story to a friend!
  • FriendFeed
  • LinkedIn
  • Ping.fm
  • Tumblr
  • Twitter

Related posts:

  1. PHP Contact Form Every website needs a way of contacting the people behind...
  2. Build a Twitter-like site with CodeIgniter and jQuery Part 2 Don't forget to check out part 1 if you...
  3. Uploading files to Rackspace Cloud (Mosso) using PHP API Rackspace Cloud is a great VPS service, but when I...
  4. Twitter-like pagination using CakePHP and jQuery I have already done this tutorial with CodeIgniter and jQuery...
  5. Adding security to CodeIgniter forms with a custom library class The class that we are going to create within CI...

Related posts brought to you by Yet Another Related Posts Plugin.

Your Ad Here

7 Responses to “PHP News System Part 2”

  1. ChiQ Montes says:

    awesome tutorial! even watched the first part! thanks!

  2. Wez says:

    I'm glad you liked it :)

  3. [...] you haven’t already viewed part 1 and part 2 then view them if you’re new to [...]

  4. kamel says:

    I have a question how I can make event in php? the user submit his email for some events and if it happen the system send the alert by email to the user just tell me in general
    Thanks in advance

  5. kamel says:

    Thank you for the email but it was not exactly what I’m looking for I’ll explain more the user just leave his email and obviously it will be stored on DB he will not receive an email immediately but if some conditions are set (ex: date=10/10/2009 and COND=yes) this COND could be controlled by the activity of an other user it is some server side program always standby, some people suggests the use of Curl library In PHP but I have no clear idea about it
    I hope finding the solution 
    Thanks in advance

  6. Jonny says:

    your a life saver!

    not using this code for the function that it is used for in this tutorial, but it has helped me make a user friendly system, for my clients to update there website.

    cheers!

  7. [...] you haven't checked out the previous tutorials then click on the names to view them. Part 1, Part 2, [...]

Leave a Reply