Presentation is loading. Please wait.

Presentation is loading. Please wait.

Creating databases for Web Applications php basics. Emailing. phpMyAdmin to set up MySQL. Homework: Use phpMyAdmin. Make posting with [unique] source on.

Similar presentations


Presentation on theme: "Creating databases for Web Applications php basics. Emailing. phpMyAdmin to set up MySQL. Homework: Use phpMyAdmin. Make posting with [unique] source on."— Presentation transcript:

1 Creating databases for Web Applications php basics. Emailing. phpMyAdmin to set up MySQL. Homework: Use phpMyAdmin. Make posting with [unique] source on MySql.

2 Reprise: PHP php files are interpreted by/on/at the server. –php code within html One or more of the following happens: –php interface to operating system is used to do things such as interact with databases OR files OR email Generally done by php using form input –HTML is produced, perhaps from data from database, and sent to the client –cookie is set and/or accessed Alternative is session data

3 Structure CLIENT Browser (Firefox): Requests URL SERVER PHP interpreter SERVER DBMS (MySQL) Build HTML page

4 php echo: prints material to the html document to be returned/sent to the client Can use print Extra credit opportunity: is there a difference?

5 A hello world program Somewhat contrived because it really doesn't need to run on the server. Demonstrate, show, then explain. but… will use it to illustrate how to use the query string http://faculty.purchase.edu/jeanine.meyer/hello world.phphttp://faculty.purchase.edu/jeanine.meyer/hello world.php http://faculty.purchase.edu/jeanine.meyer/hello world.php?who=curlyhttp://faculty.purchase.edu/jeanine.meyer/hello world.php?who=curly

6 Hello world in php <?php $who = $_GET['who']; $junk = "this is stuff that doesn't get to the html"; print ("$who wanted to say Hello, world "); ?>

7 php Variables start with a $. A string can contain a variable name and php will replace with the variable value! There are many associative arrays, such as $_GET and $_POST

8 Notes method=get produces the so-called query string. We can also generate query strings using php OR as direct call method=post sends data another way (via HTTP headers). Benefit is that less is revealed to user. Think of it also as lack of clutter.

9 Problem in my code? Hello world in php <?php $who = $_GET['who']; //how does this get set? $junk = "this is stuff that doesn't get to the html"; print ("$who wanted to say Hello, world "); ?>

10 Next We are going to create pairs of files: an html file and a php file. In this case, the html file will ask for information for sending email and the php file will do it.

11 Storyboard sendemailfromhtmlg.html sendemailp.php

12 html to php Sets of html (set up) and php files sendemailfromhtmlp.html –calls sendemailp.php sendemailfromhtmlg.html –calls sendemailg.php sendemailfromhtmlgfancy.html –calls sendemailgfancy.php

13 Note These all look the same when viewing the displayed html The source shows that each calls a different php file! This is quick, but not instantaneous. Be patient. –It is helpful to have multiple email addresses.

14 sendemailfromhtmlg.html Email

15 Your email (for reply) To email /p> Subject Body of message (you may change it) This is email sent from my own html/php application

16 textarea Similar to input Used for multi-line Can put in default content –Where should instructions go????

17 sendemailg.php <?php $to = $_GET['to']; $subject = $_GET['subject']; $body = $_GET['body']; $headers = "From: ". $_GET['from']; if (mail($to, $subject, $body,$headers)) { echo("Your message was sent"); } else { echo("There was a problem."); } ?>

18 Notice $_GET is an associative array. We use square brackets with the name of the input element in the form. Similarly, $_POST is an associative array. Concatenate strings using the. operator. –REPEAT: String concatenation uses a period (dot) NOT a plus sign! Function calls use parentheses.

19 Question What is different for the post version?

20 Mechanics in php script Need to distinguish html versus php: use Concatenation of strings operator is. (period) Single quotes and double quotes can be used, but must be paired appropriately!!!! Common task will be to generate a string (aka character string) made up of html you know ahead of time and html generated from variables. Next example mixes up html and php. First the html file and then the php file. I put in some color coding. There is some color coding in TextPad and more in Sublime.

21 Email Your email (for reply) To email Subject Body of message (you may change it) This is email sent from my own html/php application

22 php for sending email within html This script will use php mail <?php $to = $_GET['to' ]; $subject = $_GET['subject' ]; $body = $_GET['body' ]; $headers = "From: ". $_GET['from' ]; if (mail($to, $subject, $body,$headers)) { echo( "Your message was sent" ); } else { echo( "There was a problem." ); } ?> This is after the php

23 Exercise Write a pair of files. One html and one php. The html file has a form with action pointing to the php file. You can build on the send email files. You can do other things, like sending to the giving email and also to another email you know…. NOT MINE! Make it work!

24 geolocation example http://socialsoftware.purchase.edu/jeanine. meyer/emailing/geolocationkmemail.htmlhttp://socialsoftware.purchase.edu/jeanine. meyer/emailing/geolocationkmemail.html Look at code—focus on form

25 Your email (for reply) To email Subject: Body of message (you may change it) My geolocation is at the address given in the subject line.

26 <?php $to = $_POST['to']; $subject = $_POST['subject']; $body = $_POST['body']; $headers = "From: ". $_POST['from']; if (mail($to, $subject, $body,$headers)) { echo("Your message was sent"); } else { echo("There was a problem."); } ?>

27 Extra credit possibility Figure out how to get an error Note: the use of mail is an asynchronous operation: initiated from php (sent to operating system on the server)

28 For information about php and MySQL <?php ob_start(); // buffering output phpinfo(); $phpinfo = ob_get_contents(); //get contents echo $phpinfo; //print out as html ?> http://socialsoftware.purchase.edu/jeanine.meyer/php infotest.php

29 php example Test Welcome to the store <?php …. Print(" New Products "); Print( " “. $newproduct1name); ?> Variable set & used

30 php variables within strings For all php variables, you can put the variable name in a string: print("The name input was: $fname"); –php figures out that $fname is a variable and gets its value. NOTE: out of habit in using other programming languages, sometimes I don't do that: print ("The name input was :". $fname); NOTE: the string concatenation operator is. Caution: SQL often requires a single quotation mark

31 Form data in php Built-in functions $_GET[' '] $_POST[' '] If you want to NOT distinguish (considered less secure) $_REQUEST[' '] also includes $_COOKIE Name in form

32 So… Try writing [yet another] html file calling a php file that uses $_Request

33 Checking if a form variable has been set Common situation to check if this is first time $saywhich=@$_GET['saywhich']; Prevents error if no value exists

34 random rand (a, b) returns a pseudo-random choice from a to b, inclusive $choice=rand(0, sizeOf($states)-1); The arrays in php (like JavaScript and ActionScript) start indexing at zero.

35 Overview Useful to think in terms of 3 language domains (my terminology): –client side browser (interpreter of html) –server side interpreter of asp/JavaScript or php (There is another level here for the operating system, but this is what the asp objects and the php built-in functions & variables provide.) –database interpreter of SQL (by Access or MySQL)

36 Warnings Applicable to php and other languages: To output quotation marks in the HTML, you may use single quotes or 'escape' double quotation marks: print (" "); Preview: SQL statements require quotation marks around values in certain cases. SQL statements use a single equals sign (=) for checking for equality. JavaScript and php use == in IF statements.

37 Claim All database management systems share similar structure. –Tables Records Fields –Records have primary keys –Records may have fields that hold foreign keys, that is, pointers to records in other tables or in that table. They differ on efficiencies noticeable in large[r], production systems –How many simultaneous queries –Security issues

38 Pre Banner example (NOT ACCURATE) Table of courses: Each record represents one course. Course identifier: MAT3530 Cross-listing: NME3520 Name: Creating Databases for Web Applications ASSUMING a course has at most one pre- requisite, each record has field that Pre-req: MAT1420 NOTE: the pre-reqs are more complicated, since there are many possibilities. This would require a new table.

39 Creating database Create tables [Create / specify relations.] If database to be used on stand-alone computer, create Forms, Queries, Views. Instead, we will create programs (html and php scripts) to do this.

40 Table Define what is a record in the table: what are the fields? –What is the information associated with this one thing? What is the data type of each field? –If the databases will contain many records, may be important to be careful as to size Is there an intrinsic primary key (unique for the record in the table) or should system supply one? Fields can have at most one value (can be null) –Multiple values means you need another table

41 Caution Defining (designing) a database is not trivial. May require revision. Interview process with clients/customers/systems owners can take time.

42 What should we do? Budget (past and future expenses, dates) Favorite web sites Music collection (with features) Courses taken (grades) / will take Candidates ?

43 Spreadsheet vs Database Scale: DBMS can handle large amounts of information Efficiency: DBMS can handle different datatypes DBMS have facilities for MANAGING access by multiple users DBMS supports multiple tables and queries across the multiple tables. MySQL (and other DBMS) have easier (?) connections to php and other programming languages –Extra credit opportunity: do posting on php and Excel or php and Open Office or VB.net & xls, etc. Give explanation / examples, NOT JUST THE LINK!

44 Preview Team projects: take one of my sample projects at http://faculty.purchase.edu/jeanine.meyer/d b/examples.html http://faculty.purchase.edu/jeanine.meyer/d b/examples.html understand and present to class make enhancements

45 Admission There is (at least) one problem in the trivia quiz involving questions.

46 Trivia game Questions table –Question id –Question text –Question answer –Question category –Question score Player table History table

47 Trivia database: players Player id Player name Player password

48 Trivia database: history Event id Player id Question id Result (right or wrong) Time stamp

49 Entity Relationship Diagram Questions Ques id Text Answer Category score Players Player id Player name Password History historyID Ques id Player id Result Time stamp

50 phpMyAdmin Used to set up tables for your / my one database –Can also do this entirely using php code After setting up table, we will then use php for adding, deleting and query-ing records in the tables. Can use phpMyAdmin to see what is in the tables

51 Homework To do email examples, you need to upload to your server account. –Get html email examples working if you haven't done so –Experiment with phpMyAdmin First use regular password, then id and password given in README file Required: Find a good source on MySql and make posting. [Continue review of HTML/HTML5] Next class: parallel structures


Download ppt "Creating databases for Web Applications php basics. Emailing. phpMyAdmin to set up MySQL. Homework: Use phpMyAdmin. Make posting with [unique] source on."

Similar presentations


Ads by Google