PHP_and_XAMPP_workshop_by_Alex_Gray

advertisement
Server-side scripting & Database Workshop
The two most popular languages for server-side scripting are; ASP & PHP. In principle there
is very little difference between the two and either can be used to achieve the same results.
However both will have differing functions and will require different syntax. In essence both
are programming languages and will therefore call on your programming skills, for example
FOR loops and IF statements.
PHP/ASP code is included within the HTML of a webpage and is used to provide dynamic
content and for interaction with a database.
For the purposes of this workshop we will focus on PHP, however the techniques used can be
equally applied to ASP. In addition client-side scripting, such as JavaScript, maybe needed by
a website, a common use is for validation. Again such languages will use a different syntax
but the same techniques.
XAMPP (http://www.apachefriends.org/en/xampp.html)
This is a package of programs which are commonly used for website development. It includes
the Apache webserver, PHP, MySQL database, PHPMyAdmin. One of the major advantages
of this package is that it is easy to install and use, unlike downloading all of the programs and
installing them separately. It also has the advantage that you can run it from a USB memory
stick, which means that you can easily run your website from any windows PC without the
need to install anything.
The one we will use can be downloaded from http://www.apachefriends.org/en/xamppwindows.html#646 . This is the windows lite version. Download the .exe file, when prompted
extract to the root of your USB (ie. F:/). If you install within a folder (eg. F:/website) xampp
will not work.
You will now find a directory called xampplite. This directory will contain a number of folders,
the most important of which is htdocs. This is where you will need to place the files for your
website.
But first check XAMPP is working by running xampp-control.exe, which is found in the
xampplite directory. On the resulting window click on the start button for both Apache and
MySQL. Note: when ending your session be sure to return to this menu and stop both and
then select exit.
Now open a browser window and enter the following URL:
http://localhost/
You should now see the XAMPP welcome screen.
MySQL – PHPMyAdmin
MySQL is a very powerful, and free, database. However it does not come with a GUI.
PHPMyAdmin is a GUI for MySQL and makes it a lot easier to use.
The following URL is the homepage for this application, using your XAMPP installation:
http://localhost/phpmyadmin/
It is fairly self explanatory, try:







Creating a new database called mydb
Add a table called mydbtable , have three fields called A , B , C set the type to INT
for each. Make field A the primary key.
Enter data into the table (using the INSERT tab)
View the data (using the BROWSE tab)
Edit the data (use the pen icon under the table displaying the data)
Delete data
Delete the database
Nick Gray
Page 1
09/03/2016
PHP – MYSQL example
Go to xampplite/htdocs and create a new folder called Test. Then create the following files
and place them inside this folder. One of the files is a SQL dump file, create a database called
test data , then click on the import tab and copy the contents of the test.txt file.
All of these files are included in the embedded zip file:
These files will setup a simple database driven website, which can be found at
http://localhost/test/index.php , however if you extract the files you may need to use
http://localhost/test/test/index.php.
The website makes use of CSS for layout, you may wish to research this in more detail. The
links on the left, and the content of Page 1 & 2, are created from the database.
The add_page.php file is incomplete. Use the internet to research how to insert data into the
database using PHP, when you finish the file and it will let you create new pages and the links
will automatically update.
The files are
index.php
<html>
<head>
<?php
// mysql database open connection code
$con = mysql_connect("localhost","root","") // the format of this code is ("address for the
database","username","password"
?>
<link href="css.css" rel="stylesheet" type="text/css"/>
<title>Homepage</title>
</head>
<body>
<div id="page_name" align="center">Homepage</div>
<div id="content_holder">
Please select a page from the left
</div>
<div id="link_bar">
<a href="index.php" title="Homepage" target="_self">Homepage</a> <br />
<a href="new_page.php" title="New Page" target="_self">New Page</a> <br />
<?php
//PHP code for retreiving page links from db
mysql_select_db("test data", $con);
$result = mysql_query("SELECT id, title FROM db_table ");
while($row = mysql_fetch_array($result))
{
echo "<a href=\"content.php?id=".$row['id']."\" title=\"".$row['title']."\"
target=\"_self\">".$row['title']."</a> <br />";
}
?>
</div>
<?php include("db_conn_close.php"); ?>
</body>
Nick Gray
Page 2
09/03/2016
</html>
content.php
<html>
<head>
<?php
// mysql database open connection code
$con = mysql_connect("localhost","root",""); // the format of this code is ("address for the
database","username","password";
$id = $_SERVER['QUERY_STRING']; //gets the id number of the page requested from the
URL
//PHP code for retreiving data from the database
mysql_select_db("test data", $con);
$result = mysql_query("SELECT * FROM db_table WHERE ".$id);
$res_data = mysql_fetch_array($result);
?>
<link href="css.css" rel="stylesheet" type="text/css"/>
<title><?php echo $res_data["title"];?></title>
</head>
<body>
<div id="page_name" align="center"><?php echo $res_data["title"];?></div>
<div id="content_holder">
<?php echo $res_data["content"];?>
</div>
<div id="link_bar">
<a href="index.php" title="Homepage" target="_self">Homepage</a> <br />
<a href="new_page.php" title="New Page" target="_self">New Page</a> <br />
<?php
//PHP code for retreiving page links from db
mysql_select_db("test data", $con);
$result = mysql_query("SELECT id, title FROM db_table ");
while($row = mysql_fetch_array($result))
{
echo "<a href=\"content.php?id=".$row['id']."\" title=\"".$row['title']."\"
target=\"_self\">".$row['title']."</a> <br />";
}
?>
</div>
<?php include("db_conn_close.php"); ?>
</body>
</html>
new_page.php
<html>
<head>
<?php
// mysql database open connection code
$con = mysql_connect("localhost","root","") // the format of this code is ("address for the
database","username","password";
?>
<link href="css.css" rel="stylesheet" type="text/css"/>
<title>New Page</title>
</head>
<body>
<div id="page_name" align="center">New Page</div>
<div id="content_holder">
Nick Gray
Page 3
09/03/2016
<form name="new_webpage" method="post" enctype="multipart/form-data"
action="add_page.php">
Page title :
<input name="page_title" value="" size="20"/> <br />
Page content :
<textarea name="page_content" cols="75" rows="10"></textarea>
<input type="submit" name="action" value="Save Page"/>
</form>
</div>
<div id="link_bar">
<a href="index.php" title="Homepage" target="_self">Homepage</a> <br />
<?php
//PHP code for retreiving page links from db
mysql_select_db("test data", $con);
$result = mysql_query("SELECT id, title FROM db_table ");
while($row = mysql_fetch_array($result))
{
echo "<a href=\"content.php?id=".$row['id']."\" title=\"".$row['title']."\"
target=\"_self\">".$row['title']."</a> <br />";
}
?>
</div>
<?php include("db_conn_close.php"); ?>
</body>
</html>
add_page.php
<?php
//Something is missing here
$page_title = $_POST["page_title"];
$page_content = $_POST["page_content"];
mysql_select_db("test data", $con);
mysql_query("INSERT INTO db_table ( ??? ) VALUES ( ??? )");// ??? need replacing
echo "<br/>";
echo "New page created, please click on the link below";
echo "<br/>";
echo "<a href=\"index.php\" title=\"Homepage\" target=\"_self\">Homepage</a>";
?>
db_conn_close.php
<?php
// mysql database close connection code
mysql_close($con);
?>
css.css
html {height: 100%;}
body {
margin : 10px 10px 10px 10px;
background-color:#FFFFFF;
height: 100%;
}
Nick Gray
Page 4
09/03/2016
#page_name {
float : right;
text-align : center;
font-family: "lucida calligraphy", arial, 'sans serif';
font-size: 250%;
color: blue;
width : 84%;
min-height: 40px;
height:auto !important;
height: 40px;
padding-top : 20px;
padding-bottom : 20px;
padding-left : 3px;
padding-right : 3px;
}
#link_bar {
clear : both;
float : left;
text-align : center;
width : 14%;
min-height : 50%;
background : ;
padding : 3px;
}
#content_holder {
float: right;
margin: 1px auto;
height: 75%;
width: 84%;
padding: 3px;
background : ;
}
__________________________________________________________________________
The following is a SQL dump file, put the contents into a file called test.txt. Create a database
called test data , then click on the import tab and copy the following:
-- phpMyAdmin SQL Dump
-- version 2.9.2
-- http://www.phpmyadmin.net
--- Host: localhost
-- Generation Time: Jun 05, 2007 at 01:24 PM
-- Server version: 5.0.33
-- PHP Version: 5.2.1
--- Database: `test data`
--- ---------------------------------------------------------- Table structure for table `db_table`
-CREATE TABLE `db_table` (
`id` int(6) NOT NULL auto_increment,
`title` text collate latin1_general_ci NOT NULL,
`content` text collate latin1_general_ci NOT NULL,
PRIMARY KEY (`id`)
Nick Gray
Page 5
09/03/2016
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci
AUTO_INCREMENT=3 ;
--- Dumping data for table `db_table`
-INSERT INTO `db_table` VALUES (1, 'Page 1', 'This is content for page 1.');
INSERT INTO `db_table` VALUES (2, 'Page 2', 'This is content for page 2');
Sources of information
Two of the best websites for help on PHP or ASP coding are:
http://www.w3schools.com/
http://www.tizag.com/
Also there are many sites offering help with coding problems, so if you encounter any errors
do a google search for the type of error.
Nick Gray
Page 6
09/03/2016
Download