|
|
|
|
![]() ![]() |
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]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 Now we need to create a table for the data to be inserted into. Use PHPMyAdmin to create the table with the SQL feature.//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()); ?> 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. |
|
|
|
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 |
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 26th July 2008 - 06:53 AM |