ADVM420- Class #4 Web Design with PHP and MySQL Adding and Listing from a MySQL Database.

Slides:



Advertisements
Similar presentations
Designing Tables in Microsoft Access By Ed Lance.
Advertisements

MS-Access XP Lesson 1. Introduction to MS-Access Database Management System Software (DBMS) Store data in databases Database is a collection of table.
Keys, Referential Integrity and PHP One to Many on the Web.
Faculty of Sciences and Social Sciences HOPE PHP & MySQL Stewart Blakeway FML 213
Database A collection of related information stored on a computer and organized in a manner that allows access, retrieval, and use of that data.
Access Lecture 1 Database Overview and Creating Tables Create an Employee Table.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2010 All Rights Reserved. 1.
Database terms Mr. Brunton.
1 CS428 Web Engineering Lecture 23 MySQL Basics (PHP - VI)
What is MySQL? MySQL is a database. The data in MySQL is stored in database objects called tables. A table is a collections of related data entries and.
© Yanbu University College YANBU UNIVERSITY COLLEGE Management Science Department © Yanbu University College Module 6:WEB SERVER AND SERVER SIDE SCRPTING,
ACCESS CHAPTER 1. OBJECTIVES Tables Queries Forms Reports Primary and Foreign Keys Relationship.
CSCI 6962: Server-side Design and Programming
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
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.
Slide 8-1 CHAPTER 8 Using Databases with PHP Scripts: Using MySQL Database with PHP.
MySQL in PHP – Page 1 of 17CSCI 2910 – Client/Server-Side Programming CSCI 2910 Client/Server-Side Programming Topic: MySQL in PHP Reading: Williams &
INTERNET APPLICATION DEVELOPMENT For More visit:
Copyright © 2003 by Prentice Hall Module 4 Database Management Systems 1.What is a database? Data hierarchy and data organization Field, record, file,
ASP.NET Programming with C# and SQL Server First Edition
PHP MySQL Introduction
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizard’s Guide to PHP by David Lash.
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
MySQL + PHP.  Introduction Before you actually start building your database scripts, you must have a database to place information into and read it from.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
Web Server Administration Chapter 7 Installing and Testing a Programming Environment.
 A database is a collection of data that is organized so that its contents can easily be accessed, managed, and updated. What is Database?
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
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.
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.
1. Connecting database from PHP 2. Sending query 3. Fetching data 4. Persistent connections 5. Best practices.
Database and mySQL Week 07 Dynamic Web TCNJ Jean Chu.
Relational Databases (MS Access)
Discovering Computers Fundamentals Fifth Edition Chapter 9 Database Management.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
PHP+MySQL Integration. Connecting to databases One of the most common tasks when working with dynamic webpages is connecting to a database which holds.
® Microsoft Access 2010 Tutorial 2 Building a Database and Defining Table Relationships.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2015, Fred McClurg, All Rights.
11 3 / 12 CHAPTER Databases MIS105 Lec15 Irfan Ahmed Ilyas.
PHP Database connectivity Connecting with RDBMS and editing, adding, and deleting databases therein are all done through PHP functions.
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.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
Creating a simple database This shows you how to set up a database using PHPMyAdmin (installed with WAMP)
Chapter 8 Manipulating MySQL Databases with PHP PHP Programming with MySQL 2 nd Edition.
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.
Introduction to MySQL Ullman Chapter 4. Introduction MySQL most popular open-source database application Is commonly used with PHP We will learn basics.
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizard’s Guide to PHP by David Lash.
CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.
Introduction to MySQL  Working with MySQL and MySQL Workbench.
Notes: **A Row is considered one Record. **A Column is a Field. A Database is…  an organized set of stored information usually on one topic  a collection.
COM621: Advanced Interactive Web Development Lecture 10 PHP and MySQL.
The Web Wizard’s Guide to PHP by David Lash
CS320 Web and Internet Programming SQL and MySQL
Unix System Administration
Database application MySQL Database and PhpMyAdmin
ISC440: Web Programming 2 Server-side Scripting PHP 3
Web Systems Development (CSC-215)
CS3220 Web and Internet Programming SQL and MySQL
Web Programming– UFCFB Lecture
Chapter 4 Introduction to MySQL.
MySQL Database System Installation Overview SQL summary
CS3220 Web and Internet Programming SQL and MySQL
Introduction to MS ACCESS
Presentation transcript:

