0% found this document useful (0 votes)
17 views22 pages

UNIT-II PHP 2022 New

PHP NOTES

Uploaded by

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

UNIT-II PHP 2022 New

PHP NOTES

Uploaded by

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

Unit – II: HTML, HTML forms and MySql connectivity

HTML Form
An HTML form is a section of a document which contains controls such as text fields,
password fields, checkboxes, radio buttons, submit button, menus etc.
An HTML form facilitates the user to enter data that is to be sent to the server for
processing such as name, email address, password, phone number, etc. .

Tag Description

<form> It defines an HTML form to enter inputs by the used side.

<input> It defines an input control.

<textarea> It defines a multi-line input control.

<label> It defines a label for an input element.

<fieldset> It groups the related element in a form.

<legend> It defines a caption for a <fieldset> element.

<select> It defines a drop-down list.

<optgroup> It defines a group of related options in a drop-down list.

<option> It defines an option in a drop-down list.

<button> It defines a clickable button.

HTML Form Tags

Mr.Gawas Pradeep.A
Department Of Computer Science
R.B.M.College,Chandgad
Let's see the list of HTML 5 form tags.
HTML <form> element
The HTML <form> element provide a document section to take input from user. It
provides various interactive controls for submitting information to web server such as
text field, text area, password field, etc.
Syntax:
1. <form>
2. //Form elements
3. </form>

HTML <input> element


The HTML <input> element is fundamental form element. It is used to create form
fields, to take input from user. We can apply different input filed to gather different
information form user. Following is the example to show the simple text input.

Example:
<body>
<form>
Enter your name <br>
<input type="text" name="username">
</form>
</body>
Output:

HTML TextField Control

Mr.Gawas Pradeep.A
Department Of Computer Science
R.B.M.College,Chandgad
The type="text" attribute of input tag creates textfield control also known as single
line textfield control. The name attribute is optional, but it is required for the server
side component such as JSP, ASP, PHP etc.
<form>
First Name: <input type="text" name="firstname"/> <br/>
Last Name: <input type="text" name="lastname"/> <br/>
</form>

Output:

HTML <textarea> tag in form


The <textarea> tag in HTML is used to insert multiple-line text in a form. The size of
<textarea> can be specify either using "rows" or "cols" attribute or by CSS.

Example:

Mr.Gawas Pradeep.A
Department Of Computer Science
R.B.M.College,Chandgad
<html>
<head>
<title>Form in HTML</title>
</head>
<body>
<form>
Enter your address:<br>
<textarea rows="2" cols="20"></textarea>
</form>
</body>
</html>

Output:

Label Tag in Form


It is considered better to have label in form. As it makes the code
parser/browser/user friendly.
If you click on the label tag, it will focus on the text control. To do so, you need to
have for attribute in label tag that must be same as id attribute of input tag.

<form>
1. <label for="firstname">First Name: </label> <br/>

Mr.Gawas Pradeep.A
Department Of Computer Science
R.B.M.College,Chandgad
2. <input type="text" id="firstname" name="firstname"/> <br/>
3. <label for="lastname">Last Name: </label>
4. <input type="text" id="lastname" name="lastname"/> <br/>
5. </form>

Output:

HTML Password Field Control


The password is not visible to the user in password field control.
<form>
<label for="password">Password: </label>
<input type="password" id="password" name="password"/> <br/>
</form>
Output:

The GET Method and POST Method

Mr.Gawas Pradeep.A
Department Of Computer Science
R.B.M.College,Chandgad
There are two ways the browser(client) can send information to the web server.
 The GET Method
 The POST Method

The $_ GET Method

In PHP, the $_GET variable is used to collect values from HTML forms using

method get.

Information sent from an HTML form with the GET method is displayed in the
browser's address bar, and it has a limit on the amount of information to send.

 The variable names and values will be visible in URL if HTML forms submitted by the
GET method.
 The GET method is restricted to send up to 2048 characters only.
 When you submit sensitive information like passwords then should not use this
method.
 GET method can't be used, to send binary data like images and Word documents.
 GET method data can be accessed using PHP QUERY_STRING environment variable.
.
The $_ POST Method

Mr.Gawas Pradeep.A
Department Of Computer Science
R.B.M.College,Chandgad
In PHP, the $_POST variable is used to collect values from HTML forms using

