PHP News System Part 2

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
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • email
  • FriendFeed
  • LinkedIn
  • Ping.fm
  • Tumblr
  • Twitter

No related posts.

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

, , , ,

7 Responses to PHP News System Part 2

  1. ChiQ Montes July 31, 2008 at 7:31 am #

    awesome tutorial! even watched the first part! thanks!

  2. Wez July 31, 2008 at 10:01 am #

    I'm glad you liked it :)

  3. kamel August 15, 2008 at 7:16 pm #

    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

  4. kamel August 15, 2008 at 7:46 pm #

    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

  5. Jonny August 16, 2008 at 11:23 pm #

    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!

Trackbacks/Pingbacks

  1. The Tutorial Blog » Blog Archive » PHP News System Part 3 - August 1, 2008

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

  2. The Tutorial Blog » Blog Archive » PHP News System Part 4 - August 17, 2008

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

Leave a Reply