Web Extension PHP and XMLInternet Programming (Mu)
“esh> Year of Passing
ceclect name=yop> PHP Script for Year of
$x",
echo "The number is: $x ",
}
>
<
Department
Current Occupation
teurroceu>
<>
Name of Industry/Others
=txteomp>
‘
"Electronics & Telecommunication">Electronies & Telecommunication
TateWeb Extension PHP and XML
4
Internet Programming (MU) 3
<
Designation
Company's Address
‘
Interested for atending Alumni Event?
‘
Yes
input type=radio name="e1" value
> No
>
Any Suggestions?
‘
‘
‘
WateInternet Programming (MU)
output
@ Atumni
<
> c
Avo: el
Alumal Registration Form
First Name
Last Name
Address
Mobile Numb.
Email
Department
Current
Occupation
Name of
i
Industry/Others —
Designation
Company
Address
Interested for
attending
\ Alumni Event’
Any
Suggestions?
Upon Fill the Form the Data will be Si
inside the database with the required nu
stor} Reso! |
5:35 Web Extension PHP and XML
jist
fon Form
A Notsecure | locathostvalumny.. st & @ i
Top SAL ine
P SQL Interwews... (1) Thank You EB Resaing ist
Rin
er
@Yes ONO
submitted to The Database. We have to create the Database and the table
mber of fields. We are going to use MySQL database for storing and
retrieving data to and from database.
Following PHP Script, we ha
sve used to perform Alumni Registration by adding the data tothe alumni database,
YeeWeb Extension PHP ar
yet Programming (MU) 5-36
BY using post method, we are
accessing the data entered in each
field of Registration .phpfile
Smob=$_POSTYtstmobile'};
$Seml=$_POST[txtemail;
$y0p=$_POSTIYop']s
$dept=$_POSTTdept’}
Scur=$_POST[xtcurroccu'};
:_POSTL txteomp';
$_POSTItxtdesies
Scompaddr=$_POST[txtcompaddr'};
$r1=$_POsTfs); ‘
Ssuggestion=$_POST[txtsuge]s _—
Connection to the
‘MySQL database
Sipaddress=$_SERVER[(REMOTE_ADDR'};
con = mysql_connect("localhost","ro0t
rmysql_seleet_db(‘alumni", Seon);
if ($con)
4
ie(Could not connect: '. mysql_error)); Checking Whether Data is
eS) Already Available
{
$sq="SELECT * FROM alumni_reg where a_email_id='Sem!",
Bren} =mysql_query(8aq.8con);
if{mysl_num_tows(Sresl)>0)
{$rexe my queryl Boal $eon),
Soom yng foteh_ anny
Inserting, Vata To The
ashumno sey Table
“NN.
Siniel= Bini Ny
Seq "INSERT INTO
Suid Seow 1"
He _nhile a email bagi,
18S bub Brel
Argh oun ser
| Sy Meh Round eran
10 "
You have repintered nu
ently.
< oaky >";
flu "br> A
i Negri
}
nyso_elose(Seon);
header cation:/Samplefade_alu.php')
)
>
Following PHP S
pt is used to Generate the Report Heport PHP
";
echo "
". §
echo "
". $row['a_fnm']. "
",
echo "
". Srow['a_Inm'}. "
";
echo "
", Srow['a_adds']. "
";
echo "
", Srow'a_mobile'] . "";
echo "
" . $row I. ">";
Vem_Internet Programming (MU)
a Web Extension PHP and XML
echo "
Example (PDO)
setAttribute(PDO::ATTR_ERRMODE, PDO:ERRMODE. |
CEPTION);,
‘echo "Connected successfully";
} catch(PDOException Se) {
‘echo "Connection failed: ". $e->gethless:
}
>
Close the Connection
The connection will be closed automatically wh
following:
MySQLi Object-Oriented:
feonn->close(s
MySQUiProcedural
Pbo
™rnqi_close($eonn);
Sonn = nul;
Web Extension PHP and XML.
fen the script ends, To close the connection before, use thera ‘ng 0) eal Web Extension PHP and xy
Internet Programming See
Create Table in Database using PHP script
‘<2php
Sscrvemame = "localhost";
Susemame = "root";
Spascwont
$abname = “connection_demo";
M Create conneetion
Sconn = new mysqli($servername, $usemame, Spassword, dbname);
MU Check connection
if Sconn->connect_eror) {
Aie('Connection failed: ". $conn-> connect_ erro);
}
11 salto create table
‘$5q] = "CREATE TABLE Students (
Rollno INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50))";
if (Sconn->query($sql) =
TRUE) {
‘echo "Table Student created successfully";
Velse {
‘echo "Error ereating table: "
y
Sconn->close();
= Sconn-> error;
>
Output
| EB tocathost/connectiondemosco, x + = o
<- > & OD tccathostrconnectic Eee) >>
Example (MySQLi Object-oriented) Table Student created
successfullyay _imernet Programming (MU)
ion of Record in the
ble using PHP script
enh
jeneae = Tecalon
username = “rons
gqasord =
alaame = “connection demo";
yGrate connection
conn = new mysqli(Sservemame, Susemam
Check connection oe mae
if Geonn-> connect_error) {
ie Connection failed: ". $conn->connect error);
}
$sql = "INSERT INTO students (Rollno,firstname, lastname, email)
VALUES (1', John’, 'Doc'’, 'john@example.com’)";
RUE) {
i Gconn-> query( $a
echo New record created successfully’
J else {
‘echo "Error: ". $sql ." ". $conn->error;,
)
$conn->close();,
>
Output
[B locathost/connectiondemofins: *
€ CG O D localhost/connectio
New record created successfully
Web Extension PHP and XML.
(e
¥
|
Wy atestW_ Internet Programming (MU) 5-44
Deletion of record using PHP script
conneet_error) {
die(Connection failed: "
+
4 sql to delete a record
+ Sconn-> conneet_eror);
‘$sql = "DELETE FROM students WHERE Rollno= 1";
if (Sconn-> query( $sql) === TRUE) {
echo "Record deleted successfully",
else {
‘echo "Error deleting record: " . $eonn->error;
}
Sconn->close();
>
1
| B locathostyconnectiondemoyins¢ x |
Record deleted successfully
c 9 a localhost/connectic
Web Extension PHP and XML
—
¥
Mly OE ee
ermet Reogeannning (MU)
wv!
Web Extension PHP and XML
L Code
form HTM!
git
enn?
eves?
re PUP login system
file inside index tu
extess® lref = "styleeag”>
php" method = "POST">
"user [>
= "bun" value = "Login onclick:
lode For Style.css
Firm {
Dorder: slid gray Ipxs
width:35%5
sorder-radius: 2px;
nargin: 200px auto;
sackground: whites
adding: S0px;
,
#oin {
color: #0;
>ackground: #337abT;y" ————
|
permet Programming (MU)
oret Web Extension PHP and XML.
Create database named connectior
mnnection demo database Create table named login in connection.demo database
with 2 columns username and password
rsa Fone nen rare areal
connection,
“Php
mwith the database which we have created in the previous st
steps.
i
Next step is to do the connectivity of login fortA Web Extension PHP ar id
BF _ Internet Programming (MU) 5:48 ML
We will create connection file for which code is given belo
authentication,php
inelude(connection.php');
Susemame = § POST[user'};
Spassword =
Susemame = stripeslashes(Susername);
Spassword = stripeslashes($password)s
mysqli_real_escape_string($con, $username)
$password
nysqli_real_escape_string(Bcon, Spassword);
Seal = "elect from login where username = ‘Susername! and password
‘Spassword";
$result = mysqli_query($con, B34);
$row = mysqli_fetch_array(Sresult, MYSQLI_ASSOC);
Scount = mysqli_num_rows(Sresul);
iff$count == 1){
eho "
Login successful
";
}
elseel
cho "ch > Login fled, Invalid username or passwonl."
>
Wasay SS
ag_ntermetRrgramming (Mu)
Web Extension PHP and XML
output
| & 2 SO matotinvitonatimtengy
Login successful
539 Tracking user: Session in PHp
‘The session is a Way to store information (in var
. (in variables) that a ple ation
isnot saved on the computer like a coo, ) that can be used across multiple pages. The inform:
+The process of working with an application inv
similar toa session. You are recognized by the co
end, On the internet, however, there is one prob
‘olves opening it, making changes, and then closing it: Thi
'mputer. It knows when you start the application and when you
lem: the web-server does not know who you are or what you do
+ By storing user information across multiple pages (eg. usernames, favorite colors, etc) session variables solve
this problem. Session variables last until the user closes the browser:
+ So,session variables hold information about one user, and are accessible to all pages within one application.
Start a PHP Session
+ Asession is started with the session_start) function,
+ Session variables are set with the PHP global variable: $. SESSION.
Now, let's create a new page called "demo session1 php’. In this page, we start a new PHP session and set some
session variables:
Example
shim >
sbody>
Stphp
Set session variables
§ SESSION ['favcolor"]
tho "s
‘Sthimnl>
|
|WH _Incernet Programming (MU) 5-50 Web Extension PHP and XML
Get PHP Session Variable Values
«Thesession_start() function must be the very first thing in your document. Before any HTML tags.
«Next we create another page called “demo, session2.php". From this page, we will access the session information
we set on the first page (*demo,session{.php").Notice that session variables are not passed individually to each
new page, instead they are retrieved from the session we open at the beginning of each page (session_start().
‘+ Alsonotice that all session variable values are stored in the global $_ SESSION variable:
<2
sesion stan
>
";
‘echo "Favorite animal is”
>
$_SESSION('favanimal"] ."";
Modify a PHP Session Variable
‘To change a scasion variable, just overwrite
Destroy a PHP Session
To remove all global y id session_destroy
all global session variables and destroy thesession,use ses
Wuse session_unset() and session. dest
0:gramming (MU)
5.51
Z Web Extension PHP and XML
a
7 crn hil
aval?
tos?
he
ene all session variables
ssn et
pdestoy te session
session _sestr0y 0
DP
‘nl
Review Questions
at Explain XML and XSL with example
0.2 _ Distinguish between XML & HTML
a Whatis XML? Explain how to write an XML document? What are the goals of XML? Clearly explain the XML Schema
and XML parsing in detail
4 Explain Built in functions used in PHP
fh car element has the childelements make, model, Year, colour,
rs and fuel_type.
where eacl
‘as the child elements no_of_cyfinde!
Considerthat a web page displays a form
rs numeric data. Write a PHP script which
imbers andthen displays the same with
pecifed by the action attribute of the
5 Create a DTD for a catalogue of cars:
engine and doors. The engine elementh
JOST and $_GET in PHP?
m2), where the user ente
nd the product of the two nur
‘embedded in the web page $
ined is GET.
6 What is the purpose of the implicit arrays $_P(
containing two text boxes (named num! andnu
collectsthis form data, finds the sum iference a
ble messages. Assume that the soriptis to b
form and thatthe method used when Ine {form is submit
cord in the Table using PHP script
array in PHP? Explain wih example?
0.7 Wiite a code for insertion of Fe
2.8 What aro the diforent ways 10 or20"?
ferels