24
Claus Brabrand, ITU, Denmark SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE Dec 16, 2011 Claus Brabrand ((( [email protected] ))) Associate Professor, Ph.D. ((( Programming, Logic, and Semantics ))) IT University of Copenhagen introduction to SCRIPTING, DATABASES, SYSTEM ARCHITECTURE EVEN MORE WEB SERVICE EXAMPLES

introduction to SCRIPTING DATABASES SYSTEM ARCHITECTURE · Claus Brabrand, ITU, Denmark SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE [ 3 ] Dec 16, 2011 Web Service: Quiz 1) Data Model:

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: introduction to SCRIPTING DATABASES SYSTEM ARCHITECTURE · Claus Brabrand, ITU, Denmark SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE [ 3 ] Dec 16, 2011 Web Service: Quiz 1) Data Model:

Claus Brabrand, ITU, Denmark SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE Dec 16, 2011

Claus Brabrand ((( [email protected] ))) Associate Professor, Ph.D. ((( Programming, Logic, and Semantics ))) IT University of Copenhagen

introduction to SCRIPTING, DATABASES, SYSTEM ARCHITECTURE

EVEN MORE WEB SERVICE EXAMPLES

Page 2: introduction to SCRIPTING DATABASES SYSTEM ARCHITECTURE · Claus Brabrand, ITU, Denmark SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE [ 3 ] Dec 16, 2011 Web Service: Quiz 1) Data Model:

[ 2 ] Claus Brabrand, ITU, Denmark SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE Dec 16, 2011

Agenda

n  Web Service: Quiz n  Web Service: ITU Project Broker (A9) n  About the Exam n  Questions

Page 3: introduction to SCRIPTING DATABASES SYSTEM ARCHITECTURE · Claus Brabrand, ITU, Denmark SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE [ 3 ] Dec 16, 2011 Web Service: Quiz 1) Data Model:

[ 3 ] Claus Brabrand, ITU, Denmark SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE Dec 16, 2011

Web Service: Quiz 1) Data Model:

2) Data Transact's:

3)

qid question answer wrong1 wrong2

1 What is the average rainfall in Amazon basin?

2000 mm per year

200 mm per year

20000 mm per year

2 Who won the EURO 92? Denmark Germany France

questions:

INSERT INTO questions (question, answer, wrong1, wrong2) VALUES ('Is it Friday?', 'Yes', 'No', 'Maybe') ; SELECT * FROM questions ;

Select questions: Add quesion:

AUTO

Site

map

:

Page 4: introduction to SCRIPTING DATABASES SYSTEM ARCHITECTURE · Claus Brabrand, ITU, Denmark SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE [ 3 ] Dec 16, 2011 Web Service: Quiz 1) Data Model:

[ 4 ] Claus Brabrand, ITU, Denmark SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE Dec 16, 2011

Create Tables

CREATE TABLE questions ( qid INT PRIMARY KEY AUTO_INCREMENT, question TEXT NOT NULL, answer TEXT NOT NULL, wrong1 TEXT NOT NULL, wrong2 TEXT NOT NULL );

qid question answer wrong1 wrong2

1 What is the average rainfall in Amazon basin?

2000 mm per year

200 mm per year

20000 mm per year

2 Who won the EURO 92? Denmark Germany France

questions:

AUTO

Page 5: introduction to SCRIPTING DATABASES SYSTEM ARCHITECTURE · Claus Brabrand, ITU, Denmark SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE [ 3 ] Dec 16, 2011 Web Service: Quiz 1) Data Model:

[ 5 ] Claus Brabrand, ITU, Denmark SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE Dec 16, 2011

start.html <html> <body> <h3>Welcome to the Quizzzzzz</h3> <a href="add_q.html">ADD question</a> <p/> <a href="quiz.php">TAKE quiz</a> </body> </html>

