© Anselm SpoerriInfo + Web Tech Course Information Technologies Info + Web Tech Course Anselm Spoerri PhD (MIT) Rutgers University

Slides:



Advertisements
Similar presentations
PHP II Interacting with Database Data. The whole idea of a database-driven website is to enable the content of the site to reside in a database, and to.
Advertisements

Keys, Referential Integrity and PHP One to Many on the Web.
© Anselm SpoerriInfo + Web Tech Course Information Technologies Info + Web Tech Course Anselm Spoerri PhD (MIT) Rutgers University
© Anselm SpoerriInfo + Web Tech Course Information Technologies Info + Web Tech Course Anselm Spoerri PhD (MIT) Rutgers University
© Anselm SpoerriInfo + Web Tech Course Information Technologies Info + Web Tech Course Anselm Spoerri PhD (MIT) Rutgers University
© Anselm SpoerriInfo + Web Tech Course Information Technologies Info + Web Tech Course Anselm Spoerri PhD (MIT) Rutgers University
© Anselm SpoerriInfo + Web Tech Course Information Technologies Info + Web Tech Course Anselm Spoerri PhD (MIT) Rutgers University
© Anselm SpoerriInfo + Web Tech Course Information Technologies Info + Web Tech Course Anselm Spoerri PhD (MIT) Rutgers University
Information Technologies Anselm Spoerri PhD (MIT)
MySQL and PHP By Trevor Adams.
© Anselm SpoerriInfo + Web Tech Course Information Technologies Info + Web Tech Course Anselm Spoerri PhD (MIT) Rutgers University
© Anselm Spoerri Web Design Information Visualization Course Prof. Anselm Spoerri
Lecture 3 – Data Storage with XML+AJAX and MySQL+socket.io
Application Development Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries.
© Yanbu University College YANBU UNIVERSITY COLLEGE Management Science Department © Yanbu University College Module 6:WEB SERVER AND SERVER SIDE SCRPTING,
Session 5: Working with MySQL iNET Academy Open Source Web Development.
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:
Advanced Database Management System Lab no. 11. SQL Commands (for MySQL) –Update –Replace –Delete.
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizard’s Guide to PHP by David Lash.
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.
PHP meets MySQL.
Accessing MySQL with PHP IDIA 618 Fall 2014 Bridget M. Blodgett.
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.
PHP MySQL. SQL: Tables CREATE TABLE tablename { fieldname type(length) extra info,... } Extra info: –NULL (allows nulls in this field) –Not NULL (null.
Creating Dynamic Web Pages Using PHP and MySQL CS 320.
School of Computing and Information Systems CS 371 Web Application Programming PHP – Forms, Cookies, Sessions and Database.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
SYST Web Technologies SYST Web Technologies Databases & MySQL.
PHP and MySQL CS How Web Site Architectures Work  User’s browser sends HTTP request.  The request may be a form where the action is to call PHP.
© Anselm Spoerri Web Design Information Visualization Course Prof. Anselm Spoerri
PHP+MySQL Integration. Connecting to databases One of the most common tasks when working with dynamic webpages is connecting to a database which holds.
© Anselm Spoerri Web Design Information Visualization Course Prof. Anselm Spoerri
HTML, PHP, and MySQL: Putting It All Together. Making a Form Input tags Types: “text” “radio” “checkboxes” “submit”
Intro to DatabasesClass 4 SQL REVIEW To talk to the database, you have to use SQL SQL is used by many databases, not just MySQL. SQL stands for Structured.
Creating PHPs to Insert, Update, and Delete Data CS 320.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP & MySQL.
PHP Database connectivity Connecting with RDBMS and editing, adding, and deleting databases therein are all done through PHP functions.
PHP Database Processing CIS 1715 Web Technologies.
© Anselm SpoerriInfo + Web Tech Course Information Technologies Info + Web Tech Course Anselm Spoerri PhD (MIT) Rutgers University
© Anselm Spoerri Web Design Information Visualization Course Prof. Anselm Spoerri
NMD202 Web Scripting Week5. What we will cover today PHP & MySQL Displaying Dynamic Pages Exercises Modifying Data PHP Exercises Assignment 1.
Creating a simple database This shows you how to set up a database using PHPMyAdmin (installed with WAMP)
MySQL. Is a SQL (Structured Query Language) database server. Can be accessed using PHP with embedded SQL Queries Supports Large DB’s, 60,000 tables with.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Chapter 8 Manipulating MySQL Databases with PHP PHP Programming with MySQL 2 nd Edition.
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
MySQL MySQL and PHP – interacting with a database.
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizard’s Guide to PHP by David Lash.
Software-Projekt 2008 Seminarvortrag“Short tutorial of MySql“ Wei Chen Verena Honsel.
PHP and SQL Server: Connection IST 210: Organization of Data IST2101.
COM621: Advanced Interactive Web Development Lecture 10 PHP and MySQL.
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Introduction to Web programming
Web Design and Development
Chapter 19 PHP Part III Credits: Parts of the slides are based on slides created by textbook authors, P.J. Deitel and H. M. Deitel by Prentice Hall ©
Introduction to Web programming
BASIC PHP and MYSQL Edward S. Flores.
ISC440: Web Programming 2 Server-side Scripting PHP 3
Web Systems Development (CSC-215)
Tutorial 6 PHP & MySQL Li Xu
Web Programming– UFCFB Lecture
Introduction to Web programming
Information Technologies Anselm Spoerri PhD (MIT)
Information Technologies Anselm Spoerri PhD (MIT)
Presentation transcript:

© Anselm SpoerriInfo + Web Tech Course Information Technologies Info + Web Tech Course Anselm Spoerri PhD (MIT) Rutgers University

© Anselm SpoerriInfo + Web Tech Course Lecture 11 - Overview PHP, MySQL and CSS  Dynamic Website Exercise 5 Demo –Credentials Management –MySQL: Key Commands –Recap: Login.php | connectDatabase.php –Form with Radio Buttons | $_POST | PHP to Display $_POST –Create MySQL Table for Data from Form with Radio Buttons –Add Form Data to MySQL table (use MySQL Workbench to monitor) –Use PHP to Display Data in MySQL Table –Compute Average Score and Control Display with CSS Lectures – Week 11 Content

© Anselm SpoerriInfo + Web Tech Course Recap – Credential Management MySQL Workbench Connect to studentweb.comminfo.rutgers.edu using NetID (or studentweb) credentials Connect to MySQL using MySQL credentials  Can talk to server with MySQL database from your computer SFTP Connect to studentweb.comminfo.rutgers.edu using NetID (or studentweb) credentials  Can upload HTML and PHP pages to server with MySQL database  Set permissions PHP ‒ Create login_yourlastname.php file that stores MySQL credentials so we can access MySQL from inside PHP code

© Anselm SpoerriInfo + Web Tech Course Recap – Set Up Connection to PHP & MySQL Server Open Filezilla and select Site created for “studentweb” Host = “studentweb.comminfo.rutgers.edu” Select SFTP and Normal User = “yourNetIDusername” (or yourSCILSNETusername) Password = “yourNetIDpassword” (or yourSCILSNETpassword) Need to specify path to course folder in “Remote Site” slot in Filezilla: /www/studentweb.comminfo.rutgers.edu/htdocs/YourCourseFolder/username Example: YourCourseFolder = class When moving files to server, remember to set permissions 755 URL to test page in Browser: “ Example:

© Anselm SpoerriInfo + Web Tech Course MySQL – Key Commands Command Parameter(s)Meaning ALTER DATABASE, TABLEAlter DATABASE or TABLE BACKUP TABLEBack up TABLE CREATE DATABASE, TABLE,Create DATABASE or TABLE DELETE (expression with TABLE & ROW)Delete ROW from TABLE DESCRIBE TABLEDescribe the TABLE'S columns DROP DATABASE,TABLEDelete DATABASE or TABLE INSERT (expression with data)Insert data RENAME TABLERename TABLE SHOW (too many items to list)List item's details UPDATE (expression with data)Update an existing record USE databaseUse database MySQL Resource:

© Anselm SpoerriInfo + Web Tech Course Recap – MySQL & PHP: Process Steps The process of using MySQL with PHP: 1.Connect to MySQL mysql_connect 2.Select the database to use mysql_select_db 3.Build a query string $query = "SELECT * FROM tableName "; 4.Perform the query $result = mysql_query($query); 5.Retrieve the results and output it to a web page –$rows = mysql_num_rows($result); –$row = mysql_fetch_row($result); 6.Repeat Steps 3 to 5 until all desired data retrieved. 7.Disconnect from MySQL (usually done automatically)

© Anselm SpoerriInfo + Web Tech Course Class Demos of MySQL & PHP Steps 0-4 last lecture –login.php –Connect to Your MySQL Database –Run Query –Display Results –Display Results (faster) Steps 5-10 this lecture –Form with Radio Buttons and Validate with JavaScript –Use $_POST and PHP to Display $_POST –Create MySQL Table for Data from Form with Radio Buttons –Add Form Data to MySQL table (use MySQL Workbench to monitor) –Use PHP to Display Data in MySQL Table –Compute Average Score and Control Display with CSS

© Anselm SpoerriInfo + Web Tech Course Recap – MySQL & PHP: login.php for your database – Step 0 Create login_lastname.php file <?php // login_lastname.php make sure to place in personal folder $db_hostname = 'localhost'; $db_database = 'YourCourseFolder_NetIDusername'; $db_username = 'yourMySQLusername'; $db_password = 'yourMySQLpassword'; ?> Upload to Server, Set Permissions, View Source in Browser Now that you have login.php file saved, you can include it in any PHP files that will need to access the database by using the require_once statement.

© Anselm SpoerriInfo + Web Tech Course Recap – MySQL & PHP: Connect to Database – Steps 1-4 <?php // query.php require_once 'login_lastname.php'; $db_server = mysql_connect($db_hostname, $db_username, $db_password); if (!$db_server) die("Unable to connect to MySQL: ". mysql_error()); mysql_select_db($db_database) or die("Unable to select database: ". mysql_error()); $query = "SELECT * FROM classics"; $result = mysql_query($query); if (!$result) die ("Database access failed: ". mysql_error()); $rows = mysql_num_rows($result); for ($j = 0 ; $j < $rows ; ++$j){ $row = mysql_fetch_row($result); echo 'Author: '. $row[0]. ' '; echo 'Title: '. $row[1]. ' '; echo 'Year: '. $row[3]. ' '; echo 'ISBN: '. $row[5]. ' '; ?>

© Anselm SpoerriInfo + Web Tech Course Form & JavaScript – Step 5 Create Form with sets of Radio Buttons –Want to collect feedback about tools covered in class –Need to assign name to each set of radio buttons –Name used as attribute in MySQL database –Use “tool1”, “tool2”, … to have flexibility Validate Form with JavaScript –Want to make sure radio buttons selected … what to use? –Use JavaScript function “radioButtonSelected” –Create JavaScript function “validate”

© Anselm SpoerriInfo + Web Tech Course Form & HTML5 Validation – Step 5 Create Form with sets of Radio Buttons –Want to collect feedback about tools covered in class –Need to assign name to each set of radio buttons –Name used as attribute in MySQL database –Use “tool1”, “tool2”, … to have flexibility Validate Form using HTML5 –Need to use HTML5 doctype and encoding –Want to make sure radio buttons selected … how to leverage HTML5? –Include required in each input of type="radio"

© Anselm SpoerriInfo + Web Tech Course $_POST $_POST = All User Input –Web server bundles up all user input and puts it into an array named $_POST. $_POST is an associative array –Each field has an element in the array named after that field. in showResults.php $tool1 = $_POST['tool1'];

© Anselm SpoerriInfo + Web Tech Course $_POST and PHP – Step 6 Create PHP page to Display $_POST Simple, but hand-coded way to display $_POST –echo ("Info Tech Tool 1 = ". $_POST['tool1']); Flexible way to display $_POST –Create PHP function to display $_POST contents foreach ( $postarray as $tool => $score ) { echo "$tool". " = ". "$score "; } Remember to use: formTest_step6.html and showResults_step6.php as well as to update action in form and update reference for login_username.php

© Anselm SpoerriInfo + Web Tech Course Create “tools” SQL Table – Step 7 Database = yourusername has been created for you USE DatabaseToUse; // tells SQL which database to use Note: if YourCourseFolder contains – hyphens then will trigger SQL error You can use the grave mark ` to escape names that contain reserved lexical symbols such as - Example: USE `class _studentUsername`; Workaround in MySQL Workbench: Double-click icon of database to use in left-hand panel below “Schemas” to tell SQL which database to use. CREATE TABLE tools ( tool1 INT UNSIGNED, tool2 INT UNSIGNED, id INT UNSIGNED NOT NULL AUTO_INCREMENT KEY) ENGINE MyISAM; DESCRIBE tools; // Delete table if want to start fresh DROP TABLE tools;

© Anselm SpoerriInfo + Web Tech Course Connect to MySQL Database and Add Form Data – Step 8 Connect to MySQL See Step 1 make sure to link to your login file Make Sure Form Data Specified isset($_POST['tool1']) Sanitize Form Data function mysql_fix_string($string) { if (get_magic_quotes_gpc()) $string = stripslashes($string); return mysql_real_escape_string($string); } Add Form Data to Table = tools test in MySQL Workbench $query = "INSERT INTO tools (tool1, tool2) VALUES". "('$tool1', '$tool2')"; Remember to use: formTest_step8.html and showResults_step8.php as well as to update action in form and update reference for login_username.php

© Anselm SpoerriInfo + Web Tech Course Display Table Contents – Step 9 Display Contents of Table = “tools” See Steps 2-4 Make sure to link to your login file Make sure to specify to use tools table $query = "SELECT * FROM tools"; $result = mysql_query($query, $db_server); if (!$result) die ("Database access failed: ". mysql_error()); $rows = mysql_num_rows($result); for ($j = 0 ; $j < $rows ; ++$j){ $row = mysql_fetch_row($result); // need to consult table to identify correct index for field echo ' Tool 1: '. $row[0]. ' '; echo ' Tool 2: '. $row[1]. ' '; } Remember to use: formTest_step9.html and showResults_step9.php as well as to update action in form and update reference for login_username.php

© Anselm SpoerriInfo + Web Tech Course Display SUM and Average Scores using CSS – Step 10 Retrieve SUM data $query = "SELECT SUM(tool1), SUM(tool2) FROM tools"; Display SUM and Average $firstrow = mysql_fetch_row($result); echo ' SUM for Info Tool 1: '. $firstrow[0]. ' and AVE = '. number_format($firstrow[0] / $rows, 2). ' '; CSS class controls display of SUM and Average Define CSS class in style tag inside of head tag Need to escape \ the quotation marks echo ' '; Remember to have closing tag Remember to use: formTest_step10.html and showResults_step10.php as well as to update action in form and update reference for login_username.php

© Anselm SpoerriInfo + Web Tech Course Exercise 5 – Next Steps Once step10 works, need to prepare to be able to track 10 tools ‒ “tools” table needs fields for tool1, …, tool10 ‒ Drop existing “tools” table ‒ Create new “tools” table with the needed fields (modify SQL query in step 7) ‒ Form needs to be modified to collect data for 10 tools ‒ validate function needs to be modified so that fields for 10 tools are checked ‒ PHP code needs to be modified to examine and record, retrieve & display data from MySQL table for 10 tools

© Anselm SpoerriInfo + Web Tech Course Reminders Watch out ‒ Spelling Errors ‒ Filename Errors login.php action=“file.php” ‒ Missing Closing " or ) or } ‒ Missing ; ‒ Missing Permissions ‒ Saved in Wrong Location on Server  Check Easy Things First