Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Fundamentals of Web DevelopmentRandy.

Slides:



Advertisements
Similar presentations
WEB DESIGN TABLES, PAGE LAYOUT AND FORMS. Page Layout Page Layout is an important part of web design Why do you think your page layout is important?
Advertisements

Lecture 6/2/12. Forms and PHP The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input When dealing with HTML forms.
HTML 5 and CSS 3, Illustrated Complete Unit L: Programming Web Pages with JavaScript.
Lecture-7/ T. Nouf Almujally
Browsers and Servers CGI Processing Model ( Common Gateway Interface ) © Norman White, 2013.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
Using Visual Basic 6.0 to Create Web-Based Database Applications
A Guide to Oracle9i1 Introduction To Forms Builder Chapter 5.
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
Chapter 14: Advanced Topics: DBMS, SQL, and ASP.NET
Chapter 7 Managing Data Sources. ASP.NET 2.0, Third Edition2.
2440: 141 Web Site Administration Web Server-Side Programming Professor: Enoch E. Damson.
WHAT IS PHP PHP is an HTML-embedded scripting language primarily used for dynamic Web applications.
Introduction To Databases IDIA 618 Fall 2014 Bridget M. Blodgett.
Chapter 9 Collecting Data with Forms. A form on a web page consists of form objects such as text boxes or radio buttons into which users type information.
Sys Prog & Scripting - HW Univ1 Systems Programming & Scripting Lecture 15: PHP Introduction.
1 Web Developer & Design Foundations with XHTML Chapter 6 Key Concepts.
1 Web Database Processing. Web Database Applications Static Report Publishing a report is prepared from a database application and exported to HTML DB.
Reading Data in Web Pages tMyn1 Reading Data in Web Pages A very common application of PHP is to have an HTML form gather information from a website's.
Database Design for DNN Developers Sebastian Leupold.
ASP.NET Programming with C# and SQL Server First Edition
Basics of Web Databases With the advent of Web database technology, Web pages are no longer static, but dynamic with connection to a back-end database.
Overview of Previous Lesson(s) Over View  ASP.NET Pages  Modular in nature and divided into the core sections  Page directives  Code Section  Page.
About Dynamic Sites (Front End / Back End Implementations) by Janssen & Associates Affordable Website Solutions for Individuals and Small Businesses.
Sys Prog & Scripting - HW Univ1 Systems Programming & Scripting Lecture 19: Database Support.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
XHTML Introductory1 Linking and Publishing Basic Web Pages Chapter 3.
10-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
FUNCTIONS AND STORED PROCEDURES & FUNCTIONS AND PROTECTING A DB AND PHP (Chapters 9, 15, 18)
M1G Introduction to Database Development 6. Building Applications.
PHP meets MySQL.
9 Chapter Nine Compiled Web Server Programs. 9 Chapter Objectives Learn about Common Gateway Interface (CGI) Create CGI programs that generate dynamic.
Web Server Administration Chapter 7 Installing and Testing a Programming Environment.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.
Section 17.1 Add an audio file using HTML Create a form using HTML Add text boxes using HTML Add radio buttons and check boxes using HTML Add a pull-down.
ASP.NET.. ASP.NET Environment ASP.NET is Microsoft's programming framework that enables the development of Web applications and services. It is an easy.
Accessing MySQL with PHP IDIA 618 Fall 2014 Bridget M. Blodgett.
Creating Dynamic Web Pages Using PHP and MySQL CS 320.
Chapter 6 PHP Interacts with Mysql Database. Introduction In PHP, there is no consolidated interface. Instead, a set of library functions are provided.
PHP and MySQL CS How Web Site Architectures Work  User’s browser sends HTTP request.  The request may be a form where the action is to call PHP.
Lecturer: Gareth Jones. How does a relational database organise data? What are the principles of a database management system? What are the principal.
Accessing Your MySQL Database from the Web with PHP (Ch 11) 1.
Discovering Computers Fundamentals Fifth Edition Chapter 9 Database Management.
Website Development with PHP and MySQL Saving Data.
Database Design and Management CPTG /23/2015Chapter 12 of 38 Functions of a Database Store data Store data School: student records, class schedules,
CHAPTER 9 PHP AND MYSQL. A POSSIBLE SITE CONFIGURATION Application Folder index.php includes (folder)header.phpfooter.phpstyle.cssmodel (folder)mysqli_connect.php.
3 Copyright © 2004, Oracle. All rights reserved. Working in the Forms Developer Environment.
© Anselm Spoerri Web Design Information Visualization Course Prof. Anselm Spoerri
David Lawrence 7/8/091Intro. to PHP -- David Lawrence.
BlackBerry Applications using Microsoft Visual Studio and Database Handling.
Microsoft FrontPage 2003 Illustrated Complete Integrating a Database with a Web Site.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Fundamentals of Web DevelopmentRandy.
CHAPTER 10 PHP MySQL Database
Since you’ll need a place for the user to enter a search query. Every form must have these basic components: – The submission type defined with the method.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 1: Introduction to IS2803 Rob Gleasure
8 th Semester, Batch 2009 Department Of Computer Science SSUET.
Microsoft Office 2013 Try It! Chapter 4 Storing Data in Access.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Fundamentals of Web DevelopmentRandy.
Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Fundamentals of Web DevelopmentRandy.
PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used, free, and efficient alternative.
DBMS & TPS Barbara Russell MBA 624.
Working with Databases
Multitier Architecture, MySQL & PHP
ISC440: Web Programming 2 Server-side Scripting PHP 3
Web Systems Development (CSC-215)
Tutorial 6 PHP & MySQL Li Xu
PHP Forms and Databases.
Presentation transcript:

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Fundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Textbook to be published by Pearson Ed in early Fundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar © 2015 Pearson Working with Databases Chapter 11

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Objectives Databases and Web Development Database APIs Accessing MySQL in PHP 5 Sample Database Techniques 7 Managing a MySQL Database 4 6

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar DATABASES AND WEB DEVELOPMENT Section 1 of 7

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Databases and Web Development This chapter covers the core principles of relational Database Management Systems (DBMSs). All database management systems are capable of managing large amounts of data, maintaining data integrity, responding to many queries, creating indexes and triggers, and more. The term database can refer to both the software (i.e., to the DBMS) and to the data that is managed by the DBMS.

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar MySQL There are many other open source and proprietary relational DBMS, including: PostgreSQL Oracle Database IBM DB2 Microsoft SQL Server MySQL We will be using MySQL

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar The Role of Databases Databases provide a way to implement one of the most important software design principles: one should separate that which varies from that which stays the same In Web Development

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Separate that which varies That is, use a DB to store the content of pages. The program (PHP) determines which data to display, often from information in the GET or POST query string, and then uses a database API to interact with the database Although the same separation could be achieved by storing content in files on the server, databases offer intuitive and optimized systems that do far more than a file based design that would require custom-built reading, parsing, and writing functions.

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar That Which Changes Can be stored in the DB

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Database Design Tables A database in a Relational DBMS is composed of one or more tables. A table is a two-dimensional container for data that consists of records (rows); Each record has the same number of columns, which are called fields, which contain the actual data. Each table will have one special field called a primary key that is used to uniquely identify each record in a table.

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Tables in a Database Illustrated example

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar DATABASE APIS Section 3 of 7

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar API API stands for application programming interface and in general refers to the classes, methods, functions, and variables that your application uses to perform some task. Some database APIs work only with a specific type of database; others are cross-platform and can work with multiple databases. Application Programming Interface

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar PHP MySQL APIs MySQL extension. This was the original extension to PHP for working with MySQL and has been replaced with the newer mysqli extension. This procedural API should now only be used with versions of MySQL older than (At the time of writing, the current version of MySQL was ) mysqli extension. The MySQL Improved extension takes advantage of features of versions of MySQL after This extension provides both a procedural and an object-oriented approach. This extension also supports most of the latest features of MySQL. PHP data objects (PDOs). This object-oriented API has been available since PHP 5.1 and provides an abstraction layer (i.e., a set of classes that hide the implementation details for some set of functionality) that with the appropriate drivers can be used with any database, and not just MySQL databases. However, it is not able to make use of all the latest features of MySQL. There is more than 1

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar We will show how to do some of the most common database operations using the procedural mysqli extension as well as the object-oriented PDO. As the chapter (and book) proceed, we will standardize on the object-oriented, database- independent PDO approach.

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar MANAGING A MYSQL DATABASE Section 4 of 7

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar How do you access the DBMS Although you will eventually be able to manipulate the database from your PHP code, there are some routine maintenance operations that do not warrant custom code. Tools include: Command Line Interface phpMyAdmin MySQLWorkbench So, so many ways

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar phpMyAdmin Will even generate PHP code A popular web-based front-end (written in PHP) called phpMyAdmin allows developers to access management tools through a web portal.

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar MySQL Workbench Powerful design tools The MySQL Workbench is a free tool from Oracle to work with MySQL databases.

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar ACCESSING MYSQL IN PHP Section 5 of 7

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Database Connection Algorithm No matter what API you use, the basic database connection algorithm is the same: 1.Connect to the database. 2.Handle connection errors. 3.Execute the SQL query. 4.Process the results. 5.Free resources and close connection.

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Database Connection Algorithm An illustration through example

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Database Connection Algorithm An illustration through example

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Storing Connection Details Hard-coding the database connection details in your code is not ideal. Connection details almost always change as a site moves from development, to testing, to production. We should move these connection details out of our connection code and place it in some central location.

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Handling Connection Errors We need to handle potential connection errors in our code. Procedural mysqli techniques use conditional (if...else) statements on the returned object from the connection attempt. The PDO technique uses try-catch which relies on thrown exceptions when an error occurrs.

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Handling Connection Errors Procedural Approach

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Handling Connection Errors Object-Oriented PDO with try-catch In addition PDO has 3 different error modes, that allow you to control when errors are thrown.

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Execute the Query Procedural and Object-Oriented Both return a result set, which is a type of cursor or pointer to the returned data

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Queries that don’t return data Procedural and Object-Oriented

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Integrating User Data Say, using an HTML form posted to the PHP script

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Integrating User Data Not everyone is nice. While this does work, it opens our site to one of the most common web security attacks, the SQL injection attack.

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar SQL Injection Illustration From Chapter 16

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Defend against attack Distrust user input The SQL injection class of attack can be protected against by Sanitizing user input Using Prepared Statements

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Sanitize User Input Quick and easy Each database system has functions to remove any special characters from a desired piece of text. In MySQL, user inputs can be sanitized in PHP using the mysqli_real_escape_string() method or, if using PDO, the quote() method

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Prepared Statements Better in general A prepared statement is actually a way to improve performance for queries that need to be executed multiple times. When MySQL creates a prepared statement, it does something akin to a compiler in that it optimizes it so that it has superior performance for multiple requests. It also integrates sanitization into each user input automatically, thereby protecting us from SQL injection.

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Prepared Statements mysqli

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Prepared Statements PDO

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Prepared Statements Comparison of two techniques

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Process Query Results mysqli

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Process Query Results Mysqli – using prepared statements

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Fetch into an object Instead of an array Consider the following (very simplified) class: class Book { public $id; public $title; public $copyrightYear; public $description; }

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Fetch into an object Instead of an array The property names must match exactly (including the case) the field names in the table(s) in the query

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Fetch into an object A more flexible example, where class names needn’t match DB fields

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Freeing Resources And closing the connection

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Using Transactions mysqli

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Using Transactions PDO

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar SAMPLE DATABASE TECHNIQUES Section 7 of 7

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Display a list of Links One of the most common database tasks in PHP is to display a list of links (i.e., a series of elements within a ).

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Display a list of Links At its simplest, the code would look something like the following: $sql = "SELECT * FROM Categories ORDER BY CategoryName”; $result = $pdo->query($sql); while ($row = $result->fetch()) { echo ' ’; echo ' ’; echo $row['CategoryName']; echo ' ’; echo ' ’; }

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Display a list of Links More maintainable version

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Search and Results Page Visual of search box, results page, and no results page

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Search and Results Page In this example, we will assume that there is a text box with the name txtSearch in which the user enters a search string along with a Submit button. The data that we will filter is the Book table; we will display any book records that contain the user-entered text in the Title field.

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Search and Results Page To redisplay the search term we will add code like: " /> To where we generate the form. Unfortunately…

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Search and Results Page Problem to be solved

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Editing a Record

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Editing a Record

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Files in the Database There are two ways of storing files in a database: 1.Storing file location in the database, and storing the file on the server’s filesystem 2.Storing the file itself in the database in the form of a binary BLOB

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Files in the Database As a file Location

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Files in the Database As a BLOB

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Files in the Database Storing and retrieving BLOBs

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Files in the Database Storing and retrieving BLOBs Listing can then be integrated into HTML image tags by pointing the src attribute to our script Would now reference a dynamic PHP script like:

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Files in the Database HTTP headers matter The same file output with correct and incorrect headers is interpreted differently by the browser

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar What You’ve Learned Databases and Web Development Database APIs Accessing MySQL in PHP 5 Sample Database Techniques 7 Managing a MySQL Database 4 6