Page 6: introduction to SCRIPTING DATABASES SYSTEM ARCHITECTURE · Claus Brabrand, ITU, Denmark SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE [ 3 ] Dec 16, 2011 Web Service: Quiz 1) Data Model:

[ 6 ] Claus Brabrand, ITU, Denmark SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE Dec 16, 2011

add_q.html <html> <body> <h3>Please Add a Question</h3> <form action="add_q.php"> Question:<br/> <input type="text" size="80" name="question"/>? <p/> The Answer:</br> <input type="text" size="80" name="answer"/> <p/> A Wrong Answer:</br> <input type="text" size="80" name="wrong1"/> <p/> Another Wrong Answer:</br> <input type="text" size="80" name="wrong2"/> <p/> <input type="submit" value="Add Q!"/> </form> </body> </html>

Page 7: introduction to SCRIPTING DATABASES SYSTEM ARCHITECTURE · Claus Brabrand, ITU, Denmark SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE [ 3 ] Dec 16, 2011 Web Service: Quiz 1) Data Model:

[ 7 ] Claus Brabrand, ITU, Denmark SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE Dec 16, 2011

add_q.php <?php include("fn_mydb_connect.php"); include("fn_input_validation.php"); mydb_connect(); $question = $_REQUEST['question'] ; $answer = $_REQUEST['answer'] ; $wrong1 = $_REQUEST['wrong1'] ; $wrong2 = $_REQUEST['wrong2'] ; chk_text($question); chk_text($answer); chk_text($wrong1); chk_text($wrong2); mysql_query("INSERT INTO questions (question, answer, wrong1, wrong2) VALUES ('$question', '$answer', '$wrong1', '$wrong2');"); header("Location: start.html"); mysql_close(); ?>

Page 8: introduction to SCRIPTING DATABASES SYSTEM ARCHITECTURE · Claus Brabrand, ITU, Denmark SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE [ 3 ] Dec 16, 2011 Web Service: Quiz 1) Data Model:

[ 8 ] Claus Brabrand, ITU, Denmark SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE Dec 16, 2011

quiz.php (I/II) <html><body> <?php include("fn_mydb_connect.php"); mydb_connect(); $rows = mysql_query( "SELECT * FROM questions ;" ); $count = mysql_num_rows($rows) ; // gives the # rows in select result $r = rand(1, $count); // pick a random (row) number for ($i = 0; $i < $r; $i++) { $row = mysql_fetch_array($rows); } $question = $row['question']; $qid = $row['qid']; $answers[0] = $row['answer']; $answers[1] = $row['wrong1']; $answers[2] = $row['wrong2']; shuffle($answers);

( ... continues ... )

Page 9: introduction to SCRIPTING DATABASES SYSTEM ARCHITECTURE · Claus Brabrand, ITU, Denmark SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE [ 3 ] Dec 16, 2011 Web Service: Quiz 1) Data Model:

[ 9 ] Claus Brabrand, ITU, Denmark SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE Dec 16, 2011

quiz.php (II/II)

echo "The question is: <b>$question</b>?" ; echo "<form action='eval.php'> <input type='hidden' name='qid' value='$qid'/> <input type='radio' name='answer' value='$answers[0]'/> $answers[0] <input type='radio' name='answer' value='$answers[1]'/> $answers[1] <input type='radio' name='answer' value='$answers[2]'/> $answers[2] <p/> <input type='submit' value='Done!'/> </form>" ; mysql_close(); ?> </body></html>

( ... continued ... )

Page 10: introduction to SCRIPTING DATABASES SYSTEM ARCHITECTURE · Claus Brabrand, ITU, Denmark SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE [ 3 ] Dec 16, 2011 Web Service: Quiz 1) Data Model:

[ 10 ] Claus Brabrand, ITU, Denmark SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE Dec 16, 2011

