Download presentation
Presentation is loading. Please wait.
1
Creating databases for Web Applications
php basics. ing. 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 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 Generally, the interface is used to produce a customized HTML page.
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
6
<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>
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. Repeat on get versus post.
9
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>
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 and the php file will do it.
11
Storyboard sendemailfromhtmlg.html sendemailp.php
This represents what you are doing.
12
html to php Sets of html (set up) and php files
send fromhtmlp.html calls send p.php send fromhtmlg.html calls send g.php send fromhtmlgfancy.html calls send gfancy.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 addresses.
14
sendemailfromhtmlg.html <html> <head> <title>Email
15
<body> <form action="send g.php"> <p>Your (for reply) <input type="text" name="from" /> </p> <p> To <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 sent from my own html/php application </TEXTAREA> </p> <input type="submit" value="SEND" /> </form> </body> </html>
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 <?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.
21
<html> <head> <title>Email</title></head> <body>
<form action="send gfancy.php"> <p>Your (for reply) <input type="text" name="from" /> </p> <p>To <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 sent from my own html/php application </TEXTAREA> </p> <input type="submit" value="SEND" /> </form> </body> </html>
22
<html> <head> <title>php for sending 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>
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 files. You can do other things, like sending to the giving and also to another you know…. NOT MINE! Make it work!
24
geolocation example Look at code—focus on form
25
<form name="msg" action="sendemailp.php" method="post"><p>
Your (for reply)<input type=" " name="from" required/>To <input type=" " 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>
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
ob_start(); // buffering output phpinfo(); $phpinfo = ob_get_contents(); //get contents echo $phpinfo; //print out as html ?>
29
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 Here the built-in procedure is print (there is also echo).
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 Special trick just with php.
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 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) This will become more obvious with the coding.
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 ("<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. If you do think of the 3 language domains, this will be understandable, though still easy to forget. This is a preview/warning. We will go over it again.
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 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 Player table History 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 examples, you need to upload to your server account. Get html 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.