Presentation is loading. Please wait.

Presentation is loading. Please wait.

Creating databases for web applications Library. New example: student database. Homework: Complete class example. Catch up on source postings. Do creation.

Similar presentations


Presentation on theme: "Creating databases for web applications Library. New example: student database. Homework: Complete class example. Catch up on source postings. Do creation."— Presentation transcript:

1 Creating databases for web applications Library. New example: student database. Homework: Complete class example. Catch up on source postings. Do creation and simple entry and retrieval for a database.

2 Library? Show information (ERD) and process (DFD)

3 Simple example Students: name, gpa, department http://socialsoftware.purchase.edu/jeanine. meyer/studentexample/showstudents.phphttp://socialsoftware.purchase.edu/jeanine. meyer/studentexample/showstudents.php http://socialsoftware.purchase.edu/jeanine. meyer/studentexample/showdepartments. phphttp://socialsoftware.purchase.edu/jeanine. meyer/studentexample/showdepartments. php http://socialsoftware.purchase.edu/jeanine. meyer/studentexample/enterstudent.htmlhttp://socialsoftware.purchase.edu/jeanine. meyer/studentexample/enterstudent.html

4 Storyboard createtable enterstudent.html enterstudent.php showstudents.php showdepartments.php

5 Note Plenty to add –improve look of enterstudent.html –provide better feedback –add edit student info function, probably based on displaying a list –add more display functions, such as top student in each department, all students with averages over a certain amount, ? –more information

6 create the one table Fields id name department GPA So what should the SQL and what should all the code be? Write it down.

7 <?php require("connectcode.php"); $tname = "students"; $fields = "sid INT NOT NULL AUTO_INCREMENT PRIMARY KEY, sname CHAR(30), sgpa FLOAT(5,4), sdept CHAR(30)"; $query = "DROP TABLE students"; mysqli_query($link,$query); $query = "CREATE TABLE $tname ($fields)"; if (mysqli_query($link,$query)) { print ("The table, $tname, was created successfully. \n"); } else { print ("The table, $tname, was not created. \n"); } mysqli_close($link); ?>

8 Adding a record to the students table The enterstudent.html file does not use php. It displays a form that invokes enterstudent.php using the action attribute on the form. Think about what the html file would look like….

9 enterstudent.html Input student Student Name Department GPA

10 Note The names of the input fields of the form do not have to be consistent with the names of the fields in the database table. Extra credit opportunity: Report on Ruby on Rails. This is an alternative to html and php that produces code that uses these connections. The php code does insert slashes to prevent problems with names such as O'Bama. … does check that gpa is a number. Could add a tighter check that it is a number between 0 and 5 (?). The 0 for the sid field is a placeholder. The stored sid will be the autoincremented value.

11 enterstudent.php <?php require("connectcode.php"); $sn =$_GET['sn']; $sn = ADDSLASHES($sn); $sd=$_GET['sd']; $sd = ADDSLASHES($sd); $sgpa = $_GET['sgpa']; if (!is_numeric($sgpa)) { print ("Bad GPA data."); } else { $query="INSERT INTO students VALUES ("; $query.="'0','$sn',$sgpa,'$sd')"; print ("query is $query. "); if (mysqli_query($link,$query)) { print ("Student record added successfully"); } Else { print ("failure to add student record "); } } mysqli_close($link); //this depends on db link ?>

12 Note Order of fields is important for the VALUES form of the INSERT statement. There is another form: INSERT INTO students SET … Extra credit: research and see what works.

13 showstudents.php List contents of students table <?php require("connectcode.php"); $query="SELECT * FROM students"; $result=mysqli_query($link, $query); print(" "); print(" Student Department GPA "); while ($row=mysqli_fetch_array($result)) { print(" "); print(" ".$row['sname']." "); print (" ".$row['sdept']." "); print (" ".$row['sgpa']." "); print (" "); } mysqli_close($link); ?>

14 Another enhancement What to do when the table gets big? Implement 'paging' Put in buttons, perhaps using an a tag with a query string giving the page value The SELECT statement has a LIMIT feature –SELECT …. LIMIT 1, 10 –SELECT …. LIMIT 11, 10 –$query = "SELECT ….. LIMIT ". $next. ", 10 " Opportunity for research.

15 showdepartments Aggregate functions –count –average –sum –… Use names (aliases) in the SELECT statement ORDER BY GROUP BY

16 from showdepartments.php <?php require("connectcode.php"); $query="SELECT sdept, AVG(sgpa) as a, COUNT(*) as c FROM students GROUP BY sdept ORDER BY a DESC"; $result=mysqli_query($link, $query); print(" "); print(" Department Average GPA COUNT "); while ($row=mysql_fetch_array($result)) { print(" "); print(" ".$row['sdept']." "); print (" ".$row['a']." "); print (" ".$row['c']." "); print (" "); } mysqli_close($link); ?>