eval.php (I/II) <html><body> <?php include("fn_mydb_connect.php"); mydb_connect(); $answer = $_REQUEST['answer']; $qid = $_REQUEST['qid']; chk_text($answer); chk_heltal($qid); $rows = mysql_query("SELECT * FROM questions WHERE qid = '$qid';"); $row = mysql_fetch_array($rows); if ($row == NULL) { error("No such question!!!"); } $correct_answer = $row['answer'];

( ... continues ... )

Page 11: introduction to SCRIPTING DATABASES SYSTEM ARCHITECTURE · Claus Brabrand, ITU, Denmark SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE [ 3 ] Dec 16, 2011 Web Service: Quiz 1) Data Model:

[ 11 ] Claus Brabrand, ITU, Denmark SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE Dec 16, 2011

eval.php (II/II)

echo "You answered...: <p align='center'><font size='+2'><b>$answer</b></font></p> which is...:" ; if ($answer == $correct_answer) { echo "<p align='center'> <font size='+2' color='green'><b>Correct!</b></font></p>"; } else { echo "<p align='center'> <font size='+2' color='red'><b>Incorrect!</b></font></p>"; echo "The correct answer is...: <p align='center'><font size='+2'><b>$correct_answer</b></font></p>"; } mysql_close(); ?> <p/> [ <a href="quiz.php">Quiz again</a> | <a href="start.html">Back to quiz</a> ] </body></html>

( ... continued ... )

Page 12: introduction to SCRIPTING DATABASES SYSTEM ARCHITECTURE · Claus Brabrand, ITU, Denmark SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE [ 3 ] Dec 16, 2011 Web Service: Quiz 1) Data Model:

[ 12 ] Claus Brabrand, ITU, Denmark SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE Dec 16, 2011

Agenda

n  Web Service: Quiz n  Web Service: ITU Project Broker (A9) n  About the Exam n  Questions

Page 13: introduction to SCRIPTING DATABASES SYSTEM ARCHITECTURE · Claus Brabrand, ITU, Denmark SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE [ 3 ] Dec 16, 2011 Web Service: Quiz 1) Data Model:

[ 13 ] Claus Brabrand, ITU, Denmark SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE Dec 16, 2011

Web Service: Project Broker 1) Data Model:

2) Data Transactions:

3) Sitemap:

pid title description name email password

1 Man on Mars We need to put a man on Mars before China. Email for details.

Barack Obama

obama@ hotmail.com

top*secret

2 Freedom of speech demo

We are organizing a demo against the freedom of speech!

Anonymous

[email protected]

12345

projects:

INSERT INTO projects (title, description, ...) VALUES ('Man on the Moon', 'We need to...', ...) ;

SELECT * FROM projects ORDER BY title ;

Select all projects:

Add project:

AUTO

SELECT * FROM projects WHERE pid = '7';

Select specific project: UPDATE projects SET description = '..', title = '..' WHERE pid = '7';

Update specific project:

password

title

Site

map

:

Page 14: introduction to SCRIPTING DATABASES SYSTEM ARCHITECTURE · Claus Brabrand, ITU, Denmark SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE [ 3 ] Dec 16, 2011 Web Service: Quiz 1) Data Model:

[ 14 ] Claus Brabrand, ITU, Denmark SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE Dec 16, 2011

Create Tables

CREATE TABLE projects ( pid INT PRIMARY KEY AUTO_INCREMENT, title VARCHAR(255) NOT NULL UNIQUE, description TEXT NOT NULL, name VARCHAR(100) NOT NULL, email VARCHAR(100) NOT NULL, password VARCHAR(10) NOT NULL );

pid title description name email password

1 Man on Mars We need to put a man on Mars before China. Email for details.

Barack Obama

obama@ hotmail.com

top*secret

2 Freedom of speech demo

We are organizing a demo against the freedom of speech!

Anonymous

[email protected]

12345

projects:

AUTO

Page 15: introduction to SCRIPTING DATABASES SYSTEM ARCHITECTURE · Claus Brabrand, ITU, Denmark SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE [ 3 ] Dec 16, 2011 Web Service: Quiz 1) Data Model:

[ 15 ] Claus Brabrand, ITU, Denmark SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE Dec 16, 2011

projects.php <html><body><h3>List of Projects:</h3><ul> <?php include("fn_mydb_connect.php"); include("fn_input_validation.php"); mydb_connect(); $rows = mysql_query("SELECT * FROM projects ORDER BY title;"); while ( $row = mysql_fetch_array($rows) ) { $pid = $row['pid']; $title = $row['title']; $description = $row['description']; $name = $row['name']; $email = $row['email']; echo "<li><b>$title</b><br/><em>$description</em><br> ( by <a href='mailto:$email'>$name</a> )<br> [ <a href='edit_project.php?pid=$pid'>edit project</a> ]</li><p/>" ; } mysql_close(); ?> </ul> <a href="new_project.html">Create new project</a> </body></html>

Page 16: introduction to SCRIPTING DATABASES SYSTEM ARCHITECTURE · Claus Brabrand, ITU, Denmark SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE [ 3 ] Dec 16, 2011 Web Service: Quiz 1) Data Model:

[ 16 ] Claus Brabrand, ITU, Denmark SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE Dec 16, 2011

new_project.html <html> <body> <h3>New Project</h3> <form action="new_project.php"> Title:<br> <input type="text" size="40" name="title" /><p/> Description:<br> <textarea name="description" rows="10" cols="40">Enter Decription...</textarea></p> Name:<br> <input type="text" size="40" name="name" /><p/> Email:<br> <input type="text" size="40" name="email" /><p/> Password:<br> <input type="password" size="10" name="password" /><p/> <input type="submit" value="Create Project!" /><p/> </form> </body> </html>

Page 17: introduction to SCRIPTING DATABASES SYSTEM ARCHITECTURE · Claus Brabrand, ITU, Denmark SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE [ 3 ] Dec 16, 2011 Web Service: Quiz 1) Data Model:

[ 17 ] Claus Brabrand, ITU, Denmark SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE Dec 16, 2011

new_project.php <?php include("fn_mydb_connect.php"); include("fn_input_validation.php"); mydb_connect(); $title = $_REQUEST['title']; $description = $_REQUEST['description']; $name = $_REQUEST['name']; $email = $_REQUEST['email']; $password = $_REQUEST['password']; chk_text($title); chk_text_multi_line($description); chk_text_multi_line($name); chk_email($email); chk_text_multi_line($password); mysql_query("INSERT INTO projects (title, description, name, email, password) VALUES ('$title', '$description', '$name', '$email', '$password');"); header("Location: projects.php"); mysql_close(); ?>

function chk_text_multi_line ( $t ) { if ( preg_match('/^[^<>]+$/', $t) == 0 ) { error("Please input valid text, no '&gt;' or '&lt;'"); } }

Page 18: introduction to SCRIPTING DATABASES SYSTEM ARCHITECTURE · Claus Brabrand, ITU, Denmark SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE [ 3 ] Dec 16, 2011 Web Service: Quiz 1) Data Model:

[ 18 ] Claus Brabrand, ITU, Denmark SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE Dec 16, 2011

edit_project.php <html><body><h3>Edit Project</h3><?php include("fn_mydb_connect.php"); include("fn_input_validation.php"); mydb_connect(); $pid = $_REQUEST['pid']; chk_heltal($pid); $rows = mysql_query("SELECT * FROM projects WHERE pid = '$pid';"); $row = mysql_fetch_array($rows); if ($row == NULL) { error("No such Project!"); } $title = $row['title']; $description = $row['description']; echo "<form action='update_project.php'> <input type='hidden' name='pid' value='$pid' /> Title:<br> <input type='text' size='40' name='title' value='$title'/><p/> Decription:<br> <textarea name='description' rows='10' cols='40'>$description</textarea></p> <input type='password' name='password'/> <input type='submit' value='Update'/> </form>"; mysql_close(); ?></body></html>

