0% found this document useful (0 votes)
3 views30 pages

CH6 WEB Lecture2

This document provides an overview of PHP, a server-side scripting language, including its syntax, variable declaration, and methods for handling forms (GET and POST). It also covers how to connect to a MySQL database, execute queries for inserting, updating, and deleting records, and the respective PHP functions used for these operations. Key concepts such as outputting text, comments, and handling form data securely are also discussed.

Uploaded by

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

CH6 WEB Lecture2

This document provides an overview of PHP, a server-side scripting language, including its syntax, variable declaration, and methods for handling forms (GET and POST). It also covers how to connect to a MySQL database, execute queries for inserting, updating, and deleting records, and the respective PHP functions used for these operations. Key concepts such as outputting text, comments, and handling form data securely are also discussed.

Uploaded by

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

PART 6: part II

Web Database Programming


using PHP

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

with PHP: echo,die and print.


 In the example above we have used the echo

statement to output the text "Hello World".

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

 Non secure (displayed in address bar)

 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

indices in the respective arrays.


 For example, to access the value of an input box

named ‘first_name’ in a form whose method is


POST, we’d write:
$_POST[ ‘first_name’ ]
 If the form method is GET,

$_GET[ ‘first_name’ ] 9
PHP FORMS - $_GET FUNCTION

> The built-in $_GET function is used to collect


values from a form sent with method="get".
> Information sent from a form with the GET

method is visible to everyone (it will be displayed in


the browser's address bar) and has limits on the
amount of information to send (max. 255
characters).

10
PHP FORMS - $_GET FUNCTION

11
PHP FORMS - $_GET FUNCTION

The "welcome.php" file can now use the $_GET


function to collect form data (the names of the form
fields will automatically be the keys in the $_GET
array)

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

passwords or other sensitive information!


> However, because the variables are displayed in

the URL, it is possible to bookmark the page. This


can be useful in some cases.
> The get method is not suitable for large variable

values; the value cannot exceed 100 chars.


13
PHP FORMS - $_POST FUNCTION
> The built-in $_POST function is used to collect
values from a form sent with method="post".
> Information sent from a form with the POST

method is invisible to others and has no limits on


the amount of information to send.
> Note: However, there is an 8 Mb max size for the

POST method, by default (can be changed by


setting the post_max_size in the php.ini file).

14
PHP FORMS - $_POST FUNCTION

And here is what the code of action.php might look like:

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.

For the age field, since we know it is a number, we


can just convert it to an integer which will
automatically get rid of any stray characters. The
$_POST['name'] and $_POST['age'] variables are
automatically set for you by PHP.

16
PHP FORMS - $_POST FUNCTION
 When to use method="post"?
 > Information sent from a form with the POST

method is invisible to others and has no limits on


the amount of information to send.
 > However, because the variables are not

displayed in the URL, it is not possible to


bookmark the page.

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’ ];

if($uname == “user” && $paswd == “pass”){


//login successful
header( ‘Location: home.php’ );
exit();
}else{
//login failed
header( ‘Location: login.html’ );
exit();
}
19
} ?>
MYSQL FUNCTIONS WORKING WITH
PHP
 MySQL is an open-source database management
system that well integrates with PHP.
 PHP provides several functions that allow us to

work with MySQL databases.


 In order to access data in a database,
 Connect to the host machine on which the database
server is running
 Select the database with which you want to work with
 Issue queries via SQL statements
 Iterate and use the result set, if any
 For all of the above functions as well as many
others, PHP provides many handy functions. 20
MYSQL FUNCTIONS (CONT’D)
 To connect to the database host
resource mysql_connect ( string server ,
string username , string password)

server : name/IP of the machine on which


the db server is running
username : used for login to the database
server
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.

In PHP, this is done with the mysql_connect()


function.
Syntax
mysql_connect(servername,username,password);

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>

<form action="insert.php" method="post">


Firstname: <input type="text" name="firstname" />
Lastname: <input type="text" name="lastname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>

</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".

The "insert.php" file connects to a database, and


retrieves the values from the form with the PHP
$_POST variables.

Then, the mysql_query() function executes the


INSERT INTO statement, and a new record will be
added to the "Persons" table.

Here is the "insert.php" page:

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_query("UPDATE Persons SET Age=36


WHERE FirstName='Peter' AND LastName='Griffin'");

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_query("DELETE FROM Persons WHERE


LastName='Griffin'");

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

You might also like