0% found this document useful (0 votes)
1 views10 pages

ICT245 Web Programming PHP Lectures Part II

The document is a set of lecture slides on Web Programming with a focus on PHP, covering topics such as connecting to MySQL databases, executing SQL statements, and handling file uploads. It includes code examples and best practices for combining PHP with HTML to create dynamic web applications. The lecture aims to equip students with the skills to perform CRUD operations and effectively manage file uploads in PHP.
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)
1 views10 pages

ICT245 Web Programming PHP Lectures Part II

The document is a set of lecture slides on Web Programming with a focus on PHP, covering topics such as connecting to MySQL databases, executing SQL statements, and handling file uploads. It includes code examples and best practices for combining PHP with HTML to create dynamic web applications. The lecture aims to equip students with the skills to perform CRUD operations and effectively manage file uploads in PHP.
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
You are on page 1/ 10

07-Aug-24

ICT245
Web Programming
Introduction to PHP

Web Technologies Lecture Slides by : Maxwell Dorgbefu Jnr.

Silence Hour
Visit
https://www.w3schools.com/php
/php_mysql_connect.asp
Read for 25 minutes.

Web Technologies Lecture Slides by : Maxwell Dorgbefu Jnr.

1
07-Aug-24

Today’s Lecture
2.0
1.0
Performing
Connecting
CRUD
to MySQL
operations on
Database
MySQL
using PHP
Database using
PHP

3.0
4.0
Designing Combining
HTML PHP and HTML
forms for in a single
file uploads document
Web Technologies Lecture Slides by : Maxwell Dorgbefu Jnr.

Objectives of the Lecture


• By the end of the lecture, the student will
be able to:
– Connect to MySQL database and select a
database using PHP scripts.
– Use mysqli functions to process SQL
statements in PHP.
– Write PHP codes to accept and process files
uploaded by users.
– Combine PHP and HTML effectively in a single
document.

Web Technologies Lecture Slides by : Maxwell Dorgbefu Jnr.

2
07-Aug-24

Connecting to MySQL Database


Server from PHP
Currently, connecting to MySQL database is
achieved by using either mysqli or PDO
functions.
In this lecture we focus on mysqli
functions.
The connection parameter to the database
serves as a “bridge” created at the business
logic layer, between the presentation layer
(UI) and the data layer in a typical 3-tier
web application.
Web Technologies Lecture Slides by : Maxwell Dorgbefu Jnr.

Parameters for Connecting to MySQL Database


Server from PHP
 Establishing a connection to MySQL database in
PHP needs the following parameters:
$dbHost --- Is the name or the IP address of
the database server.
$dbUser --- Is an authorized username that
has the privilege to connect to the database
server.
$dbPassword --- Is the password
corresponding to the specified username.
$dbName --- The name of the preferred
database on the database server
Web Technologies Lecture Slides by : Maxwell Dorgbefu Jnr.

3
07-Aug-24

Connecting to MySQL Database Server and


Selecting a Database Example
<?php
// Database connection parameters
$dbHost = 'localhost';
$dbUser = 'root';
$dbPassword = ‘mydbpassword';
$dbName = 'petstore';

// Creating the connection


$dbConn = new mysqli($dbHost,
$dbUser, $dbPassword, $dbName);
// Checking the connection
if ($dbConn->connect_error) {
die("Connection failed: " . $dbConn->connect_error);
}
echo "Connected successfully";
?>
Web Technologies Lecture Slides by : Maxwell Dorgbefu Jnr.

Closing the Connection to MySQL


Database Server.
The connection opened to the database in
a PHP script will be closed as soon as the
execution of the script ends.
It is however, advisable to do so explicitly
with the syntax; $dbConn->close();
Where $dbConn is the instance of an
opened connection (the “bridge”) to
MySQL database server.

Web Technologies Lecture Slides by : Maxwell Dorgbefu Jnr.

4
07-Aug-24

Executing Embedded SQL Statements in


PHP
• To execute embedded SQL statements on MySQL database
using PHP we go by the syntax below:
$queryResult = $dbConn ->query($sql_statement);
• Where $sql_statement is the SQL Statement you intend to
execute, and $queryResult is a variable or resource that
holds the results of the SQL statement you executed.
<?php
if ($dbConn->query($sql_statement) === TRUE) {
echo “Message upon successful execution of SQL
Statement";
} else {
echo “Error message: " . $dbConn->error;
}
$dbConn->close();
?> Web Technologies Lecture Slides by : Maxwell Dorgbefu Jnr.

The query( ) and fetch_object( ) Functions


• The fetch_object( ) fuction takes the result set returned
by the query( ) function as its parameter. E.g: $resultSet-
>fetch_object();
• In order to display the contents of the result set to users,
developers mostly use the while loop and create their
own record set to which they assign the contents of the
result set for onward display.
$resultSet = $dbConn->query(“SELECT * FROM $dbTable”);
while($recordSet = $resultSet->fetch_object()){
echo $recordSet->fieldname_1;
echo $recordSet->fieldname_2;
echo $recordSet->fieldname_3;
echo $recordSet->fieldname_n;
} Web Technologies Lecture Slides by : Maxwell Dorgbefu Jnr.

5
07-Aug-24

Creating Sample Tables using SQL


$sql_db = CREATE DATABASE IF NOT EXISTS staffDB;

$sql1 = “CREATE TABLE IF NOT EXISTS department(DeptID


VARCHAR(10), DeptName VARCHAR(25), Phone VARCHAR(21),
PRIMARY KEY(DeptID))”;

