Вы находитесь на странице: 1из 5

CD Database

This example uses the cdcol database that comes with XAMPP. The cdcol database
has 1 table called cds which holds the name, artist and year of a CD collection, there is
also a 4
th
field id which is used as the key field. (Note that the field names in the DB
are in a foreign language and so are titel, interpret and jahr.)

The example we will create here will allow us to view the contents of the cds table, and
add records to it should we want to. To achieve this we will need to have 4 files:

cd_index.php this is the main page and shows the table contents and is displayed
in the image below.
cd_add.php this is the web page containing the form that we will use to input the
data into the database. The form contents are passed on to
add_cd.php this page actually executes the update of the database. If the
database is updated properly, a small menu appears allowing the user to view the
database or add another cd to it.
st_cds.css this is the style sheet for formatting the 3 pages above. As I have
noted before, I am not very artistic so feel free to play around with the style sheet and
make you pages look their best.





Cd_index.php
<HTML>
<HEAD>
<TITLE>CD DATABASE</TITLE>
<LINK rel="stylesheet" type="text/css" href="st_cds.css">
</HEAD>
<BODY>
<H1>Hank's CD Database</H1>
<?php
//set up connection variables
$pass="asecret";
$uname="anon";
$host="localhost";
$dbname="cdcol";
$table="cds";
//Now connect to MySQL Server
mysql_connect($host, $uname, $pass)
or die("<br><B>Cannot Connect to MySQL</B><br>".mysql_error() );
//Now connect to the Database itself
mysql_select_db($dbname)
or die("<br><B>Database Access Error!!</B><br>".mysql_error() );
//Set up query and result variables
$query = "Select * from $table";
$results = mysql_query($query);
//END PHP To Set up Table in HTML
?>
<TABLE>
<TR>
<TD class="title">NAME</TD><TD class="title">ARTIST</TD>
<TD class="title">YEAR</TD>
</TR>
<?php
//Restart PHP Code
if ($results)
{
while($cd = mysql_fetch_object($results))
{
$title = $cd -> titel;
$name = $cd -> interpret;
$year = $cd -> jahr;
//End PHP Code to to display data in HTML
?>
<TR>
<TD><?php echo("$title") ?></TD>
<TD><?php echo("$name") ?></TD>
<TD><?php echo("$year") ?></TD>
</TR>
<?php
//Restart PHP again to finsih the loop and If
}
}
else
{
die("Cannot get data from database".mysql_error() );
}
?>
</TABLE>
<UL id="menu">
<LI><A HREF='cd_add.php'>Add Another CD to DB</A></LI>
</UL>
</BODY>
</HTML>
Link to page to
add records to
the table.
End of the HTML Table
Note the
embedded PHP
echo statements.
Start of the HTML Table to display all the data from the DB
PHP Code to set up the
variables, open the MySQL
Server, open the Database, then
prepare and run the Query
Link to the Style
Sheet
add_cd.php

<html>
<head>
<title>CD Database</title>
<LINK rel="stylesheet" type="text/css" href="st_cds.css">
</head>
<body>

<H1>Hank's CD Database - Add a CD</H1>
<FORM action="add_cd.php" method="POST">
<TABLE>
<TR>
<TD class="title">Title</TD><TD class="title">Data Entry</TD>
</TR>
<TR>
<TD>Title</TD>
<TD><input type="text" name="in_title"></TD>
</TR>
<TR>
<TD>Singer/Band</TD>
<TD><input type="text" name="in_band"></TD>
</TR>
<TR>
<TD>Year</TD>
<TD><input type="text" name="in_year"></TD>
</TR>
<TR>
<TD colspan=2>
<input type="submit" value="Add to DB">
<input type="reset" value="Blank Fields">
</TD>
</TR>
</TABLE>
</FORM>
<UL id="menu">
<LI><A HREF='cd_index.php'>View All Records in DB</A></LI>
</UL>
</body>
</html>

The data from the form objects here is sent to the POST array when the submit button
(captioned Add to DB) is clicked. This process is controlled by the parameters in the
<FORM> tag at the top.

<FORM action="add_cd.php" method="POST">

This notes the name of the page to move to and the method of data transfer between
the two pages when the submit button is clicked.
cd_add.php

<html>
<head>
<title>CD Database</title>
<LINK rel="stylesheet" type="text/css" href="st_cds.css">
</head>
<body>
<H1>Hank's CD Database - CD Add Routine</H1>
<?php
//set up connection variables
$pass="asecret";
$uname="anon";
$host="localhost";
$dbname="cdcol";
$table="cds";
//Now connect to MySQL Server / Database
mysql_connect($host, $uname, $pass)
or die("<br><B>Cannot Connect to MySQL</B><br>".mysql_error() );
mysql_select_db($dbname)
or die("<br><B>Database Access Error!!</B><br>".mysql_error() );
$intitle = $_POST["in_title"];
$inband = $_POST["in_band"];
$inyear = $_POST["in_year"];
//now write the query to send the data into the database
$query="INSERT INTO $table (titel, interpret, jahr)
VALUES ('$intitle', '$inband', $inyear);";
echo "MySQL Query Run: ".$query;
//Run the query or show error message
$results = mysql_query($query);
if( $results )
{
echo("<br>Successfully saved the entry.<br>" );
?>
<UL id="menu">
<LI><A HREF='cd_index.php'>View All Records in DB</A></LI>
<LI><A HREF='cd_add.php'>Add Another CD to DB</A></LI>
</UL>
<?php
}
else
{
die( "<br><B>Oops! Problem Writing data to Database</B><br>". mysql_error() );
}
?>
</body>
</html>

The data is taken from the
POST array and stored in the
variables:
$intitle, $inband, $inyear
The query is then formed in
the variable $query before
being executed via the
mysql_query function.
Links to the other
pages in we use to
access the
database.
st_cds.css

UL#menu {
margin-left : 0;
padding-left : 0;
list-style-type : none;
}

UL#menu a{
display : block;
text-decoration : none;
background : #9FC;
color : #000;
font-size : 18px;
width : 15em;
border : 1px solid #000;
text-align : center;
}

UL#menu a:hover
{
background : #060;
color : #fff;
}

td.title{
background : #0c9;
font-size : 20px;
}

body {
background-color: #9c9;

}

H1 {
font-family: arial, trebuchet, sans-serif;
}

td {
background: #cf9;
padding : 5px;
}


These #menu options are
used to format the
Unordered Lists IDd as
menu to show a roll over
effect
These list items are
hyperlinks to other pages
in the site.
TD Cells defined as belonging to the
class title will be formatted slightly
differently from the other TD Cells

Вам также может понравиться