08/09/2015 Intro PHP & MySQL 1 Helen Hastie Room: EM2.44 Material available on Vision (modified from slides by Monica Farrow) F27DB Introduction.

Slides:



Advertisements
Similar presentations
JQuery MessageBoard. Lets use jQuery and AJAX in combination with a database to update and retrieve information without refreshing the page. Here we will.
Advertisements

PHP II Interacting with Database Data. The whole idea of a database-driven website is to enable the content of the site to reside in a database, and to.
AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
The Librarian Web Page Carol Wolf CS396X. Create new controller  To create a new controller that can manage more than just books, type ruby script/generate.
Web Database Programming Connecting Database to Web.
10/05/2015 Introduction to Databases 1 F27DB Introduction to Database Systems More SQL Helen Hastie Room: EMB244 Material available on.
Guide to Oracle10G1 Introduction To Forms Builder Chapter 5.
J4www/jea Week 3 Version Slide edits: nas1 Format of lecture: Assignment context: CRUD - “update details” JSP models.
A Guide to Oracle9i1 Introduction To Forms Builder Chapter 5.
DT228/3 Web Development Databases. Database Almost all web application on the net access a database e.g. shopping sites, message boards, search engines.
Intermediate PHP & MySQL
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
Computer Science 101 Web Access to Databases Overview of Web Access to Databases.
2440: 141 Web Site Administration Web Server-Side Programming Professor: Enoch E. Damson.
Form Handling, Validation and Functions. Form Handling Forms are a graphical user interfaces (GUIs) that enables the interaction between users and servers.
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.
Advance Computer Programming Java Database Connectivity (JDBC) – In order to connect a Java application to a database, you need to use a JDBC driver. –
SJSU CS157B Dr. Lee1  2004 Jenny Mitchell Two Useful Tools You Can’t Live Without by Jenny Mitchell SJSU CS157B Section PHP and MySQL.
Lecture 3 – Data Storage with XML+AJAX and MySQL+socket.io
02/09/2015 Intro PHP & MySQL 1 Helen Hastie Room: EMB244 Material available on Vision (modified from slides by Monica Farrow) F27DB Introduction.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Advance Database Management Systems Lab no. 5 PHP Web Pages.
Application Development Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries.
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.
FALL 2005CSI 4118 – UNIVERSITY OF OTTAWA1 Part 4 Web technologies: HTTP, CGI, PHP,Java applets)
Lecture 6 – Form processing (Part 1) SFDV3011 – Advanced Web Development 1.
Create an online booking system (login/registration)
CSCI 6962: Server-side Design and Programming JDBC Database Programming.
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
Jonathan Westlake, ed: Nic Shulver JSP, Structure and JavaBeans JSP models Assignment context “Register as a member of the site/show member details/update.
Creating a Web Site to Gather Data and Conduct Research.
Active Server Pages ASP is Microsoft’s server-side script engine for dynamically-generated web pages. Most common language used is VBScript. If you use.
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.
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
Accessing MySQL with PHP IDIA 618 Fall 2014 Bridget M. Blodgett.
15/10/20151 PHP & MySQL 'Slide materials are based on W3Schools PHP tutorial, 'PHP website 'MySQL website.
Introduction to MySQL Lab no. 10 Advance Database Management System.
PHP MySQL Introduction. MySQL is the most popular open-source database system. What is MySQL? MySQL is a database. The data in MySQL is stored in database.
Creating Dynamic Web Pages Using PHP and MySQL CS 320.
NMED 3850 A Advanced Online Design January 12, 2010 V. Mahadevan.
Chapter 6 Server-side Programming: Java Servlets
CSC 2720 Building Web Applications Server-side Scripting with PHP.
Creating PHPs to Insert, Update, and Delete Data CS 320.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP & MySQL.
1 CS 430 Database Theory Winter 2005 Lecture 14: Additional SQL Topics.
Controlling Web Site Access Using Logins CS 320. Basic Approach HTML form a php page that collects the username and password  Sends them to second PHP.
Li Tak Sing COMPS311F. Database programming JDBC (Java Database Connectivity) Java version of ODBC (Open Database Connectivity) ODBC provides a standard.
Creating a simple database This shows you how to set up a database using PHPMyAdmin (installed with WAMP)
CS562 Advanced Java and Internet Application Introduction to the Computer Warehouse Web Application. Java Server Pages (JSP) Technology. By Team Alpha.
©SoftMooreSlide 1 Introduction to HTML: Forms ©SoftMooreSlide 2 Forms Forms provide a simple mechanism for collecting user data and submitting it to.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Chapter 8 Manipulating MySQL Databases with PHP PHP Programming with MySQL 2 nd Edition.
IST 220 – Intro to Databases Lecture 2 Touring Microsoft Access.
JDBC CS 260 Database Systems. Overview  Introduction  JDBC driver types  Eclipse project setup  Programming with JDBC  Prepared statements  SQL.
Database Connectivity and Server-Side Scripting Chapter 12.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 1: Introduction to IS2803 Rob Gleasure
CSC 2720 Building Web Applications Basic Frameworks for Building Dynamic Web Sites / Web Applications.
PHP Form Processing * referenced from
: Information Retrieval อาจารย์ ธีภากรณ์ นฤมาณนลิณี
PHP: Further Skills 02 By Trevor Adams. Topics covered Persistence What is it? Why do we need it? Basic Persistence Hidden form fields Query strings Cookies.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.
Unit 4 Working with data. Form Element HTML forms are used to pass data to a server. A form can contain input elements like text fields, checkboxes, radio-buttons,
PHP / MySQL Introduction
ISC440: Web Programming 2 Server-side Scripting PHP 3
Web Systems Development (CSC-215)
Presentation transcript:

08/09/2015 Intro PHP & MySQL 1 Helen Hastie Room: EM2.44 Material available on Vision (modified from slides by Monica Farrow) F27DB Introduction to Database Systems Accessing MySQL database via PHP - 2

08/09/2015 Intro PHP & MySQL 2 Recap Website design Choices Request spy details (Enter code) Spy details displayed Update spy (enter details) Changes confirmed Enter new spy (enter details) Add confirmed Login Covered in lecture 5

08/09/2015 Intro PHP & MySQL 3 Recap - sessions Sessions are visits to a website PHP provides functions for managing sessions Use session_start() before outputting any html Session variables are used to store and retrieve session data E.g. Adding a session variable $_SESSION[‘password’]=$_POST[‘password’]; E.g. Retrieving a session variable $password = $_SESSION[‘password’]; A session ends when the user moves away from the site, or it can time-out

08/09/2015 Intro PHP & MySQL 4 Recap - Display spy - response Links to other actions One table for the Spy data Another for the skills To keep things simple, I am using the column name from the database as headings Not ideal!

08/09/2015 Intro PHP & MySQL 5 Recap : dbfunctions.php file This file contains my own functions to run each of the mysql database functions, and also to automatically display a table This makes the code in the main script easier to read Instead of lots of blocks of code ‘do this, then see if it works’ one after the other, you see simpler code, shown on the next slide It means that beginning programmers can use my example php scripts, and only change Their username the SQL queries the parameters from the form, which are needed for the SQL query

08/09/2015 Intro PHP & MySQL 6 Creating the form to add a spy Mostly html here heading Links Input fields

08/09/2015 Intro PHP & MySQL 7 Error prevention For gender, use radio buttons to ensure that only valid options are entered Use php scripts to get values from MySQL for drop-down list and the multiple option box The php scripts query the database to find out the existing spymasters and skills

