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

free web hosting
Open Discussion > CONTRIBUTE > Tutorials

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

Unholy Prayer
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.




 

 

 


Reply

html
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

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.

Recent Queries:-
  1. how to database input form in openoffice - 399.70 hr back. (1)
  2. adding data to database and display it later - 406.23 hr back. (1)
  3. php displaying data from database - 406.27 hr back. (4)
  4. get form data in php - 449.95 hr back. (1)
  5. adding data to database using php code - 502.13 hr back. (1)
  6. adding data to mysql using php videos - 692.45 hr back. (1)
  7. displaying data from forms in php - 744.28 hr back. (1)
  8. php code for displaying data from database - 768.07 hr back. (1)
  9. adding tables to mysql in c-panel - 785.79 hr back. (1)
  10. displaying database data using php - 825.42 hr back. (1)
  11. displaying database data with php - 825.44 hr back. (2)
  12. using forms with mysql - 916.56 hr back. (1)
  13. php mysql examplle for adding an account - 927.05 hr back. (1)
  14. php code for displaying a items from database - 952.64 hr back. (1)
Similar Topics

Keywords : adding, data, database, displaying, forms, php, mysql

  1. Create Dynamic Html/php Pages Using Simple Vb.net Code
    Taking your application data, and creating a webpage for others to vie (1)
  2. Adding Flash Music Player To Home/any Page
    How-to (put any song you want!!!) (4)
    Want to put music on all pages (near the top)? Want any song you want? Well heres how! Place the
    following IN PLACE of the tag (paste in while thats highlighted. The text in red you HAVE to
    replace with the song url. The text in green is OPTIONAL (whether you want it to autostart or not.
    Right now it doesnt, if you want it to replace no with yes.) Of course place this in Admin
    Cp>>>Skinning and Styles>>>Board Wrappers>>>Header and Body QUOTE REPLACE THIS
    WITH YOUR SONG URL &autoStart= no " /> src="http://www.podbean.com/podcast-au....
  3. Getting Started With Mysql
    creating tables and insert data into them. (2)
    Hi in this tutorial you will learn how to create tables and insert items into them. First steps are
    to create the database - go into your cpanel and mysql databases, from there make an account and a
    database and then attach them together with all priviliges, call the database test and the account
    admin, with the pw as pass - or any other password. We need to connect to the database so first in
    your php file (probably named index.php) - this is how to do it. CODE
    mysql_connect("localhost", "admin", "pass") or
    die(mysql_error(&....
  4. Php - Forms, Date And Include
    Working with POST and GET and also the Date() function (0)
    /--------PHP FORMS, DATE AND INCLUDE TUTORIAL BY FLASHY--------\ Hi and welcome to my
    tutorial. I will show you how to make simple PHP forms with the POST and GET statements. I will
    also show show you the Date() function too, aswell as the include() function. OK, so first you need
    a good understanding of HTML, and you need to create your form using that. So lets start off with
    the HTML form. 1. Create a page called form.html. 2. Insert your standard tags like: CODE
    <html> <head> <title>Working with php forms.</title> </he....
  5. Adding An Aef Forum To Snews Cms
    "almost" integrated (2)
    Adding an AEF Forum to sNews CMS Although not truly integrated, here is an explanation of the
    steps I took to have an AEF Forum Software present itself as an 'integrated' page on the
    sNews CMS. 1. Download a standard sNews version 1.6 Content Management System. Instructions are
    found on the sNews site. (refer to link list below) There are several flavours of sNews. This method
    has only been tested on the sNews 1.6, but should work on other versions as well. 2. Created a
    MySQL Database and user, Modded the snews.php, and took the usual steps for installin....
  6. Adding Your Website To Google
    How to register with google (25)
    Hi everyone. Hopefully here im going to tell you how to register yourself with the Google search
    engine and get yourself in their results. Ill start the tutorial assuming you already have Meta
    tags or other search engine optimisation techniques in place. This tutorial is solely about
    registering with google. At one time i thought simply using meta tags etcetera would get you listed
    in Google, i then found out that didnt work, so i wondered why, and the fruits of my labour are what
    will go into this tutorial. the first step is to get a google account, this will get y....
  7. How To Group Multiple Sets Of Data In Microsoft Excel 2007
    (0)
    How to Group Multiple Sets of Data in Microsoft Excel 2007 Sometimes you may open several
    workbooks and work with a number of the same workbooks at a time. You can open this group of files
    with Microsoft Excel 2007 simultaneously. But you have to define them as part of a workspace, and
    save them in a single Excel 2007 file. To do this, follow below steps: 1- Click On the View tab and
    then in the Window group click Save Workspace. The Save Workspace dialog box appears. 2- In the File
    name field, type your work name and then Click Save. 3- At the top-left of window, C....
  8. Spice Up Your Forms
    With a bit of CSS and HTML alignment (11)
    Ever wonder how to make those stylish forms you see everywhere? Well now it's your change to
    learn. This short tutorial will show you how to do exactly that. Here is a bit of css to spice
    up the form. I have included comments to explain what classes will change what in the forms.
    CODE <style type='text/css'> .form table { background-color: #187cae; }
    /*The following css class will change the table cells within the .form div */ .form td {
    background-color: #bbe0f4;            padding: 3px;            border: 0px;         ....
  9. Php Form Data And Conditional Statements
    My third tutorial (2)
    Intro To PHP Tutorial 3 - Form Data and Conditional Statements Released 4/12/07 By Chris Feilbach
    aka GhostRider Contact Info: E-mail: assembler7@gmail.com AIM: emptybinder78 Yahoo:
    drunkonmarshmellows Website: http://www.ghostrider.trap17.com PART I - FORM DATA One of the
    things that makes PHP so popular is its ability to handle form data. You can do whatever you want
    with it, you can email it to yourself, store it in a database, display it on a page, you can do
    literally ANYTHING with it. There are two ways to send data to your server, GET and POST. Both
    are ....
  10. Simple User System
    php, mysql driven (19)
    Hey! Maybe you've seen my other tutorials...or my signature.. Anyways I'm going to show
    you how to make a system so users of your site could register accounts and you could have protected
    - user only - pages on your site /smile.gif" style="vertical-align:middle" emoid=":)" border="0"
    alt="smile.gif" /> Ok, so we start by creating a config.php file. CODE <?php
        $dbhost   = 'database host';     $dbname   = 'database name';
        $dbusername   = 'database username';     $dbuserpass = 'database pas....
  11. Simple Shoutbox
    PHP, MySQL driven.. (34)
    Ok, so I'm going to show you how to create a very basic shoutbox which is driven with PHP and a
    MySQL database. So, lets start - open a database management program like PHPMyAdmin and run these
    queries. SQL CREATE TABLE `shoutbox` ( `id` INT( 11 ) NOT NULL AUTO_INCREMENT
    , `name` VARCHAR( 255 ) NOT NULL , `mail` VARCHAR( 255 ) NOT NULL , `time`
    VARCHAR( 255 ) NOT NULL , `message` TEXT NOT NULL , `ip` VARCHAR( 255 ) NOT NULL ,
    PRIMARY KEY ( `id` ) ) TYPE = MYISAM ; CREATE TABLE `shoutbox_a....
  12. Cpanel Mysql Database Management
    Part 2.1 Of My 7 Part Cpanel Tutorial (6)
    This tutorial is an extension to my 7 tutorial series about the Cpanel. The 7 different Cpanel
    tutorials can be found below. Part 1: E-mail Management Part 2: Useful Site Management Tools
    Part 3: Useful Site Management Tools2.1 Part 4: Analysis/Log Files Part 5: Advanced Tools Part
    6: PreInstalled Scripts, Extras, and Cpanel Options Part 7: Fantastico Detailed Cpanel
    Tutorial Part 2.1: MySQL Management In this tutorial, i will explain how to add, edit, delete
    and over-all manage the MySQL Feature in the Cpanel. Creating a MySQL Database 1) ....
  13. Starting Or Stopping Apache And Mysql Server Via Batch File
    (0)
    Hi guys, this is a litte tutorial about how we start and stop the Apache and MySQL in Windows NT
    (2000, XP, 2003) via a batch file script. As we know in Windows NT based system Apache and MySQL
    installed as Windows Services. So we can stop and start it using NET command. For more information
    about DOS command, type HELP at command prompt. I assuming that your MySQL service name is "mysql"
    and your Apache (Apache 2.0.x) service name is "apache2". If you want to chek it click Start > Run >
    services.msc > OK. Windows IS NOT Case Sensitive. Let's get started!. 1. ....
  14. Check Referrer To Prevent Linking Yours From Other Sites
    Check referrer with Php and Mysql (8)
    Check Referrer Using Php To Prevent People Linking To Your Downloads From Other Sites Ever
    find that found some people are listing items, images and tuts and linking directly to the download
    url (those that are like my photoshop tutorial.php?id=0), which is a .php to count the number of
    downloads. To prevent this, you can add a piece of code to the download pages that checks which page
    referred them to the download page: if it's my domain, it downloads the file normally, if
    it's not, it will redirect to my home page instead. Important : Not all browser....
  15. How To Install An Openoffice Dictionary Manually
    adding the new dictionary by yourself (3)
    How To Install An OpenOffice Dictionary Manually Introduction The Tutorial
    What's OpenOffice? Introduction Before you all rant at me and tell me that there is a
    wizard to install dictionaries for you, it can sometimes not work. I know this because on my copy of
    OpenOffice Writer, it doesn't. This tutorial is very easy to follow and it's for those
    whose wizard is broking, just like mine or people who like to be difficult and do things the hard
    way /tongue.gif" style="vertical-align:middle" emoid=":P" border="0" alt="tongue.gif" />....
  16. Quiz With Php, But Without Mysql
    (3)
    Ok let`s start! Once I wrote it for school: At first we need questions (php) CODE
    $form_block = " <p>Quiz</p> <form method=\"POST\"
    action=\"$_SERVER[PHP_SELF]\">
    <p><strong>What's The Capital City Of England?</strong><br>
    <input type=\"text\" name=\"q1\"
    value=\"$q1\" size=30></p> <p><strong>How Many
    Letters Are There In The Alphabet?</strong>&....
  17. Installing Php + Mysql + Apache + Phpmyadmin On Windows Part 2
    Continue the last section which is installing phpMyadmin (0)
    QUOTE phpMyAdmin lets you control you MySQL database from a web browser. Steps: 1. If you
    haven't done so already, download the phpMyAdmin Database Manager - You can download the
    software from the phpMyAdmin website. Be sure to download the phpMyAdmin-2.6.2-pl1.zip file. Save
    the file on your Windows Desktop. ... ... ... Go to for more info. Post Copied. Member
    Banned ....
  18. Custom Bbcode Tags In Smf
    adding extra tags in a Simple Machines Forum (0)
    Adding custom BBCodes to your Simple Machines Forum is really very... simple. First, you will need
    to download three mods from the package manager. Faster Parsecode, ModSettings Textarea, and of
    course Custom BB Code. Once you have those mods set up you will need to go into your admin section
    and click on your "Edit Features and Options" link. In the Basic Forum Features section you will now
    find a "Custom BB Code tags" box. This box is where you will put your custom codes. You can add as
    many as you like, one per line. Let's break down the bits of a simple tag tha....
  19. Searching With Php And Mysql
    The easy way :P (2)
    Searching with PHP and MySQL is pretty easy when you think about it, especially if you're doing
    it the simple way (without boolean or whatever) /tongue.gif' border='0'
    style='vertical-align:middle' alt='tongue.gif' /> It consists of a few forms, a query and an
    output. As I said, simple! CODE <form name=\"form1\"
    id=\"form1\" method=\"post\" action=\"<?
    $php_self ?>\"> <table width=\"100%\"
    border=\"0\" cellspacing=\&#....
  20. Install Php 5 To Work With Mysql
    (0)
    hi, all php 5 is new to all people a long time( actually, I forget how long...). it introduced a
    more object-oriented design to developer. like, access type(public, private, protected), abstract
    type(classes that can't create objects), interface support, and more . althrought it is
    widerly available, someone still can't get it running with mysql. every time the php is starting
    up. a message prompted stated that some xtensions can not be found, and therefore, not loaded. so, I
    have written this to help other and newbie get it start quickly. note: thi....
  21. A Nice Mysql Server Check
    (4)
    I made this and its not very hard at all just fill in the info and it willl see if your mysql is up
    or down CODE <html> <head> <title> Mysql Connection Test
    </title> </head> <body> <h2> <?php // On this you need to put
    your host most of the times localhost username and password. $conncect = mysql_connect (
    "host", "Username", "password" ) or die (" Sorry your server
    can't connect to your mysql server <BR> Check to see you have put in the Username and....
  22. Adding Ur Own Files On Send To
    Windows xp (0)
    This tutorial will enable you to add places on the context menu under the Send To option. This is
    how u do it. Create a shortcut of the folder you want to add.Cut/Copy the shortcut and go to
    C:\Windows\Documents and Settings\Your user\Send To and paste the shorcut
    there. You can also rename the shortcut from shortcut to xxxxx to xxxxx Remember Your user stands
    for your userid that you have used to login. If you have any probs PM me.....
  23. Making Winrar Archives
    and adding password to winrar archives (13)
    **** This tutorial will show you how to put files into .rar Archive and pass worded (if wanted)
    **** What You Will Need Before continuing you will need a couple of thing, first of all you
    need WINRAR , which is a very powerful archive manager. It can reduce size for you email
    attachments, decompress RAR, ZIP and other types of files downloaded from the internet. You can get
    winrar at http://www.rarlabs.com The other thing is that make sure your using Windows XP because
    this is what I used to make this tutorial. I think it works with any other windows not....
  24. Mysql Database Setup
    tutorial walk through (1)
    Post copied. Google cache to source Credits have been adjusted. Setting Up Your MySQL &
    phpMyAdmin For The First Time This tutorial will show you how to set up your MySQL and your
    phpMyAdmin with ease, regardless of whether you are going to be using PHP Nuke or PHP Nuke
    Platinum. The process is still the same. -Log into your CPanel with your User Name and Password.
    -Click on MySQL icon -Now, we need to create a database also known as Db , Your User Name will
    make up part of the necessary details, but you don't need to do this as it will be done au....
  25. Php Dynamic Signatures
    Using the GD Module and MySQL (9)
    PHP Dynamic Signatures using the GD Module After much scowering on the internet to find a
    suitable tutorial on this subject, I came up empty-handed. So I was forced to learn it on my own
    through trial & error. And since I had discovered a lack of tutorials on this subject, I dediced to
    write one! Working Example: Abstract : Using the GD Module of PHP allows a
    developer to build custom Images with Dynamic Content. Such content could be the Requesting Users
    IP Address, Web-Browser Type, Operating System, even the number of times the user has see....
  26. Backing Up And Restoring Mysql Databases
    (10)
    If you're an Administrator on a Forum, you probably know the importance of regular data backups.
    My Forum is always being hacked by someone and they always delete our SQL Databases. Well this
    tutorial is for all of you who want to protect your data and restore it if necessary! Okay,
    backing up your data is the first part. I use Cron Jobs in my cPanel to automate the backup
    process. Just use this code for backing up all your SQL Databases: CODE mysqldump -u root
    -psecret --all-databases > backup.sql OR if you wish to backup only a single database: ....
  27. How To: Install Scripts And Create Database
    preinstalled scripts, own scripts (2)
    How to install php scripts using MySQL,phpMyAdmin on Trap17 using preinstalled script/auto
    installation cPanel > Preinstalled scripts > "Addon Scripts" or "Fantastico" or "phpBB" or the
    scripts which you wanted to install screenshot - >
    http://i6.photobucket.com/albums/y222/haalaaluu/5.jpg explaination: this method is auto install
    the scripts in public_html folder, database name and granted database user respect to its database.
    note: by Addon Scripts, you can not choose other folder structure. by fantastico you can choose the
    folder structure and it is good ....
  28. Complete Login And Registration System
    doesn't use mysql! (9)
    kLogin 0.1 QUOTE(readme.txt) Readme file to kLogin 0.1 To use the internet explorer fix:
    download the latest IE7 ZIP file
    (http://sourceforge.net/project/showfiles.php?group_id=109983&package_id=119707) Extract the ie7
    zip file to the root directory of your web server. Example, if you are using a unix/linux server,
    it's on "public_html/" or "home/public_html" Open kLogin.php file with your editor and edit the
    $info_text or $info_txt variable. Then, extract the kLogin.php file in to the root
    directory of your web server also. Just run kShoutBo....
  29. How To Host Ur Own Site In 2 Mins Php+mysql Needed
    (34)
    QUOTE Run you're own server for testing phpmysql or just to host you're own website or
    for you're friends. -needS: a PC that's all 8) - How to ? download : CODE
    http://server.paehl.de/apache20.zip : 30 seconds Installing:---> 1 minute
    *********************************** Unpack the exe where ever you want. after unpack run
    serverinst.exe and change Servername and your e-mail. Start the following files one time:
    start_apache.cmd --> start apache as service mysql_start_as_service.cmd  --> dito for mysql
    mysql_first_st....
  30. Complete Login System
    With PHP + MYSQL (57)
    Its an complete login sistem made and tested by me and I think itwill be very usefull for people who
    are tryn to learn PHP. First, let's make register.php: CODE <?
    include("conn.php"); // create a file with all the database connections
    if($do_register){ // if the submit button were clicked if((!$name)
    || (!$email) || (!$age) || (!$login) ||
    (!$password) || (!$password2)){ print "You can't let
    any fields in blank....

    1. Looking for adding, data, database, displaying, forms, php, mysql

*RANDOM STUFF*





*SIMILAR VIDEOS*
Searching Video's for adding, data, database, displaying, forms, php, mysql

*MORE FROM TRAP17.COM*
Similar
Create Dynamic Html/php Pages Using Simple Vb.net Code - Taking your application data, and creating a webpage for others to vie
Adding Flash Music Player To Home/any Page - How-to (put any song you want!!!)
Getting Started With Mysql - creating tables and insert data into them.
Php - Forms, Date And Include - Working with POST and GET and also the Date() function
Adding An Aef Forum To Snews Cms - "almost" integrated
Adding Your Website To Google - How to register with google
How To Group Multiple Sets Of Data In Microsoft Excel 2007
Spice Up Your Forms - With a bit of CSS and HTML alignment
Php Form Data And Conditional Statements - My third tutorial
Simple User System - php, mysql driven
Simple Shoutbox - PHP, MySQL driven..
Cpanel Mysql Database Management - Part 2.1 Of My 7 Part Cpanel Tutorial
Starting Or Stopping Apache And Mysql Server Via Batch File
Check Referrer To Prevent Linking Yours From Other Sites - Check referrer with Php and Mysql
How To Install An Openoffice Dictionary Manually - adding the new dictionary by yourself
Quiz With Php, But Without Mysql
Installing Php + Mysql + Apache + Phpmyadmin On Windows Part 2 - Continue the last section which is installing phpMyadmin
Custom Bbcode Tags In Smf - adding extra tags in a Simple Machines Forum
Searching With Php And Mysql - The easy way :P
Install Php 5 To Work With Mysql
A Nice Mysql Server Check
Adding Ur Own Files On Send To - Windows xp
Making Winrar Archives - and adding password to winrar archives
Mysql Database Setup - tutorial walk through
Php Dynamic Signatures - Using the GD Module and MySQL
Backing Up And Restoring Mysql Databases
How To: Install Scripts And Create Database - preinstalled scripts, own scripts
Complete Login And Registration System - doesn't use mysql!
How To Host Ur Own Site In 2 Mins Php+mysql Needed
Complete Login System - With PHP + MYSQL
advertisement



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



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute your information that might help someone here.
Ask your Doubts & Queries to get answers.. "Together, We enlight each other!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE