CH6 WEB Lecture2
CH6 WEB Lecture2
1
PHP(HYPERTEXT PREPROCESSOR)
PHP is a server-side scripting language, like ASP
PHP scripts are executed on the server
PHP supports many databases (MySQL, MS SQL,
Informix, Oracle, Sybase, Solid,
PostgreSQL,Generic ODBC, etc.)
Syntax:
A PHP scripting block always starts with <?php and
ends with ?>. A PHP scripting block can be
placed anywhere in the document.
<?php
PHP Code In Here
2
php?>
CONT’D
<?php
PHP Code In Here
?>
<script language="php">
PHP Code In Here
</script>
<%php
PHP Code In Here
%>
3
EXAMPLE
<html>
<body>
<?php
echo "Hello World";
?>
</body>
</html>
NB. Every statement end with
semicolon(;)
4
.
Each code line in PHP must end with a semicolon.
The semicolon is a separator and is used to
distinguish one set of instructions from another.
There are two basic statements to output text
5
COMMENTS IN PHP
In PHP, we use // to make a one-line comment
or /* and */ to make a comment block:
<html>
<body>
<?php
//This is a comment
/*
This is
a comment
block
*/
<!– this is comment-
?>
</body>
</html>
6
VARIABLES IN PHP
Creating (Declaring) PHP Variables
PHP has no command for declaring a variable.
A variable is created the moment you first assign a
value to it:
$myCar="Volvo";
After the execution of the statement above, the
variable myCar will hold the value Volvo.
Let's create a variable containing a string, and a
variable containing a number:
<?php
$txt="Hello World!";
$x=16; 7
?>
PHP FORM HANDLING
Input to server side scripts comes from clients
through forms.
Two methods of sending data: GET & POST
GET
Search queries and small amounts of data
Also generated when a user clicks on a link
POST
Large and secure data
The default method for HTML forms is GET
8
PHP FORM HANDLING (CONT’D)
To access form field values in PHP, use the built-in
PHP arrays: $_GET and $_POST respectively for
GET and POST request methods
The names of the form fields will be used as
$_GET[ ‘first_name’ ] 9
PHP FORMS - $_GET FUNCTION
10
PHP FORMS - $_GET FUNCTION
11
PHP FORMS - $_GET FUNCTION
12
PHP FORMS - $_GET FUNCTION
> When using method="get" in HTML forms, all
variable names and values are displayed in the
URL.
> This method should not be used when sending
14
PHP FORMS - $_POST FUNCTION
15
PHP FORMS - $_POST FUNCTION
Apart from htmlspecialchars() and (int), it should
be obvious what this does. htmlspecialchars()
makes sure any characters that are special in html
are properly encoded so people can't inject HTML
tags or Javascript into your page.
16
PHP FORMS - $_POST FUNCTION
When to use method="post"?
> Information sent from a form with the POST
17
PHP FORM HANDLING (CONT’D)
Example:
//login.html
…
<form method=‘POST’ action=“login.php”>
<input type=‘text’ name=‘username’><br>
<input type=‘password’ name=‘password’><br>
<input type=‘submit’ value=‘login’>
</form>
…
18
PHP FORM HANDLING (CONT’D)
//login.php
<?php
$uname = $_POST[ ‘username’ ];
$paswd = $_POST[ ‘password’ ];
21
CREATE A CONNECTION TO A MYSQL
DATABASE
Before you can access data in a database, you must
create a connection to the database.
22
Example
In. the following example we store the connection in
a variable ($con) for later use in the script. The
"die" part will be executed if the connection fails:
<?php
$con = mysql_connect("localhost",“root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// some code
?> 23
Closing a Connection
. connection will be closed automatically when the
The
script ends. To close the connection before, use the
mysql_close() function:
<?php
$con = mysql_connect("localhost",“root”);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// some code
mysql_close($con);
?>
24
Insert Data From a Form Into a Database
Now we will create an HTML form that can be used to
. add new records to the "Persons" table.
Here is the HTML form:
<html>
<body>
</body>
25
</html>
When a user clicks the submit button in the HTML
form in the example above, the form data is sent
to "insert.php".
26
<?php
$con = mysql_connect("localhost",“root”);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$sql="INSERT INTO Persons (FirstName, LastName, Age)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[age]')“;
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added“;
mysql_close($con); 27
?>
Update Data In a Database
The UPDATE statement is used to update existing records
in a table.
<?php
$con = mysql_connect("localhost",“root”);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
mysql_close($con);
28
?>
Delete Data In a Database
The DELETE FROM statement is used to delete records
from a database table.
<?php
$con = mysql_connect("localhost",“root”);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
mysql_close($con);
?> 29
MYSQL FUNCTIONS (CONT’D)
To select a database
mysql_select_db( string db_name )
To query a database
mysql_query( string sql)
To get the number of rows in a result set
mysql_num_rows( resource result_set )
To close a connection to a database server
mysql_close()
30