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

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

Embed Size (px)

Citation preview

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

Creating databases for Web Applications

php basics. Emailing.

phpMyAdmin to set up MySQL.

Homework: Use phpMyAdmin. Make posting with [unique] source on MySql.

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

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

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

Structure

CLIENT

Browser (Firefox):

Requests URL

SERVER

PHP interpreter

SERVER

DBMS (MySQL)

Build HTML page

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

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?

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

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/helloworld.php

• http://faculty.purchase.edu/jeanine.meyer/helloworld.php?who=curly

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

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

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

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

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

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.

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

Problem in my code?<html><head><title>Hello world in php </title></head><body><?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 ");?></body></html>

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

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.

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

Storyboard

sendemailfromhtmlg.html

sendemailp.php

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

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

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

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.

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

sendemailfromhtmlg.html

<html>

<head>

<title>Email

</title>

</head>

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

<body><form action="sendemailg.php"> <p>Your email (for reply) <input type="text" name="from" /> </p> <p>To email <input type="text" name="to" /> /p><p>Subject <input type="text" name="subject" />

</p> <p> Body of message (you may change it) <br/> <TEXTAREA NAME="body" COLS=40 ROWS=6>This is email sent from my own html/php application</TEXTAREA> </p> <input type="submit" value="SEND" /></form> </body> </html>

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

textarea

• Similar to input

• Used for multi-line

• Can put in default content

– Where should instructions go????

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

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."); }?>

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

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.

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

Question

• What is different for the post version?

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

Mechanics in php script

• Need to distinguish html versus php: use <?php and ?>• 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.

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

<html> <head> <title>Email</title></head> <body><form action="sendemailgfancy.php"> <p>Your email (for reply) <input type="text" name="from" /> </p> <p>To email <input type="text" name="to" /> </p> <p>Subject <input type="text" name="subject" /> </p> <p> Body of message (you may change it) <br/> <TEXTAREA NAME="body" COLS=40 ROWS=6>This is email sent from my own html/php application</TEXTAREA> </p> <input type="submit" value="SEND" /></form> </body> </html>

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

<html> <head> <title>php for sending email within html </title> </head> <body>

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." ); }?><hr /> This is after the php </body> </html>

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

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!

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

geolocation example

• http://socialsoftware.purchase.edu/jeanine.meyer/emailing/geolocationkmemail.html

• Look at code—focus on form

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

<form name="msg" action="sendemailp.php" method="post"><p>

Your email (for reply)<input type="email" name="from" required/>To email<input type="email" name="to" required /></p>

Subject: <input type="text" name="subject" size="100" /><p>

Body of message (you may change it) <br/><TEXTAREA NAME="body" COLS=40

ROWS=5>My geolocation is at the address given in the subject line.</TEXTAREA></p>

<input type="submit" value="SEND" /></form>

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

<?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."); }?>

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

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)

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

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/phpinfotest.php

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

php example

<html><head><title> Test </title></head>

<body><h1> Welcome to the store </h1>

<?php

….

Print("<h2>New Products</h2>");

Print( "<br>“. $newproduct1name);

?>

</body></html>

Variable set & used

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

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

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

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

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

So…

• Try writing [yet another] html file calling a php file that uses $_Request

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

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

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

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.

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

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)

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

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 ("<a href=\"$filename\">");

• 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.

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

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

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

Pre Banner example (NOT ACCURATE)

• Table of courses:• Each record represents one course.Course identifier: MAT3530Cross-listing: NME3520Name: Creating Databases for Web Applications

• ASSUMING a course has at most one pre-requisite, each record has field that

Pre-req: MAT1420NOTE: the pre-reqs are more complicated, since there are

many possibilities. This would require a new table.

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

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.

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

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

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

Caution

• Defining (designing) a database is not trivial.

• May require revision.

• Interview process with clients/customers/systems owners can take time.

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

What should we do?

• Budget (past and future expenses, dates)

• Favorite web sites

• Music collection (with features)

• Courses taken (grades) / will take

• Candidates

• ?

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

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!

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

Preview

Team projects: take one of my sample projects at http://faculty.purchase.edu/jeanine.meyer/db/examples.html

• understand and present to class

• make enhancements

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

Admission

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

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

Trivia game

• Questions table– Question id– Question text– Question answer– Question category– Question score

• Player table• History table

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

Trivia database: players

• Player id

• Player name

• Player password

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

Trivia database: history

• Event id

• Player id

• Question id

• Result (right or wrong)

• Time stamp

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

Entity Relationship Diagram

QuestionsQues idTextAnswerCategoryscore

PlayersPlayer idPlayer namePassword

HistoryhistoryIDQues idPlayer idResultTime stamp

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

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

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

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