Creating databases for web applications Database datatypes. Creating database Homework: Create tables in database. Add records to database.

Slides:



Advertisements
Similar presentations
Faculty of Sciences and Social Sciences HOPE PHP & MySQL Stewart Blakeway FML 213
Advertisements

Creating Database Tables CS 320. Review: Levels of data models 1. Conceptual: describes WHAT data the system contains 2. Logical: describes HOW the database.
Structured Query Language SQL: An Introduction. SQL (Pronounced S.Q.L) The standard user and application program interface to a relational database is.
ADVM420- Class #4 Web Design with PHP and MySQL Adding and Listing from a MySQL Database.
Phonegap Bridge – File System CIS 136 Building Mobile Apps 1.
Introduction To Databases IDIA 618 Fall 2014 Bridget M. Blodgett.
Chapter 9 Collecting Data with Forms. A form on a web page consists of form objects such as text boxes or radio buttons into which users type information.
MySql In Action Step by step method to create your own database.
Creating Databases SELECT. UPDATE. Demonstrate projects. Classwork / Homework: Prepare to choose teams & projects.
Lecture 3 – Data Storage with XML+AJAX and MySQL+socket.io
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
© Yanbu University College YANBU UNIVERSITY COLLEGE Management Science Department © Yanbu University College Module 6:WEB SERVER AND SERVER SIDE SCRPTING,
Creating databases for web applications
Session 5: Working with MySQL iNET Academy Open Source Web Development.
1 Chapter 8 – Working with Databases spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.
Creating databases for web applications Walk before run? Songs with features example. Retrieve information from database. Recordsets. Arrays. Loops. Homework:
INTERNET APPLICATION DEVELOPMENT For More visit:
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizard’s Guide to PHP by David Lash.
Relational Database Concepts. Let’s start with a simple example of a database application Assume that you want to keep track of your clients’ names, addresses,
Creating databases for web applications SQL. Systems design. ER diagrams. Data flow diagrams. Storyboards. Homework: Plan database and applications for.
Creating databases for web applications Play quizzes Testing process regular expressions: form validation PHP coding handling forms Homework: regular expressions.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
Dbwebsites 2.1 Making Database backed Websites Session 2 The SQL… Where do we put the data?
PHP meets MySQL.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
CHAPTER 8 Database: SQL, MySQL. Topics  Introduction  Relational Database Model  Relational Database Overview: Books.mdb Database  SQL (Structured.
Chapter 4 Introduction to MySQL. MySQL “the world’s most popular open-source database application” “commonly used with PHP”
15/10/20151 PHP & MySQL 'Slide materials are based on W3Schools PHP tutorial, 'PHP website 'MySQL website.
LIS651 lecture 6 mySQL Thomas Krichel
Introduction to MySQL Lab no. 10 Advance Database Management System.
PHP MySQL Introduction. MySQL is the most popular open-source database system. What is MySQL? MySQL is a database. The data in MySQL is stored in database.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
CSC 2720 Building Web Applications Database and SQL.
SYST Web Technologies SYST Web Technologies Databases & MySQL.
 Whether using paper forms or forms on the web, forms are used for gathering information. User enter information into designated areas, or fields. Forms.
M1G Introduction to Database Development 2. Creating a Database.
Creating Databases CSS example. One-way encryption. Passwords. Security issues. Work session.. Homework: Making unique posting on encryption, passwords,
ITCS373: Internet Technology Lecture 5: More HTML.
Creating databases for Web Applications php basics. ing. phpMyAdmin to set up MySQL. Homework: Use phpMyAdmin. Make posting with [unique] source on.
14. Uploading Files to MySQL Database. M. Udin Harun Al Rasyid, S.Kom, Ph.D Desain dan.
Creating Databases Local storage. join & split Classwork: show 1 table application. Share designs for oscars application. Adaptive select. Homework: [Catch.
Creating Databases for web applications [Complete presentations] More SQL Class time: discuss final projects. Do posting if you have not done it.
Creating databases for web applications Library. New example: student database. Homework: Complete class example. Catch up on source postings. Do creation.
Advanced Web 2012 Lecture 3 Sean Costain What is a Database? Sean Costain 2012 A database is a structured way of dealing with structured information.
Creating a simple database This shows you how to set up a database using PHPMyAdmin (installed with WAMP)
Sql DDL queries CS 260 Database Systems.
Creating Databases for Web applications Server side vs client side PHP basics Homework: Get your own versions of sending working: both html and Flash!
Creating databases for web applications Report on using sources, getting examples working. Retrieve information from database. Recordsets. Arrays. Loops.
CMPT 258 Database Systems The Relationship Model (Chapter 3)
Creating Databases One-way encryption. Passwords. Security issues. Data normalization. Integrity and Robustness. Homework: Finalize teams & projects. Making.
Mr C Johnston ICT Teacher
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
>> Introduction to MySQL. Introduction Structured Query Language (SQL) – Standard Database Language – Manage Data in a DBMS (Database Management System)
CHAPTER 10 PHP MySQL Database
Creating Databases for Web Applications 3-Tier. Design vs Function vs Content. More SQL. More php. Homework: work on final projects.
Class 3Intro to Databases Class 4 Simple Example of a Database We’re going to build a simple example of a database, which will allow us to register users.
Relational Databases and MySQL. Relational Databases Relational databases model data by storing rows and columns in tables. The power of the relational.
Microsoft Office 2013 Try It! Chapter 4 Storing Data in Access.
Starting with Oracle SQL Plus. Today in the lab… Connect to SQL Plus – your schema. Set up two tables. Find the tables in the catalog. Insert four rows.
Introduction to MySQL Ullman Chapter 4. Introduction MySQL most popular open-source database application Is commonly used with PHP We will learn basics.
Creating Databases for Web applications Making a table of table information. Reprise on database design. SQL. Classwork/Homework: Projects! Postings.
Creating databases for web applications
Presented by: Teererai Marange
Creating Databases Local storage. join & split
Creating Databases SELECT. UPDATE. Demonstrate projects. Do not track.
CS3220 Web and Internet Programming SQL and MySQL
Chapter 4 Introduction to MySQL.
MySQL Database System Installation Overview SQL summary
Presentation transcript:

Creating databases for web applications Database datatypes. Creating database Homework: Create tables in database. Add records to database.

Database design Not easy! Formal methods exist. Review: databases are made up of –Tables: tables made up of Records: records made up of fields Speaking of rows and columns is misleading Critical issue: fixed number of fields, though a specific field may be optional (aka not required) –NOT NULL in MySQL jargon –MySQL does support variable length strings.

Critical requirement Want information present only one place in database –Don’t need to change your address multiple times. Reference integrity –Reference in one record point to existing record E.g., Orders point to actual customer Order detail point to actual orders

Data types Terminology varies for different DBMS products Performance (speed) of operations varies with different datatypes Size varies with different datatypes Performance and size limits are points of competition among the different DBMS products

MySQL datatypes: numbers INT (aka INTEGER), can be UNSIGNED (Size 4 bytes = 32 bits) TINYINT, SMALLINT, MEDIUMINT, BIGINT –Different sizes FLOAT (4 bytes), DOUBLE (8 bytes), can specify precision within these limits more

MySQL datatypes, strings CHAR(specified length) VARCHAR(maximum length) –trade off time to handle variable length versus space to not always store max. length TINYBLOB short, variable length string, up to 255 characters BLOB, TEXT variable length string MEDIUMBLOB, MEDIUMTEXT, LONGBLOB, LONGTEXT

MySQL datatypes: enum ENUM –Specify one of a set of values –Stored as an integer, with 0 indicated unset or not in the specified set –Doing this may be more efficient because built-in MySQL routines do the searching

MySQL datatypes: date/time DATE TIME DATETIME YEAR TIMESTAMP

Tables Specify one field as the primary key Primary keys are unique IN THAT TABLE –Let the DBMS create the primary key OR –Depend on intrinsic value that is guaranteed to be unique addresses ISBN numbers ? A field in one table may be a foreign key. This is a reference to a primary key in another table. MORE ON THIS LATER.

Database Assume database itself is created for us AND we have permissions to create new tables. NOTE: permissions can be set by MySQL commands, including queries sent by php. Start off talking general SQL and then specific php and MySQL

phpMyAdmin Can use this to create tables in your one database. The next slides show SQL Create statements to do this. NOTE THE https!!!!! Next sign in with your equivalent of and password THEN use the special MySQL user name and password. This was in the Readme file. You can change the password. Click on password name (jmeyer_db) on the left and then you can create a new table. EVERYONE get to this point!

Create table example CREATE TABLE movies ( mid INT NOT NULL AUTO_INCREMENT PRIMARY KEY, mname CHAR(30), mdesc TEXT, myear YEAR )

Create table example CREATE TABLE players ( pid INT NOT NULL AUTO_INCREMENT PRIMARY KEY, pname CHAR(30), score INT NOT NULL, lastplayed DATE )

Create example CREATE TABLE games ( gid INT NOT NULL AUTO_INCREMENT PRIMARY KEY, pid INT, gtime TIMESTAMP, score INT ) The pid field will refer to / have the value of the pid field (the primary key) of a specific player. Here in this table, it is called a foreign key.

Foreign keys Some versions of MySQL (and other DBMS) have ways to specify that the pid value is a foreign key pid INT REFERENCE players The DBMS then will check to make sure it is a valid value. Since my JavaScript & php coding should guarantee this in various places, I omit specifying this from my examples. –Extra credit opportunity for posting on this. –It may be that letting MySQL catch errors and trapping the errors in the php is a better approach

Class Exercises Write the CREATE TABLE statement for a table MySQL generated id, course 'number' (MAT ), name, cap, credits, teacher, time slot, building Published book: the ISBN number can serve as primary key, title, year of publication Your own idea

Inherent challenges for implementation … in developing database applications database persists. That is, data in database lasts (that is the whole point) so my/your/our testing needs to be aware of that. I forgot the passwords for larry, curly and moe.

2 table example Bookmarks application: finders and sites r/research/register.htmlhttp://socialsoftware.purchase.edu/jeanine.meye r/research/register.html r/research/addsite.htmlhttp://socialsoftware.purchase.edu/jeanine.meye r/research/addsite.html r/research/showsites.phphttp://socialsoftware.purchase.edu/jeanine.meye r/research/showsites.php r/research/showsitesbycategory1.phphttp://socialsoftware.purchase.edu/jeanine.meye r/research/showsitesbycategory1.php

Create the tables <?php function createtable($tname,$fields) { global $DBname, $link; $query = "DROP TABLE $tname"; 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"); } ?>

Creating bookmark tables <?php require("opendbo.php"); $tname = "sitesfinders"; $fields="sid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, stitle char(50), sdate DATE, surl char(100), sdescription TEXT, scategory char(30), finderid INT "; createtable($tname, $fields);

$tname = "finders"; $fields = "finderid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, username char(50), epw char(64)"; createtable($tname,$fields); mysql_close($link); ?>

adding a site addsite.html Use two scripts (files) Make input fields new types. This should/will provide some checking, aka form validation. addsite.php

Simplified addsite.html Add site Your name for site: Date: Site description: Web address: Category:

Simplified addsite.php Add song to database <?php require("opendbo.php"); $tname = "sites"; $stitle=addslashes($_GET["stitle"]); $sdate=addslashes($_GET["sdate"]); $sdesc=addslashes($_GET["sdesc"]); $surl=addslashes($_GET["surl"]); $scat = addslashes($_GET["scat"]); $query = "INSERT INTO $tname values ('0','$stitle','$sdate','$surl','$sdesc','$scat')"; $result = mysqli_query($link,$query); if ($result) { print("The site was successfully added. \n"); } else { print ("The site was NOT successfully added. \n"); } ?>

Actual addsite application Uses localStorage for id and pw Encodes password using clientside coding –SHA256 algorithm Checks id and password using finders table Only then adds to sitesfinders table

NOTE Should do checking on Date and URL fields. –This can be enhancement for the group project. The addslashes 'escapes' any special characters. We see that again in next example. –May not be necessary all the time.

Show bookmark/sites all the sites (and all the fields for each record) select one of the categories –use php to get list of DISTINCT category entries –use php to select exactly those In both cases, the query returns an array of associative (by name) arrays. The names are the field names of the table. My php code creates a table, one row for each record. I chose to display the URLs as the href of an a tag AND as the contents of the a element.

showsites.php List contents of sites table <?php require("opendbo.php"); $query="SELECT * FROM sites ORDER BY sdate DESC"; $result=mysqli_query($link, $query); print(" "); print(" Title URL Date Description Category "); while ($row=mysqli_fetch_array($result)) { print(" "); print(" ".$row['stitle']." "); print (" ".$row['surl']." "); print (" ".$row['sdate']." "); print (" ".$row['sdescription']." "); print (" ".$row['scategory']." "); print (" "); } mysqli_close($link); ?>

show by category Two php files, one calling the other as the action of a form Form has select showsitesbycategory1.php showsitesbycategory2.php

showsitesbycategory1.php List sites in category Pick the category you want: Choices: <?php require("opendbo.php"); $query="SELECT DISTINCT scategory FROM sites"; $categories = mysqli_query($link, $query); while ($row=mysqli_fetch_array($categories)) { $cat=$row['scategory']; print (" $cat \n"); } mysqli_close($link); print (" "); print (" \n"); print (" "); mysqli_close($link); ?>

from showsitesbycategory2.php <?php $scat = $_GET['pickedcategory']; print "Sites in $scat category "; require("opendbo.php"); $query="SELECT * FROM sites WHERE scategory ='$scat' ORDER BY sdate DESC"; $result=mysqli_query($link, $query);

$NoR=mysql_num_rows($result); if ($NoR==0) { print ("No sites in that category"); } //should not happen else { print(" "); print(" Title URL Date Description "); while ($row=mysqli_fetch_array($result)) { print(" "); print(" ".$row['stitle']." "); print (" ".$row['surl']." "); print (" ".$row['sdate']." "); print (" ".$row['sdescription']." "); print (" "); } mysqli_close($link); ?>

new example: stories A story is a sequence of –scenes, each holding text and html image tags, with title and author. There may be choices at certain points. meyer/newstories/tellStory.phphttp://socialsoftware.purchase.edu/jeanine. meyer/newstories/tellStory.php Room for improvement –This is one of the projects to enhance.

3 tables stories2: holds title, author, scene, permflag plus a primary key auto-increment field generated by MySQL parentchild: holds one record for each (forward) link: parent to child (scene to a successor) plus a primary key … starts: holds one record for each scene designated as start of a story: first (pointer into stories2 table) plus a primary key …

Entity Relationship Diagram stories2 sid title scene author permflag parentchild pcid parent child starts stid first each parent, child, first is exactly 1 record in stories A record in stories2 may not be represented as a parent or child or first

Comment Could this be done other ways? Yes. –My first attempt used one table with prev and next fields

Example of SQL SELECT The php variable $next holds the current scene. Code gathers information on all child (successor) scenes to present choice to viewer. in SQL (as much as possible with $next holding a value) SELECT s.sid, s.title, s.author FROM parentchild as p JOIN stories2 as s WHERE p.parent= $next AND s.sid= p.child php: $query= "SELECT s.sid, s.title, s.author FROM parentchild as p JOIN stories2 as s "; $query.=" WHERE p.parent=$next AND s.sid=p.child";

Explanation Look at the parentchild table and pick up all the records with the parent field being a specified value. Call this group A. Look at stories2 table and pick up all records with sid being equal to a child value in group A. Think of this as a new table, consisting of portions of the old tabled joined together. for this new table, extract the sid, title and author values. –this will be used to create the form (radio buttons and text next to the radio buttons)

phpmyadmin Go to admin admin –First logon: use your id and password –Second logon: use what was sent to you in the NOTE: for our applications, we will be using php scripts, not phpmyadmin. BUT phpmyadmin may be useful for debugging.

opendbo.php file <?php global $DBname, $link; $host = ' ’; $user=" "; $password=" "; $DBname=" ”; $link = new mysqli($host,$user,$password); if ($link->connect_error) { die("Connection failed: ". $conn->connect_error); } mysqli_select_db($link,$DBname); ?>

Homework [Confirm that you can access phpmyadmin] Use phpmyadmin to create table Use code to create a table –need to drop table to create a (new) table with the same name Write html and php to INSERT records Write php to display whole table Use code in charts and posted on-line sources! –Post comments –Improve appearance –Extra credit: insert default values. Do validation. USE SOURCES!!!

Preview Will divide into teams, each teams taking one of the examples and –study it & enhance it –present to the class –Bookmark project, with userid/password system: –Tell a story: –Others: THEN do an original project (can copy parts of sample projects)