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

Unit 5: Introduction to

XAMPP
Introduction to XAMPP
Objectives
At the end of this chapter, you should be able to:

• Install XAMPP
• Create a MySQL database
• Insert, update and delete data in tables
• Connect to MySQL database using PHP
• Insert records in database using PHP
• Validate forms using PHP
Introduction

XAMPP is an open source web server used as development tool for


designers and programmers to test their work on their computers
without any access to Internet. This package consists of Apache
HTTP Server, MySQL database and interpreters for script written in
PHP.
Introduction to MySQL

MySQL is a mainly popular open source database, it allows the user to store data in
database in form of tables using SQL (Structured Query Language). MySQL is a
relational database because data can be stored in multiple related tables and these
data can be used as single store storage and retrieve these data electronically from
multiple tables.
Installation of XAMPP

Download XAMPP and start installation and by default XAMPP is installed in


directory C:/xampp
Accessing MySQL via
PhpMyAdmin

PhpMyAdmin is an application that can be used to manage


MySQL database. As we have already Installed XAMPP, now open
web browser and type: http://localhost and select English
language from appearing screen and follow steps.

1. Click PhpMyAdmin to start working with MySQL


2. Type the name of database to create a database called Botho
University and click Create button.
3. Type the name of a table to create table called Students and
specify number of fields to be created and click Go button.
4. Type your fields names, appropriate data types and length
where required and click Save Button.
Inserting, updating, selecting and
deleting records via PhpMyAdmin

Inserting Values
Now we have created a database called Botho University along
with a table called Students, let us insert values in our table.

1. Click insert tab and enter values for the first row and click Go
button to save the record.
2. Repeat the previous step to enter another record.
Selecting and Updating Values
1. When you are in table view, the table shows the records
entered, in order to update a record in a table, select a
record to be updated by clicking on the checkbox and Click a
pencil icon
2. A window that allows you to update the values will appear,
and you can now update your records and click Go button to
save the changes.
Deleting a record

In order to delete a record from a table, click a red cross next to


pencil icon and the following prompt message will appear and
click Ok button to delete.
Activity
Task 1
1. Create a new database called Bokamoso Hospital
2. Create a table in Bokamoso Hospital called Patients with
the following fields:
Field Name Data type Length

National ID Int 15

Firstname Varchar 30

Surname Varchar 30

Date of Birth Date

Phone NO Int 15
4. National ID is a Primary key
5. Enter 5 records in your table
Establishing Connection to
MySQL
We have learnt how to create a database using MySql. Now
we are going to connect to your database called Botho
University, this is an essential step to allow your commands
to be executed, before you can connect to database make
sure that all your files are saved in htdocs .
Running PHP scripts in Xammp

Follow steps below to save your files:


1. Create a folder called WCD in a folder htdocs at
C:\Xammp\htdocs
2. Type the script below in notepad and save it as first.php in
the wcd folder
<!DOCTYPE html>
<html>
<body>

<?php
echo "My first PHP script!";
?>

</body>
</html>
To run the script type the following :
http://localhost/wcd/ as the URL . The following screen will
appear.
Click the script to
run
Connecting to the database
Some of functions that we are going to use in order to connect
to our database and enter records:

Functions Results

Connects to Mysql and you need to indicate the


mysql_connect()
hostname, username and password.

mysql_select_db() Selects your database

mysql_query() specify SQL statements

mysql_results() Returns the records in the table

mysql_error() Returns error message if query failed

mysql_close() Closes the database connection


Inserting records into a table
Create a simple HTML form interface for our table Students
and save your file in htdocs as Registration.html
<html>
<head>
<title> Registration Form</title>
</head>
<body>
<h1><center> Registration Form</h1>
<form action="Registration.php" method="post" name="Form1">

<table width="50%" align="center">


<tr><td>Student ID</td><td><input name="stuid" type="text" ></td></tr>
<tr><td>Student Name</td><td><input name="fname" type="text" ></td></tr>
<tr><td>Gender</td><td><input name="gender" type="text"></td></tr>
<tr><td>Postal Address</td><td><input name="address" type="text"></td></tr>
<tr><td>Email Address</td><td><input name="email" type="text"></td></tr>
<tr><td>Contact No</td><td><input name="phone" type="text" ></td></tr>
</table>
<center><input name="Submit" type="submit" value="Submit Form" />
</form></body></html>
Now we are going to enter a new record in our table using php,
type the following code and save it as Registration.php. We are
going to start by assigning variables used in students table. For
instance we are assigning Student_ID a name stuid which
references the name used in Registration form and POST
references the method specified in the Registration Form.
<?php
$id=$_POST['stuid'];
$fname=$_POST['fname'];
$gender=$_POST['gender'];
$address=$_POST['address'];
$email=$_POST['email'];
$phone=$_POST['phone'];
mysql_connect("localhost","root","")or die('Connection not Extablished');
mysql_select_db("botho university") or die('Database not connected');
$query="INSERT INTO students VALUES
('".$id."','".$fname."','".$gender."','".$address."','".$email."','".$phone."')";
mysql_query($query) or die ('Error, insert query failed');

echo"registration Successful";

?>

Now run the registration form in htdocs and enter your first
record as shown below and click Submit Form button to
submit your data into a database
Form Validation

Form validation ensures that data is entered correctly.


Validation can prevent blank spaces, input length, can allow
numbers only, ensures correct email address format. It can be
carried out on each form element to ensure that data is
entered correctly. Validation can be performed on client side
using Javascript or on a server side using Php.

In order to check if the user has entered a value in text boxes


you can use a function called empty ()

For instance let’s validate Student ID so that it does not allow


blank space
if (!empty($_POST['stuid'])){
$msg = "stuid;
$_POST[stuid] ";
}
else
{
$id = NULL;
echo "Please fill out your Student ID.
";
}
Activity

1. Create a simple HTML form interface for our table Patients


and save your file in htdocs as patients.html
2. Connect to database called Bokamoso Hospital and enter at
least 2 records using your created form.
3. Validate all fields so that they do not allow null values
Summary

From this chapter you have learnt how to install XAMPP and basics of PHP and
MYSQL. XAMPP is an Apache distribution with the most collective web development
technologies. It can be used for testing applications in PHP and MySQL. It can be
easily installed and used in all platforms. A database is a collection of data from a
base and MYSQL is the most popular database for web servers. We have learnt how
to insert values to a table using PHP. Form validation was used to validate the data
so that the form does not all null values to be submitted to the database.

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