0% found this document useful (0 votes)
24 views21 pages

Assign 6

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views21 pages

Assign 6

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Web Technology Lab Manual

Assignments no: 06
TITLE

Add dynamic web application essence using PHP, HTML and MySQL.

OBJECTIVES

To understand the principles and methodologies of PHP web based applications development
process,

PROBLEM STATEMENT

Design and develop dynamic web application using PHP and MySQL as a back-end for employee
data with insert, delete, view and update operations.

OUTCOMES

Students should be able to,


1. Develop web based application using suitable client side and server side web technologies.
2. Develop solution to complex problems using appropriate method, technologies, frameworks,
web services and content management.

SOFTWARE & HARDWARE REQUIREMENTS

Software (Minimum Requirement:):


1. Ubuntu 64 bit / Windows XP.
2. XAMPP Server
Hardware (Minimum Requirement:):
Intel p4 Machine with 1GB ARM and 32GB HDD.

Page 40
Web Technology Lab Manual

THEORY-CONCEPT

1. PHP:
The PHP Hypertext Preprocessor (PHP) began as a little open source venture that advanced as an
ever increasing number of individuals discovered how valuable it was. Rasmus Lerdorf released
the principal form of PHP route in 1994. PHP is a recursive acronym for "PHP: Hypertext
Preprocessor".

PHP is a server side scripting dialect that is installed in HTML. It is utilized to oversee dynamic
substance, databases, session following, even form whole internet business locales. It is
incorporated with various prevalent databases, including MySQL, PostgreSQL, Oracle, Sybase,
Informix, and Microsoft SQL Server.

PHP is pleasingly zippy in its execution, particularly when gathered as an Apache module on the
Unix side. The MySQL server, once began, executes even extremely complex questions with
colossal outcome sets in record-setting time.

PHP bolsters a substantial number of real conventions, for example, POP3, IMAP, and LDAP.
PHP4 included help for Java and conveyed question designs (COM and CORBA), making n-
level improvement a plausibility out of the blue. PHP is excusing: PHP dialect tries to be as
pardoning as would be prudent. PHP Syntax is C-Like.

PHP performs framework capacities, i.e. from documents on a framework it can make, open,
read, compose, and close them. PHP can deal with frames, i.e. accumulate information from
records, spare information to a document; through email you can send information, return
information to the client.

You include, erase, adjust components inside your database through PHP. Access treats factors
and set treats. Utilizing PHP, you can confine clients to get to a few pages of your site. It can
encode information.

Page 41
Web Technology Lab Manual

Example:

"Hello World" Script in PHP

To get a feel for PHP, first start with simple PHP scripts. Since "Hello, World!" is an essential
example, first we will create a friendly little "Hello, World!" script.

As mentioned earlier, PHP is embedded in HTML. That means that in amongst your normal
HTML (or XHTML if you're cutting-edge) you'll have PHP statements like this –

<html>

<head>

<title> Hello World</title>

</head>

<body>

<?php echo (“Hello Php”); ?>

</body>

</html>

To create and run PHP Web pages three fundamental parts should be introduced on your PC
framework.

Web Server − PHP will work with for all intents and purposes all Web Server programming,
including Microsoft's Internet Information Server (IIS) however then regularly utilized is
unreservedly accessible Apache Server. Download Apache for nothing here −
https://httpd.apache.org/download.cgi

Database − PHP will work with for all intents and purposes all database programming,
including Oracle and Sybase yet most regularly utilized is uninhibitedly accessible MySQL
database. Download MySQL for nothing here − https://www.mysql.com/downloads/

Page 42
Web Technology Lab Manual

PHP Parser − keeping in mind the end goal to process PHP content directions a parser must be
introduced to create HTML yield that can be sent to the Web Browser. This instructional
exercise will manage you how to introduce PHP parser on your PC.

2. MySQL:

MySQL is the most famous Open Source Relational SQL Database Management System.
MySQL is outstanding amongst other RDBMS being utilized for creating different online
programming applications. MySQL is created, advertised and upheld by MySQL AB, which is
a Swedish organization. This instructional exercise will give you a fast begin to MySQL and
make you OK with MySQL programming.

What is a Database?

A database is a different application that stores a gathering of information. Every database has
at least one unmistakable APIs for making, getting to, overseeing, seeking and recreating the
information it holds.

Different sorts of information stores can likewise be utilized, for example, records on the
document framework or vast hash tables in memory yet information getting and composing
would not be so quick and simple with those kind of frameworks.

These days, we utilize social database administration frameworks (RDBMS) to store and
oversee tremendous volume of information. This is called social database since every one of the
information is put away into various tables and relations are set up utilizing essential keys or
different keys known as Foreign Keys.

A Relational DataBase Management System (RDBMS) is a product that:

  Empowers you to execute a database with tables, segments and records.


  Ensures the Referential Integrity between columns of different tables.
  Updates the lists naturally.
 Deciphers a SQL inquiry and consolidates data from different tables.

RDBMS Terminology

Before we continue to clarify the MySQL database framework, let us modify a couple of
definitions identified with the database.

Page 43
Web Technology Lab Manual

 Database: A database is a gathering of tables, with related information.



 Table: A table is a grid with information. A table in a database resembles a basic
spreadsheet.

 Column: One section (information component) contains information of one and a similar
kind, for instance the segment postcode.

 Row: A line (= tuple, passage or record) is a gathering of related information, for
instance the information of one membership.

 Redundancy: Storing information twice, needlessly to make the framework quicker.

 Essential Key: An essential key is exceptional. A key esteem can not happen twice in
one table. With a key, you can just discover one column.

 Outside Key: A remote key is the connecting pin between two tables.

 Compound Key: A compound key (composite key) is a key that comprises of numerous
sections, since one segment isn't adequately exceptional.

 Index: A file in a database looks like a file at the back of a book.

 Referential Integrity: Referential Integrity ensures that an outside key esteem dependably
indicates a current column.

MySQL Database

MySQL is a quick, simple to-utilize RDBMS being utilized for some little and huge
organizations. MySQL is produced, showcased and upheld by MySQL AB, which is a Swedish
organization. MySQL is winding up so famous as a result of numerous great reasons:

 MySQL is discharged under an open-source permit. So you don't have anything to pay to
utilize it.

 MySQL is a capable program in its own particular right. It handles a huge subset of the
usefulness of the most costly and intense database bundles.

 MySQL utilizes a standard type of the outstanding SQL information dialect.

 MySQL takes a shot at many working frameworks and with numerous dialects including
PHP, PERL, C, C++, JAVA, and so forth.

 MySQL works rapidly and functions admirably even with extensive informational
indexes.

 MySQL is amicable to PHP, the most refreshing dialect for web advancement.

 MySQL underpins huge databases, up to 50 million lines or more in a table. The default
document measure restrain for a table is 4GB, yet you can expand this (if your working
framework can deal with it) to a hypothetical utmost of 8 million terabytes (TB).

Page 44
Web Technology Lab Manual

 MySQL is adaptable. The open-source GPL permit enables developers to alter the
MySQL programming to fit their own particular surroundings.

TECHNOLOGY/TOOL

1. Technology is to be used is PHP (PHP Hypertext Preprocessor) and tool XAMPP server is to
be used to execute PHP web application.
2. XAMPP server embeds the PHP, MySQL and phpmyadmin, these three tools must be
required to run php web application.
DESIGN/EXECUTION STEPS

For the design purpose html and CSS is to be used. For this design part contains the GUI of web
applications, how its looks like? When users going to use the web application.
Steps to install XAMPP and configure the PHP, MYSQL server.
1. Download the XAMPP using following link (download latest version as per your Operating
system Windows/ Linux). Here we are using Linux Ubuntu Systems. Copy and paste downloaded
XAMPP into home location. https://www.apachefriends.org/download.html

2. Install XAMPP with following command in terminal(copy paste the command).


sudo ./xampp-linux-x64-7.2.0-0-installer.run

3. After ab
4. ove command following installation window will appear.

Page 45
Web Technology Lab Manual

Click on next until all installation windows are completed.


During installation XAMPP look like as follows:

after successful completion of installation. It will ask to run XAMPP and here say to run. After run
the MySQL and Apache web server should be in running state. See below image.

As per above image you are ready with Apache Web Server and MySQL Database.

Page 46
Web Technology Lab Manual

4. Now Open browser and type “localhost” in URL with


quotes. Following page should be displayed.

5. the Default directory is with following path


To become root just open the terminal and type the following command sudo -i

after you become the root just type the following command to enter the root directory

cd /
“opt” is the directory in which XAMPP is installed by default.

6. As you have successfully installed and started XAMPP now just navigate to htdocs by typing the
following command into terminal:

Note: to edit or create any file in htdocs you need to be root : sudo -i

7. To navigate to root folder

cd /

Page 47
Web Technology Lab Manual

8. To navigate to htdocs

cd opt/lampp/htdocs

9. To create a file hello.php

gedit hello.php

Code for Sample Hello Script

<?php
echo(“Hello, Welcome to First PHP Application”);

?>

10. Now go to your browser then type localhost/filename.php

11. Open phpmyadmin to create MySQL database


type in browser URL “localhost/phpmyadmin” you will see following window.

Here you can create the database and table inside database as per your need.Here we created “test”
database with “users” table contain four columns.

Page 48
Web Technology Lab Manual

To start XAMPP Manually simply call this command on root:

sudo /opt/lampp/lampp start

To stop XAMPP manually simply call this command on root:

sudo /opt/lampp/lampp stop

CONCLUSION/ANALYSIS

In this assignment, we have studied how to design and develop small web application using PHP
script, XAMPP server with apache server and MySQL as backend.

PROGRAM CODE WITH OUTPUT

Here we have created “emp” directory in htdocs directory contain following files
1. index.php
2. add.html
3. add.php
3. edit.php
4.delete.php
5. config.php
6. database.sql(optional)

1. index.php
<?php
//including the database connection file
include_once("config.php");
//fetching data in descending order (lastest entry first)
//$result = mysql_query("SELECT * FROM users ORDER BY id DESC"); // mysql_query is
deprecated
$result = mysqli_query($mysqli, "SELECT * FROM users ORDER BY id DESC"); // using
mysqli_query instead
Page 49
Web Technology Lab Manual

?>

<html>
<head>
<title>Homepage</title>
</head>

<body>
<a href="add.html">Add New Data</a><br/><br/>

<table width='80%' border=0>

<tr bgcolor='#CCCCCC'>
<td>Name</td>
<td>Age</td>
<td>Email</td>
<td>Update</td>
</tr>
<?php
//while($res = mysql_fetch_array($result)) { // mysql_fetch_array is deprecated, we need to
use mysqli_fetch_array
while($res = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>".$res['name']."</td>";
echo "<td>".$res['age']."</td>";
echo "<td>".$res['email']."</td>";
echo "<td><a href=\"edit.php?id=$res[id]\">Edit</a> | <a
href=\"delete.php?id=$res[id]\" onClick=\"return confirm('Are you sure you want to
delete?')\">Delete</a></td>";
}
?>
</table>
</body>
Page 50
Web Technology Lab Manual

</html>

2. add.html
<html>

<head>

<title>Employee Data</title>

</head>

<body>

<a href="index.php">Home</a>

<br/><br/>

<form action="add.php" method="post" name="form1">

<table width="25%" border="0">

<tr>

<td>Name</td>

<td><input type="text" name="name"></td>

</tr>

<tr>

<td>Age</td>

<td><input type="text" name="age"></td>

</tr>

<tr>

<td>Email</td>

<td><input type="text" name="email"></td>

Page 51
Web Technology Lab Manual

</tr>

<tr>

<td></td>

<td><input type="submit" name="Submit" value="Add"></td>

</tr>

</table>

</form>

</body>

</html>

3. add.php
<html>

<head>

<title>Add Data</title>

</head>

<body>

<?php

//including the database connection file

include_once("config.php");

if(isset($_POST['Submit'])) {

$name = mysqli_real_escape_string($mysqli, $_POST['name']);

$age = mysqli_real_escape_string($mysqli, $_POST['age']);

$email = mysqli_real_escape_string($mysqli, $_POST['email']);

Page 52
Web Technology Lab Manual

// checking empty fields

if(empty($name) || empty($age) || empty($email)) {

if(empty($name)) {

echo "<font color='red'>Name field is empty.</font><br/>";

if(empty($age)) {

echo "<font color='red'>Age field is empty.</font><br/>";

if(empty($email)) {

echo "<font color='red'>Email field is empty.</font><br/>";

//link to the previous page

echo "<br/><a href='javascript:self.history.back();'>Go Back</a>";

} else {

// if all the fields are filled (not empty)

//insert data to database

$result = mysqli_query($mysqli, "INSERT INTO users(name,age,email)


VALUES('$name','$age','$email')");

//display success message

echo "<font color='green'>Data added successfully.";


Page 53
Web Technology Lab Manual

echo "<br/><a href='index.php'>View Result</a>";

?>

</body>

</html>

4. edit.php
<?php

// including the database connection file

include_once("config.php");

if(isset($_POST['update']))

$id = mysqli_real_escape_string($mysqli, $_POST['id']);

$name = mysqli_real_escape_string($mysqli, $_POST['name']);

$age = mysqli_real_escape_string($mysqli, $_POST['age']);

$email = mysqli_real_escape_string($mysqli, $_POST['email']);

// checking empty fields

if(empty($name) || empty($age) || empty($email)) {

if(empty($name)) {

echo "<font color='red'>Name field is empty.</font><br/>";

Page 54
Web Technology Lab Manual

if(empty($age)) {

echo "<font color='red'>Age field is empty.</font><br/>";

if(empty($email)) {

echo "<font color='red'>Email field is empty.</font><br/>";

} else {

//updating the table

$result = mysqli_query($mysqli, "UPDATE users SET


name='$name',age='$age',email='$email' WHERE id=$id");

//redirectig to the display page. In our case, it is index.php

header("Location: index.php");

?>

<?php

//getting id from url

$id = $_GET['id'];

//selecting data associated with this particular id

$result = mysqli_query($mysqli, "SELECT * FROM users WHERE id=$id");

while($res = mysqli_fetch_array($result)) {

Page 55
Web Technology Lab Manual

$name = $res['name'];

$age = $res['age'];

$email = $res['email'];

?>

<html>

<head>

<title>Edit Data</title>

</head>

<body>

<a href="index.php">Home</a>

<br/><br/>

<form name="form1" method="post" action="edit.php">

<table border="0">

<tr>

<td>Name</td>

<td><input type="text" name="name" value="<?php echo


$name;?>"></td>

</tr>

<tr>

<td>Age</td>

<td><input type="text" name="age" value="<?php echo


$age;?>"></td>

</tr>

Page 56
Web Technology Lab Manual

<tr>

<td>Email</td>

<td><input type="text" name="email" value="<?php echo


$email;?>"></td>

</tr>

<tr>

<td><input type="hidden" name="id" value=<?php echo


$_GET['id'];?>></td>

<td><input type="submit" name="update" value="Update"></td>

</tr>

</table>

</form>

</body>

</html>

5. delete.php
<?php

include("config.php");

$id = $_GET['id'];

$result = mysqli_query($mysqli, "DELETE FROM users WHERE id=$id");

header("Location:index.php");

?>

6. config.php
<?php

$database = 'localhost';

Page 57
Web Technology Lab Manual

$dbName = 'test';

$dbUser = 'root';

$dbPass = '';

$mysqli = mysqli_connect($database, $dbUser, $dbPass, $dbName);

?>

7. database.sql
a. create database emp;

b. use emp;

c. CREATE TABLE `users` (

`id` int(11) NOT NULL auto_increment,

`name` varchar(100) NOT NULL,

`age` int(3) NOT NULL,

`email` varchar(100) NOT NULL,

PRIMARY KEY (`id`)

);

OUTPUT
1. First Window

Page 58
Web Technology Lab Manual

2. Second Window click on “Add New Data” see top left corner link

3. click “add” to add data in database and it will display below window

Page 59
Web Technology Lab Manual

4. Click on “View Result ” below image will be displayed with all details in database.

5. for delete and edit, use Update link from the right side of above web page.

ORAL QUESTIONS

1. What is the use of "echo" in php?

2. How to include a file to a php page?

3. Differences between GET and POST methods ?

4. What is the use of 'print' in php?

5. What is the difference between Session and Cookie?

6. What are the different errors in PHP?

7. How to print current date and time?

8. What is the difference between sql and Mysql?

9. Why do we use GROUP BY and ORDER BY function in mysql?

10. What is JOIN in MySQL? What are the different types of join?
Page 60

You might also like