Professional Documents
Culture Documents
Introduction
Purpose of this lecture is to show how to build an online application using PHP/ MySQL platform This lecture is intended for developers who wish to develop web-based application but not familiar with PHP syntaxes
Outline
PHP basics MySQL connection Creation of accounts Managing logins Adding bid items Deleting bid items Logging out Viewing bid items Accepting bids Listing bids for each bid item
PHP basics
Background Web application architecture Basic syntax
<?php ?> tags Variables Operators
PHP basics-background
Server-side scripting language for the web PHP codes are embedded in HTML to dynamically generate web pages Originally stood for Personal Home Page
Changed to PHP Hypertext Processor
Database integration
MySQL, PostgreSQL, etc
Portability
Run on Linux, UNIX, Windows, MacOS platforms
A request is sent to the web server Web server locates the page and sends it back to the browser Browser displays the page
Application Layer
Some programs or scripts which processes the data PHP, JSP, Servlets, ASP,CGI, etc.
Database Layer
Provides the data that the application layer needs May be an RDBMS, a flat file, XML documents, etc.
?>
Tells web server where PHP code ends
Result
Returns true if $b is false and vice-versa Returns true if both $a and $b are true, otherwise false Returns true if either $a or $b is true, otherwise false Same as &&, but with lower precedence Same as ||, but with lower precedence
PHP basics-arrays
Arrays are special type of variables for storing a list of data and manipulating them efficiently An array stores multiple data items, divided into a number of slots
PHP basics-each()
Receives an array as a parameter Returns the current key and value pair as an array with four elements If the internal pointer for the array past the end of the array contents, each() returns FALSE Moves the array pointer to the next element Use reset() to move the array back at the first element
PHP basics-each() 2
PHP basics-list()
Used to assign multiple variables in one operation Usually used to capture values returned by each()
PHP basics-foreach()
Gives an easy way to iterate over arrays On sample code below:
For each loop, the value of the current element is assigned to $value Internal array pointer is advanced by one On the next loop, the next element is referenced
PHP basics-foreach() 2
On the sample code below:
The current elements key will be assigned to the variable $k on each loop
Statement if-else A statement (or block of code) will execute if a condition is true and a different statement if the condition is false
Where:
Expression1 is executed at the start; the counter is initialized here Condition is the test before each iteration Expression2 is evaluated at the end of each iteration Statement/s are executed once for each iteration
Statements inside the loop are executed for as long as the condition take the true value
Syntax: do{statement/s}while(condition)
Table tblaccount
This will hold the account info of bidders/ auctioneers Table structure
Column accountid: integer, primary key, autoincrement Column username: string 50 chars Column password: string 50 chars
Table tblbiditems
This will hold the items auctioned for bidding Table structure
Column biditemid: integer , primary key, autoincrement Column accountid: string 50 chars
This identifies the auctioneer
Table tblbidhistory
This will hold the bid info for each item being auctioned Table structure
Column bidhistoryid: integer , primary key, auto-increment Column accountid: integer Column biditemid: integer Column bidprice: double Column dtesubmitted: datetime
Connecting to databases:
Function mysql_connect:
Creates a connection to MySQL Syntax: mysql_connect($hostname, $username,$password) Ex: $conn=mysql_connect(localhost, root,password)
Function mysql_select_db
Specifies the database in MySQL for use Syntax: mysql_select_db($database, $connection) Ex: mysql_select_db(auction, $conn)
Function die
Terminates execution of PHP script
Creation of accounts
HTML form handling MySQL commands
Function mysql_query() Function mysql_error()
Adding records
SQL insert statement
File index.html
First page that displays Provide the user with the option to create accounts
File addaccount.html
Displays a form for accepting new account info
File addaccountprocess.php
$_POST array
Special arrays that hold all form variables
Function mysql_query()
Executes an SQL statement on the database
Function mysql_error()
Displays error encountered when executing an SQL statement
SQL Insert
Adds a record on a database table
Create accounts:
Username: auctioneer1
This account will place items for bidding
Managing logins
SQL select statement Function mysql_num_rows Function isset() Session URL rewriting
Querystring $_GET array
Create:
File login.php File loginverify.php File checkstatus.inc File menu.php
Function mysql_num_rows
Retrieves the number of rows from a result set Can only be used for SQL select statements
Function isset()
Checks if a variable exist Example: isset($name)
This check if the variable $name exist
Sessions
Special variables stored in web servers Allows passing of information between web pages Call the function session_start() at the start of scripts that will use sessions
URL Rewriting
Querystring
Information can be passed on by appending variable/value to the URL
$_GET array
Special array that holds all querystring values
File menu.php
File menu.php
File addauctionprocess.php
Function mysql_fetch_array()
Fetches a row as an associative from a select query result set
File menu.php
File listauctionitems.php
File deletebiditem.php
Loggin out
Function session_destroy() Create:
File logout.php
Function session_destroy()
Terminates all session variables stored in server memory
File menu.php
File logout.php
Once logout.php is called, all session variable will be dropped from server memory Browser will not be able to access any page calling checkverify.php (ex: menu.php)
Establishing relations
Table tblbiditem
Holds the items being auctioned off Column accountid identifies the owner if the auctioned item
Table tblaccount
Holds account information of the owner of the item being auctioned
Column accountid
Links the owner of the account to the auction item
File menu.php
File listbiditems.php
All items with their respective owners being auction are listed Each item will hyperlink to a PHP page for accepting bids
Accepting bids will be covered in the next topic section
Each hyperlink will append a querystring variable to identify it in the PHP page for accepting bids
Accepting bids
Using hidden fields to store ID numbers MySQL now() function Create:
File acceptbid.php File acceptbidprocess.php
Hidden fields
Not displayed to the browser Used to pass constant values
File acceptbid.php
Place the id of the auction item in a hidden field
File acceptbidprocess.php
Resulting records