Lec 7
Lec 7
Lec 7
1
Semester Projects
Assignment No#4
1. Introduction
2. Problem Statement
3. Problem Solution for proposed system
4. Related system analysis/Literature review
5. Functional and non-functional requirements
6. Advantages of system
7. Scope
8. Module
9. System Limitations
10. Methodology
11. Tools and technologies
12. Data Gathering Approach
13. Mockups
14. Conclusion
15. References
2
GET and POST Methods
8
PHP
• Why we use PHP?
We use PHP to create dynamic web pages/sites
• PHP runs on various platforms (Windows, Linux,
Unix, Mac OS X, etc.)
• PHP is compatible with almost all servers used
today (Apache, IIS, etc.)
• PHP supports a wide range of databases
9
PHP
• What do i need to run PHP pages
– Install a web server on your own PC and then install PHP
& MYSQL
– WAMP: Window, Apache, MYSQL, PHP
– XAMPP: All OS, Apache, MYSQL, PHP, Perl
Note: Install the web server start as services & then
type localhost in your browser to check whether it
is running or not
– LAMP: used for Linux Machines users
10
Editor
• Sublime Text Editor
• Notepad ++
• Notepad
11
PHP Syntax
Recommended
• PHP Tags: tags
1 2 3
Standard Tags Short Tags (Not supported Script Tags
<? Php by all web servers) <script language=”php<“
...code <? ...Code
?> .....Code </script>
?>
4
ASP Tags
<%
...code
%>
12
PHP
• MY First PHP Page: Open notepad and type the following code
<?php
Echo ”Welcome to PHP Class”;
?>
• Save it with php extension e.g. First.php
• How to run this?
– Open any web browser and type
– http://localhost/first.php
– The output will be
Welcome to PHP Class
https://www.youtube.com/watch?v=at19OmH2Bg4&list=PLu0W_9lII9aikXkRE0
WxDt1vozo3hnmtR 13
What is MySQL?
• MySQL is the most popular open-source
database system.
• The data in MySQL is stored in database
objects called tables.
• A table is a collections of related data
entries and it consists of columns and
rows.
14
Connection to MySQL Database
• Syntax
• In PHP, this is done with the mysqli_connect()
function.
• mysqli_connect(servername ,username,password,dbname );
15
Connection to MySQL Database
<?php
$link = mysqli_connect("localhost", "root", "", "dbname");
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
echo "Connect Successfully" ;
mysqli_close($link);
?>
16
Create Database
• Syntax
CREATE DATABASE database_name
<?php
$link = mysqli_connect("localhost", "root", "","demo");
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
19
PHP MySQL Insertion
<?php
$link = mysqli_connect("localhost", "root", "","demo");
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$sql = "INSERT INTO persons (id, fname, lname, email) VALUES (1, 'P1', 'P2',
‘[email protected]'), (2, 'P3', 'P4', ‘[email protected]')";
if(mysqli_query($link, $sql)){
echo "Records inserted successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
mysqli_close($link);
?>
20
PHP MySQL Selection
The SELECT statement is used to select data from one or more tables:
• Syntax:
SELECT column1_name, column2_name FROM table_name;
• Syntax:
or we can use the * character to select ALL columns from a table:
• SELECT * FROM table_name
• Syntax:
• SELECT column_name(s) FROM table_name WHERE column_name
operator value
21
<?php
PHP MySQL Selection echo "<tr>";
$link = mysqli_connect("localhost", "root", "", echo "<td>" . $row['id'] . "</td>";
"demo"); echo "<td>" . $row['fname'] . "</td>";
if($link === false) echo "<td>" . $row[‘lname'] . "</td>";
{ echo "<td>" . $row['email'] . "</td>";
die("ERROR: Could not connect. " . echo "</tr>"; }
mysqli_connect_error()); } echo "</table>";
$sql = "SELECT * FROM persons"; mysqli_free_result($result);
If }
($result = mysqli_query($link, $sql)) else
{ {
if(mysqli_num_rows($result) > 0) echo "No records found.";
{ }
echo "<table>"; }
echo "<tr>"; Else
echo "<th>id</th>"; {
echo "<th>fname</th>"; echo "ERROR: Could not able to execute $sql. " .
echo "<th>lname</th>"; mysqli_error($link);
echo "<th>email</th>"; }
echo "</tr>"; mysqli_close($link);
while($row = mysqli_fetch_array($result))
{ ?>
22
MYSQL Update Statement
• Used to update/modify records in a table
Syntax:
UPDATE Table_Name SET Column1=value1,
Column2=value2,...
where Some_Value=some_value;
23
Example
<?php
$link = mysqli_connect("localhost", "root", "","demo");
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$sql = "UPDATE persons SET email='[email protected]' WHERE id=1";
if(mysqli_query($link, $sql)){
echo "Records are updated successfully.";
} else {
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
mysqli_close($link);
?>
24
MySQL Delete Statement
• Delete record from Database
Syntax:
DELETE FROM Table_name where some_Col=
some_value
25
Example
<?php
$link = mysqli_connect("localhost", "root", "","demo");
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$sql = "DELETE FROM persons WHERE id='1'";
if(mysqli_query($link, $sql)){
echo "Records were deleted successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
mysqli_close($link);
?>
26
PHP Session
// Starting session
session_start();
?>
29
Storing and Accessing Session Data
<?php
// Starting session
session_start();
// Storing session data
$_SESSION["firstname"] = "Web";
$_SESSION["lastname"] = "Enginering";
// Accessing session data
echo $_SESSION["firstname"];
echo $_SESSION["lastname"];
?> 30
Accessing in different pages
Page1.php Page2.php
<?php <?php
// Starting session // Starting session
session_start(); session_start();
echo $_SESSION["firstname"]; echo $_SESSION["firstname"]
echo $_SESSION["lastname"]; echo $_SESSION["lastname"];
?> ?>
31
Destroying a Session
• To remove certain session data, simply unset the corresponding key
of the $_SESSION associative array
<?php
// Starting session
session_start();
// Removing session data
if(isset($_SESSION["lastname"]))
{
unset($_SESSION["lastname"]);
}
32
Destroying a Session
• To destroy a session completely, simply call the
session_destroy();
<?php
// Starting session
session_start();
// Destroying session
session_destroy();
33