Page 19: introduction to SCRIPTING DATABASES SYSTEM ARCHITECTURE · Claus Brabrand, ITU, Denmark SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE [ 3 ] Dec 16, 2011 Web Service: Quiz 1) Data Model:

[ 19 ] Claus Brabrand, ITU, Denmark SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE Dec 16, 2011

update_project.php <?php include("fn_mydb_connect.php"); include("fn_input_validation.php"); mydb_connect(); $pid = $_REQUEST['pid']; $title = $_REQUEST['title']; $description = $_REQUEST['description']; $password = $_REQUEST['password']; chk_heltal($pid); chk_text_multi_line($title); chk_text_multi_line($description); chk_text_multi_line($password); // get password from database $rows = mysql_query("SELECT * FROM projects WHERE pid = '$pid';"); $row = mysql_fetch_array($rows); if ($row == NULL) { error("No such Project!"); } $database_password = $row['password']; if ($password != $database_password) { error("Password incorrect!"); } mysql_query("UPDATE projects SET title = '$title', description = '$description' WHERE pid = '$pid';"); header("Location: projects.php"); mysql_close(); ?>

Page 20: introduction to SCRIPTING DATABASES SYSTEM ARCHITECTURE · Claus Brabrand, ITU, Denmark SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE [ 3 ] Dec 16, 2011 Web Service: Quiz 1) Data Model:

[ 20 ] Claus Brabrand, ITU, Denmark SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE Dec 16, 2011

Agenda

n  Web Service: Quiz n  Web Service: ITU Project Broker (A9) n  About the Exam n  Questions

Page 21: introduction to SCRIPTING DATABASES SYSTEM ARCHITECTURE · Claus Brabrand, ITU, Denmark SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE [ 3 ] Dec 16, 2011 Web Service: Quiz 1) Data Model:

[ 21 ] Claus Brabrand, ITU, Denmark SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE Dec 16, 2011

Intended Learning Outcomes After the course, the student is expected to be able to…:

n  plan and develop medium sized web applications using the scripting language, PHP;

n  design small MySQL databases; n  construct PHP scripts that interact with databases via SQL; n  describe the techniques behind DB-driven web applications; n  describe the fundamental system architectural

considerations behind web applications so as to be able to communicate and collaborate with programmers and technologists.

Page 22: introduction to SCRIPTING DATABASES SYSTEM ARCHITECTURE · Claus Brabrand, ITU, Denmark SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE [ 3 ] Dec 16, 2011 Web Service: Quiz 1) Data Model:

[ 22 ] Claus Brabrand, ITU, Denmark SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE Dec 16, 2011

Exam

n  48-hour take-home exam: n  From: Wednesday, January 18, 2012 at 13:00 n  To: Friday, January 20, 2012 at 13:00

n  Design + Develop a Web Service n  (using the four steps + description)

Page 23: introduction to SCRIPTING DATABASES SYSTEM ARCHITECTURE · Claus Brabrand, ITU, Denmark SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE [ 3 ] Dec 16, 2011 Web Service: Quiz 1) Data Model:

[ 23 ] Claus Brabrand, ITU, Denmark SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE Dec 16, 2011

Previous Exam Assignments

n  Movie Subscription Service n  Online Coffee Shop n  Online Shoe Shop n  Appartments 4 Rent n  News Archive n  Buy'n'Sell Service n  Bike 2 Work Service n  Vote About It

Page 24: introduction to SCRIPTING DATABASES SYSTEM ARCHITECTURE · Claus Brabrand, ITU, Denmark SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE [ 3 ] Dec 16, 2011 Web Service: Quiz 1) Data Model:

Claus Brabrand, ITU, Denmark SCRIPTING, DATABASES, & SYSTEM ARCHITECTURE Dec 16, 2011

( Have a nice weekend )

Any Questions?