method post.

Information sent from a form with the POST method is invisible 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).

 The POST method does not have any restriction on data size to be sent.
 The POST method can be used to send ASCII as well as binary data.
 The data sent by POST method goes through HTTP header, so security depends on
HTTP protocol. By using Secure HTTP, you can make sure that your information is
secure.
 PHP $_POST associative array is used to access all the sent information by POST
method.
 Variables are not visible in the URL so users can't bookmark your page.

Mr.Gawas Pradeep.A
Department Of Computer Science
R.B.M.College,Chandgad
PHP Global Variables - Superglobals
Some predefined variables in PHP are "superglobals", which means that they are
always accessible, regardless of scope - and you can access them from any function,
class or file without having to do anything special.
The PHP superglobal variables are:
 $GLOBALS
 $_SERVER
 $_REQUEST
 $_POST
 $_GET
 $_FILES
 $_ENV
 $_COOKIE
 $_SESSION
PHP $GLOBALS
$GLOBALS is a PHP super global variable which is used to access global variables
from anywhere in the PHP script (also from within functions or methods).
PHP stores all global variables in an array called $GLOBALS[index].
The index holds the name of the variable.
The example below shows how to use the super global variable $GLOBALS:

<html>
<body>

<?php

Mr.Gawas Pradeep.A
Department Of Computer Science
R.B.M.College,Chandgad
$x = 75;
$y = 25;

function addition()
{
$GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];
}
addition();
echo $z;
?>
</body>
</html>

100

PHP $_SERVER
$_SERVER is a PHP super global variable which holds information about headers,
paths, and script locations.
The example below shows how to use some of the elements in $_SERVER:
Example

<?php
echo $_SERVER['PHP_SELF'];
echo "<br>";
echo $_SERVER['SERVER_NAME'];
echo "<br>";
echo $_SERVER['HTTP_HOST'];
echo "<br>";
echo $_SERVER['HTTP_REFERER'];
echo "<br>";
echo $_SERVER['HTTP_USER_AGENT'];
echo "<br>";
echo $_SERVER['SCRIPT_NAME'];
?>

demo/demo_global_server.php
35.194.26.41

Mr.Gawas Pradeep.A
Department Of Computer Science
R.B.M.College,Chandgad
35.194.26.41
https://tryphp.w3schools.com/showphp.php?filename=demo_global_server
Mozilla/5.0 (Linux; Android 11; SM-N980F) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/97.0.4692.87 Mobile Safari/537.36
/demo/demo_global_server.php

PHP $_REQUEST
PHP $_REQUEST is a PHP super global variable which is used to collect data after
submitting an HTML form.
The example below shows a form with an input field and a submit button. When a
user submits the data by clicking on "Submit", the form data is sent to the file
specified in the action attribute of the <form> tag. In this example, we point to this
file itself for processing form data. If you wish to use another PHP file to process
form data, replace that with the filename of your choice. Then, we can use the super
global variable $_REQUEST to collect the value of the input field:
Example
<html>
<body>

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">


Name: <input type="text" name="fname">
<input type="submit">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
$name = $_REQUEST['fname'];
if (empty($name)) {
echo "Name is empty";
} else {
echo $name;
}
}
?>

Mr.Gawas Pradeep.A
Department Of Computer Science
R.B.M.College,Chandgad
</body>
</html>
Submit
Name :

PHP $_POST
PHP $_POST is a PHP super global variable which is used to collect form data after
submitting an HTML form with method="post". $_POST is also widely used to pass
variables.
The example below shows a form with an input field and a submit button. When a
user submits the data by clicking on "Submit", the form data is sent to the file
specified in the action attribute of the <form> tag. In this example, we point to the
file itself for processing form data. If you wish to use another PHP file to process
form data, replace that with the filename of your choice. Then, we can use the super
global variable $_POST to collect the value of the input field:
Example
<html>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name="fname">
<input type="submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
// collect value of input field
$name = $_POST['fname'];
if (empty($name))
{
echo "Name is empty";
}
Else
{
echo $name;

Mr.Gawas Pradeep.A
Department Of Computer Science
R.B.M.College,Chandgad
}
}
?>
</body>
</html>
Submit
Name:

PHP $_GET
PHP $_GET is a PHP super global variable which is used to collect form data after
submitting an HTML form with method="get".
$_GET can also collect data sent in the URL.
Assume we have an HTML page that contains a hyperlink with parameters:
<html>
<body>
<a href="test_get.php?subject=PHP&web=W3schools.com">Test $GET</a>
</body>
</html>
When a user clicks on the link "Test $GET", the parameters "subject" and "web" are
sent to "test_get.php", and you can then access their values in "test_get.php" with
$_GET.
The example below shows the code in "test_get.php":

Example
<html>
<body>

<?php
echo "Study " . $_GET['subject'] . " at " . $_GET['web'];

Mr.Gawas Pradeep.A
Department Of Computer Science
R.B.M.College,Chandgad
?>

</body>
</html>
Study PHP at W3schools.com

MySql Database(Old or Improved)


PHP mysqli_connect()
PHP mysqli_connect() function is used to connect with MySQL database. It
returns resource if connection is established or null.
Syntax

Mr.Gawas Pradeep.A
Department Of Computer Science
R.B.M.College,Chandgad
1. resource mysqli_connect (server, username, password)

PHP mysqli_close()
PHP mysqli_close() function is used to disconnect with MySQL database. It
returns true if connection is closed or false.
Syntax

1. bool mysqli_close(resource $resource_link)

PHP MySQL Connect Example


