Jump to content
xisto Community
Sign in to follow this  
Unholy Prayer

Adding Data To A Database And Displaying It Later Using Forms, PHP and MySQL

Recommended Posts

Requirements:[li]PHP Support

[li]MySQL Database access

I am going to use a news program as an example.

 

Ok, first you are going to need to connect to the database. Do so by using the code below. I have added some comments where you will need to edit to fit your server's specifications. Create a new file with notepad and call it config.php

 

<?php

 

//Change root to your database account's username

$dbusername = "root";

 

//Add your account's password in between the quotations

$password = " ";

 

//Add the name of the database you are using in between the quotations

$database = " ";

 

//This usually does not need to be changed.

$server = "localhost";

 

//Do not edit coding below this line.

mysql_connect($server, $dbusername, $password) or die(mysql_error());

 

mysql_select_db($database) or die(mysql_error());

 

?>

Now we need to create a table for the data to be inserted into. Use PHPMyAdmin to create the table with the SQL feature.

 

CREATE TABLE `news` (

`newsID` mediumint(8) NOT NULL auto_increment, //This will automatically change everytime you post a new database record so we won't have to use an input to change it.

`newsSubject` varchar(25) NOT NULL default '',

`newsAuthor` varchar(20) NOT NULL default '',

`newsMessage` varchar(200) NOT NULL default '',

`newsDate` varchar(25) NOT NULL default '',

PRIMARY KEY (`user_id`)

) TYPE=MyISAM;


Ok, now that we've finished connecting to the database, it's time to write out the form and insertion code. Name the following file add.php

<HTML>

<form action="<? $_SERVER['PHP_SELF']; ?>" method="POST">

Author: <input type='text' size='30' name='author'><br><br>

Subject: <input type='text' size='30' name='subject'><br><br>

Message: <textarea name='message' rows='5' cols='30'></textarea><br><br>

Date: <input type='text' size='30' name='date'><br>br>

<input type='submit' name='submit' value='Add News'></form>

 

<?php

//Include the database file

require_once('config.php');

 

//If the submit button is pressed

if(isset($_POST['submit'])){

 

 

//Turn the inputs from the form into variables for insertion

$author = $_POST['author'];

$subject = $_POST['subject'];

$message = $_POST['message'];

$date = $_POST['date'];

 

//Begin the database query

$insert = mysql_query("INSERT INTO news(newsAuthor,newsSubject,newsMessage,newsDate)

values('$author', '$subject', '$message', '$date')");

 

//Echo a message if the news was added

echo "The news was successfully inserted into the database.";

 

}

 

?>


Now that we have the insertion code written, we have to write a code that displays the news articles. Name the following file news.php

 

</php

require_once('config.php');

 

//The following bit of code will select the news and order the articles by their id, so if you post an article, the same article will show up at the top of the list.

$query = mysql_query("SELECT * FROM news order by newsID desc");

 

while($n=mysql_fetch_array($query)){

 

$author=$n["newsAuthor"];

$subject=$n["newsSubject"];

$message=$n["message"];

$date=$n["date"];

 

echo "<b>$subject<br>$message<br>Posted by $author on $date";

 

}

 

?>


And there you have it, folks! A simple and easy way to create an automated news system.

Share this post


Link to post
Share on other sites

Thanks for giving such a good tutorial about mysql and php, its informative for me and i hope it i will be informativefor all the users of this forum. Keep sharing such a good things.Thanks

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.