widhy hayuhardhika np, s.kom. overview of database structure connecting to mysql database selecting...

72
Widhy Hayuhardhika NP, S.Kom Building an online bidding application using PHP/MySQL

Upload: cristobal-regan

Post on 28-Mar-2015

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

Widhy Hayuhardhika NP, S.Kom

Building an online bidding application using

PHP/MySQL

Page 2: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

Outline Topic #1 MySQL Connection

Page 3: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

Overview of database structureConnecting to MySQL databaseSelecting the database to useUsing the require_once statement

MySQL Database Connection

Page 4: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

Database: auctionTables

tblaccounttblbiditemstblbidhistory

Overview of Database connection

Page 5: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

This will hold the account info of bidders/ auctioneers

Table structureColumn accountid: integer, primary key, auto-

incrementColumn username: string 50 charsColumn password: string 50 chars

Table tblaccount

Page 6: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

This will hold the items auctioned for biddingTable structure

Column biditemid: integer , primary key, auto-increment

Column accountid: string 50 charsThis identifies the auctioneer

Column biditem: string 50 charsColumn biddesc: tiny text

Table tblbiditems

Page 7: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

This will hold the bid info for each item being auctioned

Table structureColumn bidhistoryid: integer , primary key,

auto-incrementColumn accountid: integerColumn biditemid: integerColumn bidprice: doubleColumn dtesubmitted: datetime

Table tblbidhistory

Page 8: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

Function mysql_connect:Creates a connection to MySQLSyntax: mysql_connect($hostname, $username,

$password)Ex: $conn=mysql_connect(“localhost”,

“root”,”password”)Function mysql_select_db

Specifies the database in MySQL for useSyntax: mysql_select_db($database,

$connection)Ex: mysql_select_db(“auction”, $conn)

Function dieTerminates execution of PHP script

Connecting to databases:

Page 9: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

Create file dbconnect.incFor code reuse, a separate file can be created

to connect to the databasePHP pages can call dbconnect.inc to connect

yo the auction database

Connecting to MySQL and selecting auction database

Page 10: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

Function require_once()Loads a file into a PHP script

Reusing the database connection

Page 11: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

Outline Topic #2 Creation of Accounts

Page 12: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

HTML form handlingMySQL commands

Function mysql_query()Function mysql_error()

Adding recordsSQL insert statement

Creation of accounts

Page 13: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

Create:File index.htmlFile addaccount.htmlFile addaccountprocess.php

$_POST array

HTML form handling

Page 14: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

First page that displaysProvide the user with the option to create

accounts

File index.html

Page 15: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

Displays a form for accepting new account info

File addaccount.html

Page 16: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

$_POST arraySpecial 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 statementSQL Insert

Adds a record on a database table

File addaccountprocess.php

Page 17: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

File addaccountprocess.php script

Page 18: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

Username: auctioneer1This account will place items for bidding

Usernames: bidder1, bidder2These account will bid for item auctioned off

Create accounts:

Page 19: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

Outline Topic #3 Managing Logins

Page 20: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

SQL select statementFunction mysql_num_rowsFunction isset()SessionURL rewriting

Querystring $_GET array

Create: File login.php File loginverify.php File checkstatus.inc File menu.php

Managing logins

Page 21: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

Example 1: select * from tblaccount Selects all columns/ rows from table tblaccount

Example 2: select username, password from tblaccount Selects columns username and password for all rows in

table tblaccountExample 3: select * from tblaccount where

username=‘jundolor’ Selects all columns from table tblaccount for all rows

whose column username contains ‘jundolor’Example 4: select accountid from tblaccount

where username=‘media’ Selects column accountid from tblaccount for all rows

whose column username contains ‘media’

SQL select statement

Page 22: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

Retrieves the number of rows from a result set

Can only be used for SQL select statements

Function mysql_num_rows

Page 23: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

Checks if a variable existExample: isset($name)

This check if the variable $name exist

Function isset()

Page 24: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

Special variables stored in web serversAllows passing of information between web

pagesCall the function session_start() at the start of

scripts that will use sessions

Sessions

Page 25: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