ADVM420- Class #4 Web Design with PHP and MySQL Adding and Listing from a MySQL Database

Objectives Create our first MySQL database table tblSurvey Create a PHP script to display the data from this table Create an HTML add form & PHP script system to add new data into the table

What is a Database? A Database is a storage mechanism A Database is a collection of “tables” Eg. A Customer, Products, Orders Each table has a number of “fields” Eg. Name, Address, City, Phone, Age Each field has a specific “datatype” Eg. Character, Integer, Float, Date Each field may also have “properties” Auto-increment, index, unique, default

Primary MySQL Datatypes Varchar – variable length character text Text – unlimited length text (memo) Int – 4-byte integer ( to ) BigInt – 8-byte(?) integer (huge!) Single – small floating point numbers Double – large floating point numbers Decimal – fixed decimal (money 10,2) Date – Date only DateTime – Combined date and time

Other Database Features A Database also has a security system. MySQL’s security system is based on a username, password and host computer MySQL does not yet fully support referential integrity We also need a MySQL Database Manager to allow creation & maintenance of: Tables, fields, field properties Users, security, processes, configuration

2 MySQL Database Managers MySQL Database Administration Systems allow us to manage to our remote databases by creating tables, fields and users. phpMyAdmin A web-based alternative to MySQL Control Center Download and Install the phpMyAdmin from:  MySQL Control Center A Window’s based database management system Download and Install the MySQL Control Center from:   An ODBC driver is also available from MySQL.com

MySQL Control Center / phpMyAdmin

Installing phpMyAdmin To install phpMyAdmin: Download phpMyAdmin from: Expand the ZIP file and copy all to: ~yourname/www/phpMyAdmin Configure phpMyAdmin as shown on next page…

Configuring phpMyAdmin Find and open the config.inc.php file Modify the following lines: $cfg['PmaAbsoluteUri'] = ' $cfg['Servers'][$i]['host'] = ‘localhost'; $cfg['Servers'][$i]['auth_type'] = 'http'; Open phpMyAdmin:

Example: Survey Table Create a table called tblSurvey that has the following fields and properties: ID: int 4, auto-increment, primary key Name: varchar 60, required Children: int 4, default 0 Gender: varchar 1, not required Interests: varchar 50, not required AgeGroup: varchar 20, not required Comments: text, not required Modified: timestamp, required Do not use spaces or special characters in the field names Test your table by adding two or more rows of data!

MySQL in PHP MySQL is an extension to PHP you must ensure it is installed (it is by default) There are a set of ~40 mysql functions: All are prefixed with mysql_ Approximately 8 are used frequently Similar to other database extensions: Interbase, PostgreSQL, Oracle & Others See documentation for details…

What is SQL, anyway? SQL – Structured Query Language A language for Creating, Reading, Updating and Deleting data in a database (CRUD) Using PHP text strings, we need to “build” the required SQL statements and ask MySQL to execute them…

Four essential SQL statements SELECT – for viewing RecordSets SELECT * FROM tblSurveys ORDER BY Name; INSERT – for adding records INSERT INTO tblSurveys (Name, Children) VALUES (‘Bob’,4); UPDATE – for changing records UPDATE tblSurveys SET Name=‘Bob’, Children=5 WHERE (ID=2) DELETE – for deleting records DELETE FROM tblSurveys WHERE (ID=2);

