News Articles in PHP-Part1

Hi Fellows,

Now i’m here again came with some simple tutorial news article in php that performing adding and display the articles from the database.so we need to create two php files .

Name one file as news.php and the other one as post.php

Okay if anyone  followed my previous tutorial means This one is very simple as the previous one so lets start with the post.php file here we are going to have one textbox for the title and a textarea for the body content of the news finally a submit button.

The code is very simple as below

//action refers to redirect when it click submit

//method passing the values from the form

<form action=”post.php” method=”post”>
TITLE:<br><input type=”text” name=”title”/> <p>
//textfield
BODY:<br><textarea rows=”6″ cols=”40″ name=”body”></textarea><p>//textarea
<input type=”submit” name=”submit” value=”post it”/>//submit button
</form>

After that go to phpmyadmin  in the localhost create a new table named news so we are going to have 4 fields in that table

id_news     int                     set primary key     set auto increament

title             varchar           set length

body          text

date           date

Do the above steps to create all fields in the table. so now the table is created

Okay now we are going to insert the values into the database from the post.php

<?php
if($_POST[‘submit’])//submit is clicked
{
$title=$_POST[‘title’];
$body=$_POST[‘body’];//getting the values from the title and body
if($title && $body)//validating fields for null
{
mysql_connect(“localhost”,”root”,””) or die(mysql_error());//Database connection
mysql_select_db(“login”);//selecting database
$date= date(“y-m-d”);//date function in php
$insert=mysql_query(“INSERT INTO news values(‘ ‘,’$title’,’$body’, ‘$date’)”);//insert query where first field is empty bcoz it is auto increament
die(“Your news has been posted”);//die statement used to exit from the form and display the statement only you can use echo statement also
}
else
echo “Please enter the data”;//no value means
}
?>

I think you will be understand the script from the comments line that i included in each code of the above script if not means please ask your doubts.In the next part we will display the posted news in the News.php from the database.

Click Here For the second part of the article

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: