Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 19 PHP Part III Credits: Parts of the slides are based on slides created by textbook authors, P.J. Deitel and H. M. Deitel by Prentice Hall ©1992-2012.

Similar presentations


Presentation on theme: "Chapter 19 PHP Part III Credits: Parts of the slides are based on slides created by textbook authors, P.J. Deitel and H. M. Deitel by Prentice Hall ©1992-2012."— Presentation transcript:

1 Chapter 19 PHP Part III Credits: Parts of the slides are based on slides created by textbook authors, P.J. Deitel and H. M. Deitel by Prentice Hall © by Pearson Education, Inc. All Rights Reserved.

2 19.9 Connecting to a Database
Function mysql_connect (line 30 of database.php) connects to the MySQL database. It takes three arguments— the server’s hostname, a username and a password, and returns a database handle - a representation of PHP’s connection to the database, or false if the connection fails. Function mysql_select_db (line 35 of database.php) specifies the database to be queried, and returns a bool indicating whether or not it was successful. To query the database, we call function mysql_query (line 39 of database.php), specifying the query string and the database to query. This returns a resource containing the result of the query, or false if the query fails. It can also execute SQL statements such as INSERT or DELETE that do not return results. Function mysql_error (line 42 of database.php) returns any error strings from the database. mysql_close (line 45 of database.php) closes the connection to the database specified in its argument.

3 Outline data.html (1 of 2) Posts data to database.php

4 Outline Creates drop-down menu specifying which data to output to the screen, with * (all data) as the default selection data.html (2 of 2)

5 Outline database.php (1 of 3)
Builds a SELECT query with the selection made in data.html

6 Connects to database using server hostname localhost and username and password “iw3htp4”
Outline Specifies products as the database to be queried Queries $database with $query Returns any error strings from the database Closes the connection to the database Returns an array as $row with the values for each column of the current row in $result until has no more rows, false is return

7 foreach takes the $result ($row), iterates through each index $key of the array and stores the value in variable $value Outline database.php (3 of 3)

8 ©1992-2012 by Pearson Education, Inc. All Rights Reserved.
5 <!-- Fig. 19.15 ed5: data.html --> 6 <!-- Form to query a MySQL database. --> © by Pearson Education, Inc. All Rights Reserved.

9

10

11 19.10 Using Cookies A cookie is a text file that a website stores on a client’s computer to maintain information about the client during and between browsing sessions. A server can access only the cookies that it has placed on the client. Function setcookie takes the name of the cookie to be set as the first argument, followed by the value to be stored in the cookie. setcookie( "Name", $Name, time() + 60 * 60 * 24 * 5 ); The optional third argument indicates the expiration date of the cookie. A cookie without a third argument is known as a session cookie, while one with an expiration date is a persistent cookie. If only the name argument is passed to function setcookie, the cookie is deleted from the client’s computer. Cookies defined in function setcookie are sent to the client at the same time as the information in the HTTP header; therefore, it needs to be called before any HTML is printed. The current time is returned by function time.

12 Outline cookies.html (1 of 2) Posts form data to cookies.php
Creates fields to gather information to be written into a cookie

13 cookies.html (2 of 2) Outline Form field

14 Outline cookies.php (1 of 2)
Creates a cookie for each entered value and sets the expiration date to be five days after the current time

15 cookies.php (2 of 2) Links to the page that displays the contents of the cookie – see later

16 Look at those created cookies using Web Developer => View Cookie Information to see what you created

17 Software Engineering Observation 19.2
Some clients do not accept cookies. When a client declines a cookie, the browser application normally informs the user that the site may not function correctly without cookies enabled. Cookies should not be used to store addresses or private data on a client’s computer.

18 19.10 Using Cookies PHP creates the superglobal array $_COOKIE, which contains all the cookie values indexed by their names. Chrome – Customize and control Google Chrome Google Chrome=>Settings=>Show Advanced =>Privacy => Content Settings=> Cookies => All cookies and site data Firefox - Tools=>Options=>privacy => remove individual cookies  see cookies When using Internet Explorer => settings => internet Options => Browsing history => Settings => View files, cookies are stored in a Cookies directory on the client’s machine. In Firefox, cookies are stored in a file named cookies.txt.

19 Outline readCookies.php (1 of 2)

20 Outline readCookies.php Iterates through all values in $_COOKIE
(2 of 2)

21 19.11 Dynamic Content Function isset allows you to find out if a variable has a value. A variable ($$variable) allows the code to reference variables dynamically. You can use this expression to obtain the value of the variable whose name is equal to the value of $variable. The quotemeta function inserts a backslash (\) before any special characters in the passed string – line 82 below.

22 Outline dynamicForm.php (1 of 12)

23 Outline dynamicForm.php (2 of 12)
Checks whether the Register button has been pressed Checks that the first name field is not blank Makes an entry in the error array Sets $iserror to true

24 Outline dynamicForm.php
Checks that all other form fields are filled in correctly dynamicForm.php (3 of 12) Inserts a backslash before the parentheses in the phone number

25 Outline dynamicForm.php (4 of 12)

26 Outline dynamicForm.php (5 of 12)
Ends script here if there were no errors in the user input Section to be executed only if $iserror is true and dynamically create a form with errors indications

27 Outline dynamicForm.php Alerts the user that there are errors
(6 of 12) Iterates through $inputlist to create the form’s text boxes Outputs the field’s image Sets the value attribute of the text field to the value of the variable with the name of $inputname’s value Sets the name attribute of the text field to $inputname Puts an asterisk * next to fields that have errors

28 Outline dynamicForm.php (7 of 12)
Creates drop-down list for books, keeping the previously selected one selected

29 Outline dynamicForm.php (8 of 12)
Creates radio buttons for operating-system selection, keeping the previously selected option selected

30 Outline dynamicForm.php (9 of 12)

31 Outline dynamicForm.php (11,12 of 12)

32 Outline form.html - hard coded form from ch19.8 (1 of 4)
Appends form data to the browser request that contains the protocol and the URL of the requested resource Form data is posted to form.php to be processed

33 Outline form.html (2 of 4) Creates form fields
Creates drop-down list with book names

34 Outline form.html (3 of 4) Creates radio buttons with “Windows XP” initially selected

35 Outline formDatabase.php (1 of 3)
Selects all fields from the contacts database to display

36 Outline formDatabase.php (2 of 3)

37 Outline formDatabase.php (3 of 3)

38 19.11 Dynamic Content Ed5 has 2 files:
dynamicForm.php (generates a form with errors if applicable) and formDatabase.php (for reading from the database)

39 ©1992-2012 by Pearson Education, Inc. All Rights Reserved.

40 ©1992-2012 by Pearson Education, Inc. All Rights Reserved.

41 ©1992-2012 by Pearson Education, Inc. All Rights Reserved.

42

43

44 ©1992-2012 by Pearson Education, Inc. All Rights Reserved.

45

46 ©1992-2012 by Pearson Education, Inc. All Rights Reserved.

47 ©1992-2012 by Pearson Education, Inc. All Rights Reserved.

48 23.8 Operator Precedence Chart
The following table contains a list of PHP operators in decreasing order of precedence. Fig | PHP operator precedence and associativity. (Part 1 of 3.)

49 Fig. 23.23 | PHP operator precedence and associativity. (Part 2 of 3.)

50 Fig. 23.23 | PHP operator precedence and associativity. (Part 3 of 3.)

51


Download ppt "Chapter 19 PHP Part III Credits: Parts of the slides are based on slides created by textbook authors, P.J. Deitel and H. M. Deitel by Prentice Hall ©1992-2012."

Similar presentations


Ads by Google