08/09/2015 Intro PHP & MySQL 8 Creating the SpyMaster option list Retrieve username and password from session variables Connect to database Run query to find all spymaster codenames 'SELECT mCodeName FROM SpyMaster'; Create html option list from that //find all the spymaster codenames $query = 'SELECT mCodeName FROM SpyMaster'; $result = runQuery($query); //now need to display in option box - pto

08/09/2015 Intro PHP & MySQL 9 Display spymaster options //PUT EACH SPYMASTER CODENAME INTO AN OPTION BOX //first print out blank initial option print ' '; //get each row in turn while ($row = mysql_fetch_row($result) ) { //get value of first (only) column in the row //and place as value and display in option print ' '. $row[0]. ' '; } print ' '; }

08/09/2015 Intro PHP & MySQL 10 Display skill options Very similar to the spymaster code The skill name is value displayed for the user to choose The skill code is the value to be sent to the next script A multiple option box is created which returns all selected items in an array //option value is code, but name is displayed $query = "SELECT skillCode, skillName FROM SpySkillList"; $result = runQuery($query); print ' '; while ($row = mysql_fetch_row($result) ) { print ' '. $row[1]. ' '; } print ' ';

08/09/2015 Intro PHP & MySQL 11 Receiving the data The script referred to in the form’s action attribute, which generates the response, must: Start a session & retrieve username and password Output initial html Pick up all the form parameters Create an INSERT command with all the parameters Run it Report whether it worked or not

08/09/2015 Intro PHP & MySQL 12 Creating the insert command Picking up the form parameters Creating the INSERT command Notice outer double quotes, inner single quotes It’s probably a good idea to test this out at this stage, just printing the insert command to make sure it looks alright, before trying to run it //First pick up the parameters //from the form $spycode = $_POST["codename"]; $firstName = $_POST["first"]; Etc etc //define and run the insertion $query = "INSERT INTO Spy VALUES ('$spycode', '$firstName', '$lastName', '$date', '$gender', '$mark', 0, '$spymaster', NULL )"; print $query. " " ; /

08/09/2015 Intro PHP & MySQL 13 Inserting the spy $insResult = mysql_query($query); if ($insResult) print("Spy details for ". $firstName. " ". $lastName. " have been inserted "); else //vital to know why it failed exit ( $query. " ". mysql_error(). " " ); Run it using the function mysql_query and test to see whether it worked Print whether the insertion was a success or not. Exit if not.

08/09/2015 Intro PHP & MySQL 14 Inserting the skills A new record must also be created in the SpyWithSkill table, for each skill in the skill array parameter Consists of the spy code and the skill code $skills = $_POST['skill']; foreach($skills as $skill) //for each skill { $query = "INSERT INTO SpyWithSkill VALUES ('$spycode', $skill)"; print $query. ' '; $insResult = mysql_query($query); if ($insResult) { print("Spy skill inserted "); } else exit ( mysql_error(). " " );

08/09/2015 Intro PHP & MySQL 15 Problems with insertions What sort of things can go wrong with insertions? Duplicate Primary Key A record already exists with this primary key Foreign key invalid No matching value exists in the referenced table Invalid data Incorrect range, incorrect data type, incorrect length

08/09/2015 Intro PHP & MySQL 16 Insertion error prevention / feedback Today’s example just prints out the mysql error message, which wouldn’t be very friendly for a user It would be better to determine what is wrong and output a suitable message for the user Frequently javascript is used in the HTML form to ensure that the data is acceptable Not covered in this module It may be that the user’s computer has javascript disabled or otherwise not functioning So ideally all these values should be checked in the PHP script too

08/09/2015 Intro PHP & MySQL 17 Running an update command Updates work exactly like insertions Gather the data via a form and post to a php script Create an UPDATE command in PHP, using parameter data Run the Update command Check that it worked and output feedback

08/09/2015 Intro PHP & MySQL 18 Dates To insert a date to MySQL, we need it in the format YYYY-MM-DD E.g INSERT INTO MyTable VALUES ( …, …, …, ‘ ’,…,…); To insert today’s date, you can use the MySQL CURDATE() function: E.g. INSERT INTO MyTable VALUES ( …, …, …, CURDATE(),…,…); You can also use CURRENT_DATE() which is a synonym for CURDATE()

