Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Adding Data To A Database And Displaying It Later, Using Forms, PHP and MySQL
Unholy Prayer
post Apr 7 2007, 11:01 PM
Post #1


Newbie [Level 2]
**

Group: Members
Posts: 36
Joined: 27-December 05
Member No.: 16,252



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

QUOTE
<?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.

QUOTE

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
QUOTE

<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

QUOTE

</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.




Go to the top of the page
 
+Quote Post
html
post Oct 10 2007, 04:13 AM
Post #2


Newbie [Level 2]
**

Group: Members
Posts: 31
Joined: 28-September 07
Member No.: 50,771



Thanks for giving such a good tutorial about mysql and php, its informative for me and i hope it i will be informative
for all the users of this forum. Keep sharing such a good things.
Thanks
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Complete Login System(56)
  2. How To Host Ur Own Site In 2 Mins Php+mysql Needed(34)
  3. Complete Login And Registration System(9)
  4. How To: Install Scripts And Create Database(2)
  5. Php Dynamic Signatures(9)
  6. Mysql Database Setup(1)
  7. Making Winrar Archives(12)
  8. Adding Ur Own Files On Send To(0)
  9. A Nice Mysql Server Check(4)
  10. Install Php 5 To Work With Mysql(0)
  11. Searching With Php And Mysql(2)
  12. Custom Bbcode Tags In Smf(0)
  13. Installing Php + Mysql + Apache + Phpmyadmin On Windows Part 2(0)
  14. Quiz With Php, But Without Mysql(3)
  15. How To Install An Openoffice Dictionary Manually(3)
  1. Check Referrer To Prevent Linking Yours From Other Sites(8)
  2. Starting Or Stopping Apache And Mysql Server Via Batch File(0)
  3. Cpanel Mysql Database Management(6)
  4. Simple Shoutbox(34)
  5. Simple User System(19)
  6. Php Form Data And Conditional Statements(2)
  7. Spice Up Your Forms(11)
  8. How To Group Multiple Sets Of Data In Microsoft Excel 2007(0)
  9. Adding Your Website To Google(20)
  10. Adding An Aef Forum To Snews Cms(2)
  11. Php - Forms, Date And Include(0)
  12. Getting Started With Mysql(2)
  13. Adding Flash Music Player To Home/any Page(4)


 



- Lo-Fi Version Time is now: 26th July 2008 - 06:53 AM