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

Scripting Languages-Lecture 3

The document outlines a course on scripting languages with a focus on PHP, including a simple project requiring the creation of a CRUD application and form handling. It details the use of PHP superglobals for form data collection, attributes of HTML forms, and steps for creating a database using phpMyAdmin. Additionally, it provides code examples for connecting to a database and inserting data, along with class activities for practical application.

Uploaded by

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

Scripting Languages-Lecture 3

The document outlines a course on scripting languages with a focus on PHP, including a simple project requiring the creation of a CRUD application and form handling. It details the use of PHP superglobals for form data collection, attributes of HTML forms, and steps for creating a database using phpMyAdmin. Additionally, it provides code examples for connecting to a database and inserting data, along with class activities for practical application.

Uploaded by

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

SCRIPTING

LANGUAGES
COURSE CODE: CSSD 204
COURSE LECTURER: KINGSLEY DRAH
GOALS OF THE LECTURE
Simple PhP Project (Submission Next 2-weeks Thursday, 11:59pm)
•PHP Form Handling
•Create a simple CRUD application,
connect to a database using
phpMyAdmin.
•Making the html file, php file and the database
connections
•Insert Data into a database
PHP - A Simple HTML Form
The PHP superglobals $_GET and $_POST are used to collect form-data.
Example. index.php file
<html>
<body>

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


Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>

</body>
</html>
HTML Form Attributes
(1) The action Attribute
• The action attribute defines the action to be performed when the
form is submitted. Usually, the form data is sent to a file on the server
when the user clicks on the submit button.
• In the example below, the form data is sent to a file called
"action_page.php". This file contains a server-side script that handles
the form data:
<form action="action_page.php">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
HTML Form Attributes
(2) The method Attribute
• The method attribute specifies the HTTP method to be used when submitting
the form data. he form-data can be sent as URL variables (with
method=“get”) or as HTTP post transaction (with method=“post” ). The
default HTTP method when submitting form data is GET
• In the example below, the form data is sent to a file called "action_page.php".
This file contains a server-side script that handles the form data:
<form action="action_page.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>

Hint: With the get method, after you submit, notice that the form values is
visible in the address bar of the new browser tab whilst post doesn’t.
Cont…
Notes on GET:
• Appends the form data to the URL, in name/value pairs
• NEVER use GET to send sensitive data! (the submitted form data is visible in
the URL!)
• The length of a URL is limited (2048 characters)
• Useful for form submissions where a user wants to bookmark the result
• GET is good for non-secure data, like query strings in Google
Notes on POST:
• Appends the form data inside the body of the HTTP request (the submitted
form data is not shown in the URL)
• POST has no size limitations, and can be used to send large amounts of data.
• Form submissions with POST cannot be bookmarked
HTML Form Attributes
(3) The name Attribute
• The name attribute specifies a name for an HTML element.
• For a <form> element, the name attribute is used as a reference when
the data is submitted.

<form action="action_page.php" method="post"


>
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
Create a simple database using phpmyadmin
In this section, you will learn how to create a database with phpMyAdmin.

Step 1: First, select database on the left side of the dashboard.

Step 2: Input your database name and

Step 3: Click on create.


See how

Step 1

Step 3
Step 2: input
database name
Create a simple database using phpmyadmin
In this section, you will learn how to create a database table with phpMyAdmin.
1) First, we’ll need to select the database, in this example, we’ll use
the demo_database database.
2) You’ll notice that we have a couple of test tables already created, to create a new database
table just enter the name of the table in the “Name” field and the number of fields you want for
this table and click “Go”.
3) We’ve created the table “demo” and added two columns (name and phone), now we need to
set the details of each field within this new table and select Type of data the column will hold.
Some of the most common types are:
• INT = INTEGER – used to store a number that can be written without a fractional component.
(e.g. 2, 10, -44). An integer can be zero, positive, and negative.
CHAR = Characters – used to store character string value of fixed length, the maximum number
is of characters is 255.
VARCHAR = Variable Length Characters – used to store variable length alphanumeric data and
can store up to 65,535 characters.
TEXT = used for holding large amounts of text.
DATE = Will only hold dates.
DATETIME = Will hold both a date and a time.
See how

Step 1
Step 2: enter Step 3: specify no.
table name of columns to be
Step 4
used
Enter column names and attributes
Let’s link the database to our php file
1. First, set connection parameters

$servername = "localhost”;
$database = “personal_database”;
$username = “root”;
$password = “ ”;
Let’s link the database to our php file
2. Create a connection to the database

$conn = mysqli_connect($servername, $username,


$password, $database);
Let’s link the database to our php file
3. Check connection

if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
Let’s link the database to our php file
4. Make SQL insert statement

$sql = "INSERT INTO persons (name, email) VALUES ('', '$name',


'$email')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
Let’s link the database to our php file
5. Close connection

mysqli_close($conn);
Class Activities
<!DOCTYPE html>
<html>
<body>
<?php
echo “Class activity;

echo “create a form that accepts the following details of students; full
name, index number, email address, phone number, course code and
course name and insert the data into a database";

echo “HaPpY cOdiNg";


?>
</body>
</html>

You might also like