08/09/2015 Intro PHP & MySQL 19 PHP, objects and extensions PHP can be used in a ‘procedural’ way, as here work your way through the lines of code, use functions to reduce code duplication and simplify the main script However recent versions also allow object- oriented programming and exception handling Not covered in this module You may see examples of this on the web or in books

08/09/2015 Intro PHP & MySQL 20 Beyond MySQL and PHP A brief look at wider issues Dynamic and static web pages Connecting to a database from a programming application Programming and SQL Interoperability

08/09/2015 Intro PHP & MySQL 21 Static and dynamic web pages Some web pages are static The html never changes. These pages can be written entirely in html. Examples are the Request Spy Details page, & initial Log-in page Many web pages are dynamic Some of the html never changes Some is determined at run-time, and depends on the request. This usually involves the submission of data from html forms, and connection to a database to query or update records Suitable software includes PHP & MySQL

08/09/2015 Intro PHP & MySQL 22 Querying and updating a database The process is the same for all databases and server-side languages Submit a request, probably with data in parameters Using a script or programming language Connect to the database create SQL command including this data Submit the command to the database Process the result obtained from the database Output the response, consisting of appropriate html, which may contain data from the database

08/09/2015 Intro PHP & MySQL 23 Connecting to the database There are different databases and different scripting languages How to know how to connect? The ODBC standard, pioneered by Microsoft, is an Open Database Connectivity standard Aims to be independent of database, programming language or operating system There are ODBC drivers for most DBMS It may be necessary to locate/install these ODBC forms the basis of other connectivity such as JDBC (java connectivity) and OLE-DB (later Microsoft version)

08/09/2015 Intro PHP & MySQL 24 ODBC Architecture : 4 levels The application uses a driver to submit SQL statements and retrieve results. The driver manager helps the application find a driver. The driver translates the application's data queries into commands that the DBMS understands. Both the application and the DBMS must be ODBC-compliant -- that is, the application must be capable of issuing ODBC commands and the DBMS must be capable of responding to them A data source is a collection of data (usually a database) which the driver can query. Application Driver Manager Driver Data Source Data Source Data Source

08/09/2015 Intro PHP & MySQL 25 SQL and programming SQL alone is insufficient for an application Need some type of programming language to deal with logic (alternatives, repetition) Some DBMS have included programming functionality as an extension of SQL An example is Oracle’s PL/SQL Includes loops and conditional statements Procedures, functions and triggers can be created and stored within the database A good solution for SQL intensive applications For applications which are logic-intensive, SQL can be included within a programming language - pto

08/09/2015 Intro PHP & MySQL 26 Programming SQL A database can be accessed from a programming language in one of two ways: Embedded SQL (not covered) directly embedding SQL statements in the program source. A precompiler is required to replace SQL statements with calls to DBMS routines. A Database Connectivity API, like ODBC or JDBC (how PHP accesses MySQL) a standard set of functions in the language that access the database, using hidden 'compiler magic'. Avoids the need for precompilation and is more portable – but involves more use of Strings

08/09/2015 Intro PHP & MySQL 27 Interoperability? Relational systems with SQL provide a single model for data and so should provide good support for such general application development (one application, easy to alter the database) But: the programming interface to SQL varies the functionality provided varies the flavour of SQL varies the system catalog varies - e.g. domain names some data is not in a database DBMS vendors pride themselves on adding extra functionality

08/09/2015 Intro PHP & MySQL 28 The MySpy website My Spies website is available for downloading on the module website Download it, put into your www folder, and play with it using a URL like pies/SpyStart.html You can then take another copy and adapt it for use in your coursework, rather than starting from scratch, although you can also do this if you prefer! NOW INCLUDES Insert example page too