Example
<?php
$servername = 'localhost';
$username = 'root';
$passward = '';
$dbname=’test’;
$conn = mysqli_connect($host, $user, $pass,$dbname);
if(! $conn )
{
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully';
mysqli_close($conn);
?>
Output:

Connected successfully

Mr.Gawas Pradeep.A
Department Of Computer Science
R.B.M.College,Chandgad
PHP MySQL Create Database
PHP MySQLi Create Database Example
Example
<?php
$servername = 'localhost';
$username = 'root';
$passward = '';
$conn = mysqli_connect($host, $user, $pass);

if(! $conn )
{
die('Could not connect: ' . mysqli_connect_error());
}
echo 'Connected successfully<br/>';

$sql = 'CREATE Database mydb';


if(mysqli_query( $conn,$sql))
{
echo "Database mydb created successfully.";
}else{
echo "Sorry, database creation failed ".mysqli_error($conn);
}
mysqli_close($conn);
?>
Output:
Connected successfully
Database mydb created successfully.

Mr.Gawas Pradeep.A
Department Of Computer Science
R.B.M.College,Chandgad
PHP MySQL Create Table
PHP mysql_query() function is used to create table
PHP MySQLi Create Table Example
Example
<?php
$servername = 'localhost';
$user = 'root';
$pass = '';
$dbname = 'test';
$conn = mysqli_connect($host, $user, $pass,$dbname);
if(!$conn){
die('Could not connect: '.mysqli_connect_error());
}
echo 'Connected successfully<br/>';

$sql = "create table emp5(id INT(20) ,name VARCHAR(20) ,


emp_salary (20) );
if(mysqli_query($conn, $sql)
{
echo "Table emp5 created successfully";
}
Else
{
echo "Could not create table: ". mysqli_error($conn);
}
mysqli_close($conn);
?>
Output:
Connected successfully

Mr.Gawas Pradeep.A
Department Of Computer Science
R.B.M.College,Chandgad
Table emp5 created successfully

PHP MySQL Insert Record


PHP mysql_query() function is used to insert record in a table
PHP MySQLi Insert Record Example
Example

<?php
$servername = 'localhost';
$user = '';
$pass = '';
$dbname = 'test';
$conn = mysqli_connect($servername,$user, $pass,$dbname);
if(!$conn){
die('Could not connect: '.mysqli_connect_error());
}
echo 'Connected successfully<br/>';
$sql = 'INSERT INTO emp5(id,name,salary) VALUES (101"sonoo", 9000)';
if(mysqli_query($conn, $sql)){
echo "Record inserted successfully";
}else{
echo "Could not insert record: ". mysqli_error($conn);
}
mysqli_close($conn);
?>
Output:
Connected successfully
Record inserted successfully

Mr.Gawas Pradeep.A
Department Of Computer Science
R.B.M.College,Chandgad
PHP MySQL Delete Record
PHP mysql_query() function is used to delete record in a table
PHP MySQLi Delete Record Example
Example
<?php
$host = 'localhost:3306';
$user = '';
$pass = '';
$dbname = 'test';
$conn = mysqli_connect($host, $user, $pass,$dbname);
if(!$conn)
{
die('Could not connect: '.mysqli_connect_error());
}
echo 'Connected successfully<br/>';

$id=2;
$sql = "delete from emp4 where id=$id";
if(mysqli_query($conn, $sql)){
echo "Record deleted successfully";
}
else
{
echo "Could not deleted record: ". mysqli_error($conn);
}
mysqli_close($conn);
?>
Output:
Connected successfully

Mr.Gawas Pradeep.A
Department Of Computer Science
R.B.M.College,Chandgad
Record deleted successfully

CURSORS

In MySQL, a cursor allows row-by-row processing of the result sets. A cursor is used
for the result set and returned from a query. By using a cursor, you can iterate, or
step through the results of a query and perform certain operations on each row. The
cursor allows you to iterate through the result set and then perform the additional
processing only on the rows that require it.

A cursor contains the data in a loop. Cursors may be different from SQL commands
that operate on all the rows in the returned by a query at one time.

MySQL cursor has three types of properties.

1. Read Only
The data in the underlying table cannot be modified via a cursor.

2. Non_Scrollable
Only rows can be retrieved in the order specified by the SELECT statement. In the
reverse order, users can not retrieve records. Additionally, in the result set, users
cannot skip rows or jump to a particular row.

3. Asensitive
An asensitive cursor is used to points the actual data, whereas a temporary copy of
the data is used by an insensitive cursor used. An asensitive cursor performs faster
than an insensitive cursor because it does not have to make a temporary copy of
data.

Mr.Gawas Pradeep.A
Department Of Computer Science
R.B.M.College,Chandgad
Working with MySQL Cursors

There are some steps we have to follow while using the MySQL Cursors, let’s see.
 Declare a Cursor
 Open a Cursor
 Fetch the Cursor
 Close the Cursor
1. Declaration of a Cursor
To declare a cursor you must use the DECLARE statement. With the help
of the variables, we need conditions and handlers to declare a cursor before we can
use it. First of all we will give the cursor a name, this is how we will refer to it later in
the procedure. We can have more than one cursor in a single procedure so its
necessary to give it a name that will in some way tell us what its doing. We then need
to specify the select statement we want to associate with the cursor. The SQL
statement can be any valid SQL statement and it is possible to use a dynamic where
clause using variable or parameters as we have seen previously.

Syntax
DECLARE <cursor_name> CURSOR FOR <select_statement>;

2. Open a Cursor

For opening a cursor we must use the open statement. If we want to fetch rows from
it, then you must have to open the cursor.

Syntax

Mr.Gawas Pradeep.A
Department Of Computer Science
R.B.M.College,Chandgad
OPEN <cursor_name>;

3. Fetch the Cursor

When we have to retrieve the next row from the cursor and move the cursor to the
next row then you need to fetch the cursor. If any row exists, then the below
statement fetches the next row and cursor pointer moves ahead to the next row.

Syntax
FETCH <cursor_name> INTO <variable_list>;

4. Close the Cursor

This statement closes the open cursor, and it will deactivate the cursor and release
the memory. By this statement we can close the previously opened cursor. If it is not
closed explicitly then a cursor is closed at the end of compound statement in which
that was declared.

Syntax
CLOSE <cursor_name>;

MySQL Cursor Life Cycle

The following diagram will show you the working of MySQL Cursors. Let’s see.

Mr.Gawas Pradeep.A
Department Of Computer Science
R.B.M.College,Chandgad
Note:
The cursor follows the same function as in programming. In programming, we use a
loop such as FOR, WHILE, or Do While to iterate over one component at a time.
MySQL Cursors Examples

In the following example first of all we have to declare the Cursor and select the all
records from a table, i.e., “GetVatsaCursor”. And after we open the cursor we fetch
the record one by one from cursor. And then insert these records in another table,
i.e., “Vbackupdata”.

Mr.Gawas Pradeep.A
Department Of Computer Science
R.B.M.College,Chandgad

You might also like