Download presentation
Presentation is loading. Please wait.
1
Creating databases for web applications
Library. Student database examples. Homework: Complete class example. Catch up on source postings. Do creation and simple entry and retrieval for a database.
2
Oscar snafu relevant? Yes, because mistakes can happen.
A computer system (application) is Software Hardware People Procedures Modifications may be to add networking and/or “cloud”, perhaps change Hardware to Environment
3
So… Procedure for people who present
Check label on outside of envelope BEFORE going on stage Add message to check to teleprompter If problem, signal off-stage to get correct envelope Repeat check after opening envelope Procedure for distribution of envelopes ?
4
Library? Show information (ERD) and process (DFD)
5
Simple example Students: name, gpa, department
6
Storyboard createtable showstudents.php showdepartments.php
enterstudent.html enterstudent.php
7
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
8
create the one table Fields id name department GPA
So what should the SQL and what should all the code be? Write it down.
9
<?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.<br>\n"); } else { print ("The table, $tname, was not created. <br>\n"); mysqli_close($link); ?>
10
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….
11
enterstudent.html <html><head><title>Input student </title></head> <body> <form action="enterstudent.php"> Student Name <input type="text" name="sn"/><br/> Department <input type="text" name="sd"><br/> GPA <input type=“number" name="sgpa” min=“0” max=“6.0” /><br/> <input type="submit" value="STORE"/> </form> </body> </html>
12
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.
13
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 ?>
14
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.
15
showstudents.php <html> <head><title>List contents of students table</title></head><body> <?php require("connectcode.php"); $query="SELECT * FROM students"; $result=mysqli_query($link, $query); print("<table border='1'>"); print("<tr><th>Student</th><th>Department</th><th>GPA </th></tr>"); while ($row=mysqli_fetch_array($result)) { print("<tr>"); print("<td> ".$row['sname']."</td>"); print ("<td>".$row['sdept'] ."</td>"); print ("<td>".$row['sgpa']."</td>"); print ("</tr>"); } mysqli_close($link); ?> </table> </body> </html>
16
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.
17
showdepartments Aggregate functions
count average sum … Use names (aliases) in the SELECT statement ORDER BY GROUP BY
18
from showdepartments.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("<table border='1'>"); print("<tr><th>Department</th><th>Average GPA </th><th>COUNT </th></tr>"); while ($row=mysql_fetch_array($result)) { print("<tr>"); print("<td> ".$row['sdept']."</td>"); print ("<td>".$row['a'] ."</td>"); print ("<td>".$row['c']."</td>"); print ("</tr>"); } mysqli_close($link); ?> </table>
19
Variation Impose condition on the GROUPed data
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";
20
SELECT WHERE condition and JOIN conditions are done on individual records. HAVING condition is on GROUPed data. Look at my examples AND use sources.
21
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.
22
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
23
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 improve quality, decrease bugs Choose best technologies
24
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.
25
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 may put restrictions on us. Why?
26
fileupload1.html <html>
<head><title>Get file name </title></head><body> <form action="fileupload2.php" method="post" enctype="multipart/form-data"> <label for="file">Filename:</label> <input type="file" name="file" id="file" /> <br /> <input type="submit" name="submit" value="Submit" /> </form> </body> </html> 26
27
fileupload2.php <?php if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < )) { if ($_FILES["file"]["error"] > 0) echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; } else 27
28
fileupload2.php, cont. $target .= $_FILES["file"]["name"];
{ echo "Upload: " . $_FILES["file"]["name"] . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; 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; } } } 28
29
fileupload2.php else { echo "Invalid file"; } ?> 29
30
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 We will have people go to the board.
31
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.
32
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”)
33
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
34
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
35
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
36
Classwork/homework Devise [more] SQL statements
Study sources Keep experimenting with creating tables, simple html and php scripts for data entry and for display. Produce your equivalent of student database
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.