0% found this document useful (0 votes)
9 views40 pages

PHP Form Handling: GET vs POST Methods

The document provides an overview of storing script data in PHP, focusing on the $_GET and $_POST methods for processing forms, highlighting their differences in security and visibility. It also covers connecting to a MySQL database, executing queries, and managing session data in PHP applications. Key functions such as mysqli_connect(), mysqli_query(), and session management techniques are discussed to facilitate data handling and user tracking.

Uploaded by

Taylor
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)
9 views40 pages

PHP Form Handling: GET vs POST Methods

The document provides an overview of storing script data in PHP, focusing on the $_GET and $_POST methods for processing forms, highlighting their differences in security and visibility. It also covers connecting to a MySQL database, executing queries, and managing session data in PHP applications. Key functions such as mysqli_connect(), mysqli_query(), and session management techniques are discussed to facilitate data handling and user tracking.

Uploaded by

Taylor
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

INTECH 2201

LECTURE
1
Storing Script Data
$_GET & $_POST

2
Storing script data
▪ PHP variables are storage containers that
store information
▪ Examples:
▫ $alien_desc = ‘Little blue men’;
▫ $alien_desc = $_POST[‘alien_desc’];

3
Processing Forms
▪ There are two ways the browser client can
send information to the web server.
▪ The GET method.
▪ The POST method.

4
GET vs POST
Which is more secure 🤔
5
$_GET method
▪ Used to collect values in a form with
method=“get”.
▪ GET method is visible to everyone
▪ GET should NEVER be used for sending
passwords or other sensitive information!
$_POST method
▪ Used to collect values in a form with
method=“post”.
▪ POST method is invisible to everyone
▪ Developers prefer post for sending data 6
GET method
[Link] [Link]

7
GET method

When you click


the send
button, notice
the URL
contains the
username and
password

8
POST method
[Link] [Link]

9
POST method

When you click


the send
button, notice
the URL does
not contain
the username
and password

10
11
$_POST transports form data to script
▪ $_POST is a special variable that holds
form data
▪ $_POST is known as superglobal because it
is built into PHP and is available
throughout an entire script
▪ $_POST array is filled with the values
the user entered into the form.
▪ Each element corresponds to a piece of
data entered into a form field. 12
13
2
Setting up a database
connection PHP Forms and MySQL

14
Connecting to Database
▪ We use the method mysqli_connect();
▪ Before we can use it, we need to identify
the servername, username, password of the
MySQL database and the database name
▫ $servername = “localhost”;
▫ $username = “root”;
▫ $password = “”;
▫ $database = “learnDB”;

15
Connecting to Database
▪ mysqli_connect() accepts 4 parameters:
server name, username, password, and
database name
▪ Database name is optional, and can be
omitted
▫ mysqli_connect($servername,$username,$password)
//without database name
▫ mysqli_connect($servername,$username,$password,
$database) //with database name
16
Create a database

17
Connection with database name

18
Connection without database name

19
Testing the connection

We selected an existing We selected a not existing


database in the phpMyAdmin. database in the phpMyAdmin.
20
Selecting Database
▪ We used mysqli_select_db() to select the
preferred database.
▪ mysqli_select_db() accepts two
parameters, first is the database
connection, second is the database name.
▫ mysqli_select_db($connection,$database);

21
3
Querying the database
PHP Forms and MySQL

22
Executing Query
▪ When executing sql commands, we use
mysqli_query()
▪ mysqli_query() accepts two parameters,
first is the database connection, second
is the query or the sql command
▫ $query = “select * from personnel”;
▫ $result = mysqli_query($connection,$query);

23
Executing Query
▪ When an sql commands will not return a
value then we don’t need a variable that
will catch the result of mysqli_query()
▫ $query = “delete from students where id=1”;
▫ mysqli_query($connection,$query);

24
Outputting Query
▪ Output all row/data set using loop
▫ while($row = mysqli_fetch_array($result)){
echo”<div>”.$row[‘firstname’].”</div>;
echo”<div>”.$row[‘lastname’].”</div>;
echo”<div>”.$row[‘nickname’].”</div>;
echo”<div>”.$row[‘email’].”</div>;
echo”<div>”.$row[‘salary’].”</div>;
}

25
Outputting Query
▪ Get the number of rows
▫ $num_result = mysqli_num_rows($result);

▪ Output all row/data set using for loop


▫ for($i=0;$i<$num_results;$i++){
$row = mysqli_fetch_assoc($result);
echo”<div>”.$row[‘firstname’].”</div>;
echo”<div>”.$row[‘lastname’].”</div>;
}

26
Populate your database
Using the phpMyAdmin populate
atleast 1 information for
your personnel table

27
Display it
using PHP

28
Sample output

29
Closing Database
▪ To close a
database use
▫ mysqli_close(
$connection);

30
4
Sessions
PHP Forms and MySQL

31
What is a Session?
▪ A session is a global variable stored on
the server
▪ A special array used to store information
across the page requests, a user makes
during his/her visit to web application
▪ In a session based environment, every
user is identified through a unique
number called session identifier or SID
32
PHP Session Variables
▪ It allows user to store information on
the server for later use (e.g. username,
shopping items, etc.)
▪ Sessions work by creating a unique id
(UID) for each visitor and store
variables based in this UID
▪ Session variables tracks data for a user
while they travel through a series of web
pages
33
Starting a PHP Session

34
Storing and Accessing Session Data

35
Removing a Session
▪ The unset() function is used to free the
specified session variable.

36
Destroying a Session
▪ The session_destroy() function will reset
your session and will destroy all the
stored session data.

37
Reminder
▪ Every time you want to use session
variables or unset the session variables,
you need to start the session first
before accessing the variables.

38
References
▪ W3Schools (2022). PHP Tutorial.
[Link]
▪ w3resource (2020, February 26). PHP
Tutorials for beginners.
[Link]
[Link]

39
End
Any questions?
Email me at
[Link]@[Link]

40

You might also like