$sql2 = “CREATE TABLE IF NOT EXISTS staff(


StaffID VARCHAR(10), FullName VARCHAR(25),
Gender VARCHAR(6), Phone VARCHAR(21), Email VARCHAR(25),
DeptID VARCHAR(10),
PRIMARY KEY(StaffID),
CONSTRAINT staff_fk1 FOREIGN KEY(DeptID) REFERENCES REFERENCES
department(DeptID) ON UPDATE CASCADE ON DELETE NO ACTION)”;

Web Technologies Lecture Slides by : Maxwell Dorgbefu Jnr.

Executing a SELECT Statement


<?php
include(“dbConnFile.php”); // Including Database connection file.
$sql_staff =“SELECT * FROM staff”;
$selectResult = $dbConn->query($sql_staff);
while($staff_row = $selectResult->fetch_object())
{
echo “StaffID:”. $staff_row->StaffID;
echo “Staff Name: ”. $staff_row->FullName;
echo “Gender:”. $staff_row->Gender;
echo “Department:”. $staff_row->DeptID;
echo “ <br />”;
}
?> Web Technologies Lecture Slides by : Maxwell Dorgbefu Jnr.

6
07-Aug-24

File Uploads in PHP - 1


 With PHP, it is possible to upload files to the server.
 To allow users to upload files from a form can be very useful.
 Example
<html>
<body>
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<input type="file" name="file" id="file" /> <br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>

Web Technologies Lecture Slides by : Maxwell Dorgbefu Jnr.

File Uploads in PHP - 2


 Notice the following about the HTML form above:
 The enctype attribute of the <form> tag specifies which
content-type to use when submitting the form.
 "multipart/form-data" is used when a form requires
binary data, like the contents of a file, to be uploaded.
 The type="file" attribute of the <input /> tag specifies
that the input should be processed as a file.
 For example, when viewed in a browser, there will be a
Browse/Choose file button next to the input field.
 Note: Allowing users to upload files is a big security risk. Permit
only trusted users to perform file uploads to your
server, and remember to validate every input including
file uploads.

Web Technologies Lecture Slides by : Maxwell Dorgbefu Jnr.

7
07-Aug-24

A Sample File Upload Script in PHP


 The "upload_file.php" file contains the code for uploading a file:
<?php

if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb <br />";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}

?>

Web Technologies Lecture Slides by : Maxwell Dorgbefu Jnr.

Moving the File


$fileToMove = $_FILES['file1']['tmp_name'];
$destination = "./upload/" . $_FILES["file1"]["name"];
if (move_uploaded_file($fileToMove, $destination))
{
echo "The file was uploaded and moved
successfully!";
}
else {
echo "There was a problem moving the file.";
}

Web Technologies Lecture Slides by : Maxwell Dorgbefu Jnr.

8
07-Aug-24

Explanation of the File Upload Script


 The first parameter is the form's input name and the second
index can be either "name", "type", "size", "tmp_name” or
"error". Like this:
 $_FILES["file"]["name"] - the name of the uploaded file
 $_FILES["file"]["type"] - the type of the uploaded file
 $_FILES["file"]["size"] - the size in bytes of the uploaded file
 $_FILES["file"]["tmp_name"] - the name of the temporary copy
of the file stored on the server.
 $_FILES["file"]["error"] - the error code resulting from the file
upload.
 For security reasons, you should add restrictions on what the
user is allowed to upload.

Web Technologies Lecture Slides by : Maxwell Dorgbefu Jnr.

Combining PHP and HTML


 PHP and HTML can be combined to produce very
efficient dynamic web sites or applications.
 In the combination , the HTML document
structure is mostly declared once within the
normal <html> …</html> and <body>…
</body> tags.
 The PHP delimiters <?php… ?> can be used
within the page as many times as possible.
 There is no set order that PHP codes should go
into the HTML codes and vice versa, it all depends
on the logic the developer wishes to implement.

Web Technologies Lecture Slides by : Maxwell Dorgbefu Jnr.

9
07-Aug-24

Displaying the Results of SQL Queries in


HTML Tables.
<!DOCTYPE html>
<html>
<head> <title>PHP and HTML Combination!</title> </head>
<body bgcolor = “#D2C2C2”>
<?php
include(“includes/connection.php”); //Connection is included here from the includes folder.
$sql=“SELECT * FROM staff”;
$result = $dbConn->query($sql);
?>
<table border = “3” width = “45%” align = “center” style=“border-collapse:collapse;”>
<tr><td colspan = “4”>Our Cherished Staff - 2022</td> </tr>
<tr> <th>StaffID</th> <th>Full Name</th> <th>Gender</th> <th>Department</th> </tr>
<?php
while($staff_row = $result->fetch_object())
{ ?>
<tr>
<td><?php echo $staff_row->StaffID; ?></td>
<td><?php echo $staff_row->FullName; ?></td>
<td><?php echo $staff_row->Gender; ?></td>
<td><?php echo $staff_row->DeptID; ?></td>
</tr>
<?php } ?> Web Technologies Lecture Slides by : Maxwell Dorgbefu Jnr.
</body> </html>

Exercise
• By using PHP and Bootstrap, produce the 2 and
5 times table from 1 to 50 for your younger
siblings at the basic levels of education.

THE 2 TIMES TABLE THE 5 TIMES TABLE


2 X 1 = 2 5 X 1 = 5
2 X 2 = 4 5 X 2 = 10

2 X 50 = 100 5 X 50 = 250
Web Technologies Lecture Slides by : Maxwell Dorgbefu Jnr.

10

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy