CoffeeCup News




Formbuilder Using a MySQL Database

This page hopefully will show you how to display selected data submitted via FormBuilder to a MySQL Database. You must have already created your database on your server and used Formbuilder once to build the the Table and Field names.

 

Below is the form which will add information to the database and further down the page will be the actual PHP Code you will need to show selected content within a table on a web page.
When you fill in the Form and Submit it, you will come back to this page and see your Submission in the Table below, your email address will not be shown, if you do not want your real name to show just enter a nickname.

Submitted Data

Here are the results of the User submitted data.

 

NameFirst ChoiceSecond ChoiceThird Choice
DarceAd ProducerAd ProducerAd Producer
Darce JeanAd ProducerAd ProducerAd Producer
MikeAd ProducerFirestarterPix Converter
meAd ProducerAd ProducerAd Producer
Lee8-11-10Ad ProducerAd ProducerAd Producer
Me SmithAd ProducerAd ProducerAd Producer
FERNANDO GONZALEZAd ProducerAd ProducerAd Producer
simonAd ProducerCart Creator ProFlash Blogger
DennisCart CreatorCart DesignerCart Designer Pro
rtCart CreatorCart Designer ProAd Producer
leventCart CreatorCart Creator ProCart Designer
aCart CreatorCart Creator ProCart Designer
fredddyCart CreatorPhoto GalleryWeb Jukebox
BruceCart CreatorCart Designer ProCart Designer
geneCart CreatorCart Designer ProImage Mapper
AndreaCart Creator ProCart Designer ProLive Chat
Agnieska_rCart Creator ProCart DesignerAd Producer
blahblahblahCart Creator ProCart Designer ProGif Animator
fCart Creator ProCart Designer ProCart Designer Pro
SharonCart Creator ProCart DesignerAd Producer
MECart Creator ProCart DesignerCart Designer Pro
dfsfsdfCart Creator ProCart Designer ProGif Animator
KevCart Creator ProHTML EditorCart Designer Pro
appleCart Creator ProDirect FTPWeb Access Manager
WilsonCart Creator ProCart Designer ProFirestarter
asaCart Creator ProStyleSheet MakerAd Producer
Fred FrogCart Creator ProCart DesignerFlash Blogger
scott rCart Creator ProCart DesignerDo Not have One
LeeCart DesignerCart DesignerDirect FTP
sadfsdCart DesignerLive ChatDo Not have One
dfsfsfCart DesignerCart Designer ProCart Designer
skatopoulosCart DesignerCart Creator ProDo Not have One
someones first nameCart DesignerColor SchemerGif Animator
sdfsdfCart DesignerCart Designer ProAd Producer
KenCart DesignerColor SchemerMP3 Rip & Burn
judgeCart Designer ProDirect FTPDo Not have One
Pete NCart Designer ProPhoto GalleryDo Not have One
LeeCart Designer ProCart Creator ProAd Producer
dsfdsfCart Designer ProCart DesignerAd Producer
testCart Designer ProCart CreatorCart Designer Pro
dominicCart Designer ProForm BuilderVSD
peteDirect FTPPix ConverterHTML Editor
A CarterFirestarterMP3 Rip & BurnDo Not have One
AllenFirestarterDo Not have OneDo Not have One
testForm BuilderAd ProducerAd Producer
LloydForm BuilderCart DesignerWebsite Font
MattForm BuilderHTML EditorAd Producer
luckyForm BuilderDirect FTPLive Chat
GGHTML EditorForm BuilderDo Not have One
PaulHTML EditorPhoto GallerySitemapper
JackHTML EditorWeb Access ManagerSitemapper
LewisHTML EditorForm BuilderDo Not have One
ThomasHTML EditorForm BuilderSitemapper
Jo AnnHTML EditorColor SchemerForm Builder
LauraHTML EditorFlash Website SearchDo Not have One
DickHTML EditorWeb Access ManagerForm Builder
CarlosHTML EditorForm BuilderDo Not have One
AbeHTML EditorForm BuilderSitemapper
JimHTML EditorForm BuilderSitemapper
MarkLive ChatHTML EditorWeb Access Manager
DonPassword WizardCart Designer ProWeb Access Manager
JoeRSS News FlashSitemapperDo Not have One
ChrisVSDSitemapperPhoto Gallery
BubbaVSDCart Creator ProCart Designer Pro
DTGVSDHTML EditorForm Builder
MarkVSDForm BuilderWeb Access Manager
David TVSDHTML EditorCart Designer Pro
MikeVSDCart CreatorCart Designer
jackieVSDCart Creator ProFlash Menu Builder

 

 

Required Code

Please Note: When you have created your page, save your page with the .PHP extension.

 

If your server supports the .htaccess file, and depending what version of PHP your server is running, you can add a line into the .htaccess file which will allow you to carry on using the .html file format and run php and 'include' from within your page.

 

For php 5 just add this line:- AddHandler application/x-httpd-php5 .html .htm
For previous version use:- AddHandler application/x-httpd-php .html and AddHandler application/x-httpd-php .htm

Some Apache servers are already configured to do this, so please check first.

 

Here is how to edit the code you will need to add to your page in order to display it on your page.
There are a few things you will have to change to match the code to your own database settings, start from the top of the code and where you see "yourinfo" (there are 3 of these) change this to your own info, next look for "tablename in this line:-
$result = mysql_query("SELECT * FROM tablename ORDER BY First",$dbcon); " change this to to name of your Table, next after "ORDER BY" change First with the Fieldname you would like the data sorting by.
Finally you will need to change the strings below to match your own Fieldnames:

// load to vairables
$field1 = $arow["yourfield"];
$field2 = $arow["yourfield"];
$field3 = $arow["yourfield"];
$field4 = $arow["yourfield"];
// -------------------------- display data
// output rows
echo "<tr><td>$field1</td><td>$field2</td><td>$field3</td><td>$field4</td>";

That is it, below is the full code to be edited.

 

<?php
// ------------------------ database connection
// VARS for connection
$vDBHost = "localhost";
$vDBUser = "yourinfo";
$vDBPass = "yourinfo";
$vDBName = "yourinfo";
// Open Db Connection
function OpenDB()
{
global $vDBHost;
global $vDBUser;
global $vDBPass;
global $vDBName;
// Connect to server
$con = mysql_connect("$vDBHost","$vDBUser","$vDBPass");
if(!$con)
{
echo mysql_errno() . ": " . mysql_error() . "<br>\n";
echo "Could not connect to database server!<br>\n";
exit;
}
if(!mysql_select_db($vDBName, $con))
{
echo mysql_errno() . ": " . mysql_error() . "<br>\n";
echo "Could not connect to database!<br>\n";
exit;
}
return $con;
}

// Connect to DB
$dbcon = OpenDB();

// -------------------------- fetch data
echo "<table width=\"540\" align=\"center\">";
echo "<tr><th>Name</th><th>First Choice</th><th>Second Choice</th><th>Third Choice</th></tr>";
// select all the records
$result = mysql_query("SELECT * FROM tablename ORDER BY First",$dbcon);
// for each record found - while loop
while ($arow = mysql_fetch_array($result)) {
// load to vairables
$field1 = $arow["Name"];
$field2 = $arow["First"];
$field3 = $arow["Second"];
$field4 = $arow["Third"];
// -------------------------- display data
// output rows
echo "<tr><td>$field1</td><td>$field2</td><td>$field3</td><td>$field4</td>";
} // end while loop
echo "</table>";
?>

 

Table CSS

If you want the CSS code for the table display, here it is, you can leave the scroll box section out if you want.

 

<style type="text/css">
table
{
font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
font-size: 12px;
background: #fff;
border-collapse: collapse;
text-align: left;
}
th
{
font-size: 14px;
font-weight: normal;
color: #039;
padding: 10px 8px;
border-bottom: 2px solid #6678b1;
}
td
{
border-bottom: 1px solid #C0C0C0;
color: #669;
padding: 6px 8px;
}
.scroll {
width: 560px;
height: 120px;
margin-left: 100px;
margin-bottom: 10px;
padding-left: 5px;
padding-right: 5px;
overflow: auto;
}
</style>