SELECT Queries: Seven Steps to Viewing Data There are 7 steps to viewing data: 1. Connect to the mySQL server $link = mysql_connect(”host”,”user”,”pass”); 2. Select the database to use mysql_select_db(”db”); 3. Build the query you want to execute $query = "SELECT * FROM my_table"; 4. Execute the query $result = mysql_query($query); 5. Process the results $line = mysql_fetch_array($result, MYSQL_ASSOC); //Etc.. 6. Free the result set mysql_free_result($result); 7. Close the connection mysql_close($link);

SELECT Queries: Quick View of a Table… <?php $link = mysql_connect("host", "user", "password") or die("Could not connect"); mysql_select_db("my_database") or die("Could not select database"); $query = "SELECT * FROM my_table"; $result = mysql_query($query) or die("Query failed"); print " \n"; while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { print "\t \n"; foreach ($line as $col_value) { print "\t\t $col_value \n"; } print "\t \n"; } print " \n"; mysql_free_result($result); mysql_close($link); ?>

INSERT Queries: 6 Steps to adding data There are 6 steps to adding data: Get the users input Validate it – garbage in garbage out!! Connect to the mySQL server $link = mysql_connect(”host”,”user”,”pass”); Select the database to use mysql_select_db(”db”); Build the query to do the insert $query = “INSERT INTO my_table (field1,field2) VALUES (‘text’,999)"; Execute the query $result = mysql_query($query); Close the connection mysql_close($link);

INSERT Queries: an example $link = mysql_connect("my_server", "User", "Pass") or die("Could not connect"); mysql_select_db("my_database") or die("Could not select database"); $query = "INSERT INTO my_table ". " (Name,Children,Gender,Interests,AgeGroup,Comments) ". " VALUES (". "'". $name. "',". $children. ",". "'". join($gender,’,’). "',". "'". join($interests,’,’). "',". "'". $agegroup. "',". "'". $comments. "'". ")"; print “$query ”; $result = mysql_query($query) or die(mysql_error()); mysql_close($link);

Formatting your output So far, our viewing scripts have been very simple – how to produce “pretty” output? Column headings Currency and date formatting Use of Colours Let’s modify SurveyDump.php to show the data as formatted output Create the new file as: SurveyView.php

SurveyView.php /* Connecting, selecting database */ $link = mysql_connect("sequel.macewan.ca", "Username", "Password") or die(mysql_error()); mysql_select_db("DBName") or die(mysql_error()); /* Performing SQL query */ $query = "SELECT *,UNIX_TIMESTAMP(Modified) as ModDate FROM tblSurvey ORDER BY Name"; $result = mysql_query($query) or die(mysql_error()); /* Printing results in HTML */ print " \n"; print " ID \n"; print " Name \n"; print " Modified \n"; print " \n"; while ($row_data = mysql_fetch_array($result, MYSQL_ASSOC)) { print " \n"; print " ".$row_data['ID']." \n"; print " ".$row_data['Name']." \n"; print " ".date("F j, Y g:i a",$row_data['ModDate'])." \n"; print " \n"; } print " \n"; mysql_free_result($result);

Create a New Table: tblLinks Create a table called tblLinks that has the following fields and properties: int_Link_ID: int 4, auto-increment, primary key txt_Title: varchar 60, required url_Hyperlink: varchar 255, required mem_Description: text, not required dat_Modified_Date: Timestamp, automatic txt_Modified_By: varchar 20, required

Self-test: Link Add System Create a form called LinkAdd.html that will allow a user to add a link to your database, including: Hyperlink, Title, Description Create a form processing script called LinkSave.php that adds this link to the database, automatically setting: aut_Modified_By to the user’s IP address Hint: Modify an example from last class

Self-test: List the Links Modify the listing program to show the hyperlinks in the database Show the links as clickable using tags Test your system by adding and listing 5 of your favorite links

Discussion Now we have way too many links: How will we delete them? How will we edit them?