17 Variation Impose condition on the GROUPed data http://socialsoftware.purchase.edu/jeanine. meyer/studentexample/showbigdepartmen ts.phphttp://socialsoftware.purchase.edu/jeanine. meyer/studentexample/showbigdepartmen ts.php Change QUERY to $query="SELECT sdept, AVG(sgpa) as a, COUNT(*) as c FROM students GROUP BY sdept HAVING c>1 ORDER BY a DESC";

18 SELECT WHERE condition and JOIN conditions are done on individual records. HAVING condition is on GROUPed data. Look at my examples AND use sources.

19 Caution The word 'design' may refer to Design (plan, structure, organization, definition) of the database and the application The look of an application One of my examples: the content of the product being produced is the design of a grid using pre-defined units.

20 Systems design Focus on information –what is the information and how do [things] relate –Entity relationship diagramming is a tool –Information may exist already in the organization organizations may have products, perhaps customized, to hold their information includes dates, versions, owners, etc. Focus on function of application(s) –who does what –… using what information

21 System design slow down rush to implement [start] documentation Design and documentation may change as system is being built, deployed, refined…. Still, goal is to decrease bugs

22 ER diagram Database holds –Tables. Table represents one type of entity Row in table holds information on one example of the entity –Row in table made up of Attributes (aka fields) Lines connecting tables indicate occurrence of attribute in one table that points to a row in another table (or that table) –What is the pointer? The id of the row. This is called a foreign key. One mark of good design is robustness: ease of changing over time.

23 File upload What if the application requires users to upload files? –and asking them to use an ftp program not appropriate The ISP may not permit this. CTS put restrictions on us. Why?

24 fileupload1.html Get file name <form action="fileupload2.php" method="post" enctype="multipart/form-data"> Filename:

25 fileupload2.php <?php if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 200000)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: ". $_FILES["file"]["error"]. " "; } else

26 fileupload2.php, cont. { echo "Upload: ". $_FILES["file"]["name"]. " "; echo "Type: ". $_FILES["file"]["type"]. " "; echo "Size: ". ($_FILES["file"]["size"] / 1024). " Kb "; echo "Temp file: ". $_FILES["file"]["tmp_name"]. " "; if (file_exists($_FILES["file"]["name"])) { echo $_FILES["file"]["name"]. " already exists. "; } else { $target = "uploads/"; $target.= $_FILES["file"]["name"]; move_uploaded_file ($_FILES["file"]["tmp_name"],$target); echo "Stored as: ". $target; } } }

27 fileupload2.php else { echo "Invalid file"; } ?>

28 Classwork/Homework Application: database for movies Tables: movies, people_having_role_in_movie, people, nominations, ? What processes would you want? –what maintenance functions do you want to have? –what questions would you want to ask

29 JOINs Some questions require data from multiple tables. Tables can be connected by conditions, most typically connecting foreign keys with primary keys. SELECT …. FROM table1 as t JOIN table2 as s ON t.fid = s.fid –The fid field may be primary key in table1. It is referenced by a field in table2.

30 Multiple tables SELECT p.pname, n.award FROM people as p JOIN roles as r ON p.pid = r.pid JOIN nominations as n ON r.rid = n.rid WHERE n.award IN (“BEST LEAD ACTOR”, “BEST LEAD ACTRESS”, “BEST SUPPORTING ACTOR”, “BEST SUPPORTING ACTRESS”)

31 Tasks List all movies by name, ordered by date List all people by name with roles in a given movie, named $moviename List all directors (by name), with movie (by name) ordered by movie name List all movies by name in which someone was nominated for Best Lead Actor List all movies by name in which someone was nominated for an acting category –Best Lead Actor, Best Lead Actress, Best Supporting Actor, Best Supporting Actress

32 More List movie name, person name, award name ordered by movie name List movie name, number of people nominated, ordered from high to low –EXTRA CREDIT: figure out how not to count multiples for awards that have multiples (such as producer, technical awards List winners: movie name, person name, award

33 Research Some questions require consideration of absent records –What movies were nominated (had people nominated) for Best Movie but not Best Director –? Look up and study examples of LEFT JOINS

34 Classwork/homework Devise [more] SQL statements Keep experimenting with creating tables, simple html and php scripts for data entry and for display.


Download ppt "Creating databases for web applications Library. New example: student database. Homework: Complete class example. Catch up on source postings. Do creation."

Similar presentations


Ads by Google