Presentation is loading. Please wait.

Presentation is loading. Please wait.

NMD202 Web Scripting Week5. What we will cover today PHP & MySQL Displaying Dynamic Pages Exercises Modifying Data PHP Exercises Assignment 1.

Similar presentations


Presentation on theme: "NMD202 Web Scripting Week5. What we will cover today PHP & MySQL Displaying Dynamic Pages Exercises Modifying Data PHP Exercises Assignment 1."— Presentation transcript:

1 NMD202 Web Scripting Week5

2 What we will cover today PHP & MySQL Displaying Dynamic Pages Exercises Modifying Data PHP Exercises Assignment 1

3 Displaying Dynamic Pages A database driven website allows the content of the site to live in a database Web Browser Web Server PHP MySQL Request Response Request Data Provides Recordset

4 Displaying Dynamic Pages PHP has a client built into the Language. You can run all the operation available in MySQL from within PHP

5 Displaying Dynamic Pages Connect to the Database: $conn = mysql_connect(address,username,password); $conn will hold a connection identifier after the operation is successful or false if connection fails

6 Displaying Dynamic Pages Select the Database: mysql_select_db(databaseName,$conn); $conn is the connection identifier provided by the connection statement

7 Displaying Dynamic Pages Query the Database: $result = mysql_query(query,$conn); $conn is the connection identifier provided by the connection statement $result will hold the result of the query, or false if query fails

8 Displaying Dynamic Pages Handling errors Whenever your code relies on third party systems (ie:MySQL) you should always handle unforseen errors. $conn = mysql_connect(address,username,password); If (!$conn) { echo “ Could not connect ”; die(); }

9 Displaying Dynamic Pages Handling errors If (! mysql_select_db($databaseName,$conn) { echo “ Could not locate”.$ databaseName.” ”; die(); }

10 Displaying Dynamic Pages Handling errors $result = mysql_query(query,$conn); If (!$result) { die (“ Error performing query:”.mysql_error().” ”); }

11 Displaying Dynamic Pages Handling Resultsets $result = mysql_query(query,$conn); If (!$result) { die (“ Error performing query:”.mysql_error().” ”); } while ($row = mysql_fetch_array($result)) { // $row will hold an assoc array with the columns names as keys echo $row[“studentName”]; }

12 Displaying Dynamic Pages Keep config information separate: configuration.php $mySQLhost = “127.0.0.1”; $userName = “root”; $password = “”; $database = databaseName; Include configuration.php from your main file, if using inside a function don’t forget to include the keyword global

13 Exercise Display all students loaded in previous class as a table with all the attributes (name, email, etc)

14 Modifying Data Inserting data (example): Form is submitted Validation is performed Query is built based on submitted data (data should be sanitized) Query is performed Feedback is given to the user

15 Modifying Data Editing Data (example): Id record to edit is passed through the querystring Select query is executed to get the required record Form is built pre populating fields with record info Form is submitted Validation is performed Query is built based on submitted data (data should be sanitized) Query is performed Feedback is given to the user

16 Modifying Data Deleting Data (example): Id record to delete is passed through the querystring Query is built based on id (data should be sanitized) Query is performed Feedback is given to the user

17 Exercise Build a complete set of screens to insert, update, display and delete the students records. On the display table insert two more columns with links to the delete and edit pages, providing the id of the record. Split the logical parts into separate php files: display.php, edit.php, insert.php, delete.php, configuration.php

18 Assignment 1 Project Brief ER Diagram


Download ppt "NMD202 Web Scripting Week5. What we will cover today PHP & MySQL Displaying Dynamic Pages Exercises Modifying Data PHP Exercises Assignment 1."

Similar presentations


Ads by Google