QuerystringInformation can be passed on by appending

variable/value to the URL

$_GET arraySpecial array that holds all querystring values

URL Rewriting

Page 26: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

File login.php code

Page 27: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

File login.php browser shot

Page 28: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

File loginverify.php code

Page 29: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

File checkstatus.inc code

Page 30: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

File menu.php

Page 31: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

Outline Topic #4 Adding Items to Auction

Page 32: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

File menu.phpCreate:

File addauctionitem.phpFile addauctionitemprocess.php

Adding items to auction

Page 33: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

File menu.php

Page 34: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

File addauctionitem.php code

Page 35: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

File addauctionitem.php screen shot

Page 36: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

File addauctionprocess.php

Page 37: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

Outline Topic #5 Deleting Bid Items

Page 38: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

Function mysql_fetch_array()Writing querystring URL to identify records

to deleteSQL delete statementCreate:

File listauctionitems.phpFile: deletebiditem.php

Deleting Bid Items

Page 39: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

Fetches a row as an associative from a select query result set

Function mysql_fetch_array()

Page 40: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

Sample mysql_fetch_array() code

Page 41: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

Auction items belonging to current account will be selected

A loop will be created to go through each rowEach row will hyperlink to a PHP based page

for deletionTo identify the row, a querystring variable will

be appended to the URL

Writing querystring URLto identify records to delete

Page 42: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

Writing querystring URLto identify records to delete- code

Page 43: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

Example 1: delete from tblaccountDeletes all rows on table tblaccount

Example 2: delete from tblaccount where accountid=1Deletes only rows matching the condition

SQL delete statement

Page 44: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

File menu.php

Page 45: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

File listauctionitems.php

Page 46: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

File deletebiditem.php

Page 47: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

Outline Topic #6 Logging Out

Page 48: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

Function session_destroy()Create:

File logout.php

Loggin out

Page 49: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

Terminates all session variables stored in server memory

Function session_destroy()

Page 50: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

File menu.php

Page 51: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

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)

File logout.php

Page 52: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

Outline Topic #7 Viewing Bid Items

Page 53: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

Establishing relations between tablesSQL natural join clauseCreate:

File listbiditems.php

Viewing bid items

Page 54: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

Establishing relationsTable 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

Page 55: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

Used with SQL select statementConnects rows between different tables via

their common column

SQL natural join clause

Page 56: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

File menu.php

Page 57: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

All items with their respective owners being auction are listed

Each item will hyperlink to a PHP page for accepting bidsAccepting bids will be covered in the next topic

sectionEach hyperlink will append a querystring

variable to identify it in the PHP page for accepting bids

File listbiditems.php

Page 58: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

File listbiditems.php code

Page 59: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

File listbiditems.php screen shot

Page 60: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

Outline Topic #7 Accepting Bids

Page 61: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

Using hidden fields to store ID numbersMySQL now() functionCreate:

File acceptbid.phpFile acceptbidprocess.php

Accepting bids

Page 62: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

Not displayed to the browserUsed to pass constant values

Hidden fields

Page 63: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

Place the id of the auction item in a hidden fieldFile acceptbid.php

Page 64: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

File acceptbid.php screen shot

Page 65: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

File acceptbid.php HTML generated code

Page 66: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

Returns the current date and time as a value in 'YYYY-MM-DD HH:MM:SS' or YYYYMMDDHHMMSS.uuuuuu formatdepending on whether the function is used in a

string or numeric contextThe value is expressed in the current time

zone.

MySQL now() function

Page 67: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

File acceptbidprocess.php

Page 68: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

Resulting records

Page 69: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

Outline Topic #9 Listing Bids For Each Bid Item

Page 70: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

MySQL date_format() functionRelating information from two or more tablesSQL order by clause

Listing bids for each bid item

Page 71: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

Formats a string based on a specified formatThe following are some of the specifies of the

format string:%D: Day of month with English suffix%d: Numeric day of month (01…31)%M: Month name (January…December)%m: Month numeric (01…12)%Y: Year (4 digits)%y: Year (2 digits)

MySQL date_format() function

Page 72: Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement

MySQL date_format() sample