Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
33 views

Lesson 10 PHP-Connection With Database

This document discusses connecting PHP to databases. It covers HTML form requests using GET and POST methods. It then demonstrates creating a login form with validation in PHP and MySQL. It shows how to create a registration form, store the data in a database table, and fetch the stored data. The key steps are creating databases and tables in PHPMyAdmin, building HTML forms, and using PHP to connect to the database and execute SQL queries to insert, select, and display data.

Uploaded by

1045858
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Lesson 10 PHP-Connection With Database

This document discusses connecting PHP to databases. It covers HTML form requests using GET and POST methods. It then demonstrates creating a login form with validation in PHP and MySQL. It shows how to create a registration form, store the data in a database table, and fetch the stored data. The key steps are creating databases and tables in PHPMyAdmin, building HTML forms, and using PHP to connect to the database and execute SQL queries to insert, select, and display data.

Uploaded by

1045858
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Web Design and Development Lesson 10

Lesson 10: PHP –Connecting with Database

Lesson 10: PHP –Connecting with Database ................................................................................. 1


10.1 HTML FORM Requests ..................................................................................................... 2
10.1.1. GET Method .............................................................................................................. 2
10.1.2. POST Method ............................................................................................................ 3
10.2 Login Form with Validation in PHP and MYSQL ............................................................. 3
10.3 PHP FORM Registration using SQL .................................................................................. 5
10.3.1. Creating Database ..................................................................................................... 5
10.3.2. Creating Registration Form ....................................................................................... 6
10.3.3. Storing data in the database ..................................................................................... 7
10.3.4. Fetching data from the database.............................................................................. 8
Lesson 10 Revision Question ...................................................................................................... 9

Compiled by: Karari E.K email: cats.mwalimu@gmail.com 1


Web Design and Development Lesson 10

10.1 HTML FORM Requests


The most used HTTP request types are GET and POST, but there are other types in their
technical specification, such as PUT, HEAD, DELETE, PATCH and OPTIONS. We will
focus only on the two most common.
• The GET Method
• The POST Method
Before the browser sends the information, it encodes it using a scheme called URL
encoding. In this scheme, name/value pairs are joined with equal signs and different
pairs are separated by the ampersand.
name1=value1&name2=value2&name3=value3
Spaces are removed and replaced with the + character and any other nonalphanumeric
characters are replaced with a hexadecimal value. After the information is encoded it is
sent to the server.
10.1.1. GET Method
The GET method sends the encoded user information appended to the page request. The page
and the encoded information are separated by the ? character.
http://www.test.com/index.htm?name1=value1&name2=value2
The GET method produces a long string that appears in your server logs, in the browser's
Location: box.
• The GET method is restricted to send up to 1024 (8 KB, or 8192 bytes ) characters only. in URI
• Never use GET method if you have password or other sensitive information to be sent to the
server.
• GET can't be used to send binary data, like images or word documents, to the server.
• The data sent by GET method can be accessed using QUERY_STRING environment variable.
• The PHP provides $_GET associative array to access all the sent information using GET
method.

Compiled by: Karari E.K email: cats.mwalimu@gmail.com 2


Web Design and Development Lesson 10

10.1.2. POST Method


The POST method transfers information via HTTP headers. The information is encoded as
described in case of GET method and put into a header called QUERY_STRING.
• 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 (https) you can make sure that your information is secure.
• The PHP provides $_POST associative array to access all the sent information using GET
method.

10.2 Login Form with Validation in PHP and MYSQL


Step 1: create database using XAMPP PHP admin. Enter database name as login then click
create button.

Step 2. Create table called user.

Step 3 Create table fields.

Create path >Xampp>htdocs> login folder.

Next step save all table fields inside login folder > open Notepad++ >save as login.php inside
login folder.

Compiled by: Karari E.K email: cats.mwalimu@gmail.com 3


Web Design and Development Lesson 10

login.php file

Process.php file

Compiled by: Karari E.K email: cats.mwalimu@gmail.com 4


Web Design and Development Lesson 10

Successful login

10.3 PHP FORM Registration using SQL


10.3.1. Creating Database
Step 1: Create the database to use. command – create database school;
Step 2: Select the database to use. command – use school;
Step 3: Create a table named students-

Compiled by: Karari E.K email: cats.mwalimu@gmail.com 5


Web Design and Development Lesson 10

10.3.2. Creating Registration Form


Here, we will create a form and store the data collected from it in our school database.
We will Create two files, index.php and connect.php.
The file index.php will carry the form HTML code, while connect.php connects the
form with the database.

Compiled by: Karari E.K email: cats.mwalimu@gmail.com 6


Web Design and Development Lesson 10

10.3.3. Storing data in the database

➢ Open index.php >fill in the registration form


➢ After filling the form >click login then if results are successful Record saved
When we open http://localhost/phpmyadmin/ on the browser we should have something like
this in our database.

Compiled by: Karari E.K email: cats.mwalimu@gmail.com 7


Web Design and Development Lesson 10

10.3.4. Fetching data from the database


To fetch the data that we just saved, add the code below into index.php right under the
</form> tag.

Compiled by: Karari E.K email: cats.mwalimu@gmail.com 8


Web Design and Development Lesson 10

On refreshing the page, we should see (The data at the bottom of the page has been fetched
from the database):

Lesson 10 Revision Question


a) Write a PHP code to connect a database named Registration in a XAMPP database
server.

Compiled by: Karari E.K email: cats.mwalimu@gmail.com 9

You might also like