Creating PHPs to Insert, Update, and Delete Data CS 320.

Slides:



Advertisements
Similar presentations
Keys, Referential Integrity and PHP One to Many on the Web.
Advertisements

PHP and MySQL Database. Connecting to MySQL Note: you need to make sure that you have MySQL software properly installed on your computer before you attempt.
Lecture 6/2/12. Forms and PHP The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input When dealing with HTML forms.
Web Database Programming Connecting Database to Web.
Chapter 31 Basic Form-Processing Techniques JavaServer Pages By Xue Bai.
Action Queries CS 320. Review: SQL Command Types  Data Definition Language (DDL)  Used to create and modify database objects  Data Manipulation Language.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
DT211/3 Internet Application Development JSP: Processing User input.
Creating Web Page Forms. Objectives Describe how Web forms can interact with a server-based program Insert a form into a Web page Create and format a.
Intermediate PHP & MySQL
PHP and MySQL Web Development tMyn1 PHP and MySQL Web Development When you install PHP, you can select from a number of extensions. The MySQL support in.
Creating Database Tables CS 320. Review: Levels of data models 1. Conceptual: describes WHAT data the system contains 2. Logical: describes HOW the database.
PHP Scripts HTML Forms Two-tier Software Architecture PHP Tools.
Figure 1. Hit analysis in 2002 of database-driven web applications Hits by Category in 2002 N = 73,873 Results Reporting 27% GME 26% Research 20% Bed Availability.
Uploading Files. Why? By giving a user the option to upload a file you are creating an interactive page You can enable users have a greater web experience.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
Advance Database Management Systems Lab no. 5 PHP Web Pages.
Application Development Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries.
Reading Data in Web Pages tMyn1 Reading Data in Web Pages A very common application of PHP is to have an HTML form gather information from a website's.
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.
XP Tutorial 6New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Creating Web Page Forms Designing a Product Registration Form Tutorial.
INTERNET APPLICATION DEVELOPMENT For More visit:
ACM Web Development Workshop - PHP By Luis Torres.
© Yanbu University College YANBU UNIVERSITY COLLEGE Management Science Department © Yanbu University College Module 6:WEB SERVER AND SERVER SIDE SCRPTING,
Advanced Web 2012 Lecture 4 Sean Costain PHP Sean Costain 2012 What is PHP? PHP is a widely-used general-purpose scripting language that is especially.
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:
Server-side Scripting Powering the webs favourite services.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
Mark Dixon Page 1 23 – Web applications: Writing data to Databases using PhP.
PHP meets MySQL.
9 Chapter Nine Compiled Web Server Programs. 9 Chapter Objectives Learn about Common Gateway Interface (CGI) Create CGI programs that generate dynamic.
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.
Creating Dynamic Web Pages Using PHP and MySQL CS 320.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
MySQL Databases & PHP Integration Using PHP to write data to, and retrieve data from, a MySQL database.
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
Accessing Your MySQL Database from the Web with PHP (Ch 11) 1.
Database Access with PHP and MySQL CS356 Examples from Web Database Applications, by Hugh E. Williams & David Lane, O'Reilly, 2002.
Website Development with PHP and MySQL Saving Data.
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.
Controlling Web Site Access Using Logins CS 320. Basic Approach HTML form a php page that collects the username and password  Sends them to second PHP.
>> PHP: Insert Query & Form Processing. Insert Query Step 1: Define Form Variables Step 2: Make DB Connection Step 3: Error Handling Step 4: Define the.
HTLM Forms CS3505. Form Handling in Browser html User Files out form WEbBROWSErWEbBROWSEr User read response submit Get URL?input html Get file html script.
SQL ACTION QUERIES AND TRANSACTION CONTROL CS 260 Database Systems.
NMD202 Web Scripting Week5. What we will cover today PHP & MySQL Displaying Dynamic Pages Exercises Modifying Data PHP Exercises Assignment 1.
Microsoft FrontPage 2003 Illustrated Complete Integrating a Database with a Web Site.
Creating a simple database This shows you how to set up a database using PHPMyAdmin (installed with WAMP)
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.
Since you’ll need a place for the user to enter a search query. Every form must have these basic components: – The submission type defined with the method.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
Unit 4 Working with data. Form Element HTML forms are used to pass data to a server. A form can contain input elements like text fields, checkboxes, radio-buttons,
XP Tutorial 6New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Creating Web Page Forms Designing a Product Registration Form Tutorial 6.
Web Database Programming Using PHP
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Web Database Programming Using PHP
PHP Overview PHP: Hypertext Preprocessor Server-Side Scripting
Web Systems Development (CSC-215)
Web DB Programming: PHP
Lecture 2 - SQL Injection
Database Access with PHP and MySQL
Presentation transcript:

Creating PHPs to Insert, Update, and Delete Data CS 320

Approach For Inserting Data Create first page (HTML or PHP) to collect user inputs using an HTML form  Passes inputs as parameters to the second page Create second page (PHP) to execute the INSERT query and display appropriate response (success or failure) depending on the query outcome  Tip: write the insert query with its values hard coded before trying to write it using the values the user submits

Inserting New Data insert succeeds insert fails Page 1 ProductInput.html Page 2 ProductInsert.php *Complete HTML and PHP pages are available in D2L

Review: Web Tag Attributes Name: identifies the form internally within the Web page Action: specifies the action the Web server performs with the form parameter list  Usually specifies a script or program that runs on the Web server Method: how the Web server processes the parameters  GET: passes the parameters as a list on the URL  POST: sends the parameters directly to the receiving program

GET and POST Parameter Passing GET: passes the parameters as a list in the URL POST: sends the parameters directly to the receiving program without displaying them in the URL Strategy:  Use GET while you are developing PHP pages  Switch to POST when you are ready to deploy the pages Parameters Action

Review: Web form input name attributes Specifies how a parameter value passed by a Web form is referenced in the server program  Good idea: give the inputs the same names as the corresponding database fields

Referencing Form Parameters in PHPs General syntax: Specific example:  The name you give to a form’s input in the previous page is what you use for form_parameter_name in $_REQUEST $_REQUEST["parameter_name"]

Referencing Form Parameters in PHPs Remember that the parameter is coming from the page that calls the current page: Code in InsertProduct.php:

Steps for Inserting Data Using a PHP 1. Receive input parameters and assign them to variables 2. Create the query 3. Execute the query  If it fails, display the failure message  Otherwise, display the success message (good idea to redisplay the table records also)

PHP With INSERT Command <?php //Put code here to connect to MySQL and specify the database //assign the parameter values to variables $prod_desc = $_REQUEST['prod_desc']; $prod_cost = $_REQUEST['prod_cost']; $prod_price = $_REQUEST['prod_price']; //Specify the query $query = "INSERT INTO candy_product (prod_desc, prod_cost, prod_price) ". "VALUES ('$prod_desc',$prod_cost,$prod_price)"; //Execute the query $result = mysql_query($query) //Show alternate failures message when you are done with development or die("SQL Error: ". mysql_error(). " "); //Display success message echo $msg = "Record successfully inserted"; //Add code here to retrieve and display CANDY_PRODUCT table contents //so user can see result of insert ?> Character data type must be in single quotes Concatenation operator to show INSERT command on multiple lines From InsertProduct.php

Updating Data Page 1: ProductList.php Select record to update Page 3: UpdateProduct.php Confirmation (show message, updated product information or error message) Page 2: ViewProductRecord.php Display single record layout for selected record Passes selected prod_id as parameter Passes prod_id and updated values

Displaying retrieved data values in selection list: <?php $query = "SELECT prod_id, prod_desc FROM candy_product"; $result = mysql_query($query) or die("SQL Error: ". mysql_error(). " "); ?> Select a product for updating: "> From ProductList.php

Displaying retrieved values in form inputs: Receive the prod_id value as a parameter and store it as a variable Create a new form and specify prod_id as a hidden input Retrieve the current values for the selected prod_id's other values Create text inputs whose values are the current prod_id record values See: ViewProductRecord.php

Save prod_id parameter as a variable and retrieve current product values: <?php //save passed parameter as a variable $prod_id = $_REQUEST['prod_id']; //Specify the query $query = "SELECT prod_desc, prod_cost, prod_price FROM candy_product ". "WHERE prod_id = $prod_id"; //Store the result $result = mysql_query($query) or die("SQL Error: ". mysql_error(). " "); ?> From ViewProductRecord.php

Create a form, add prod_id as a hidden form input, display other values as form text input values: Update Product Information " /> Description: " type="text" size="30" maxlength="30" /> Cost: " Price: " /> From ViewProductRecord.php

Update the CANDY_PRODUCT table and display success message //save passed parameters as variables $prod_id = $_REQUEST['prod_id']; $prod_desc = $_REQUEST['prod_desc']; $prod_cost = $_REQUEST['prod_cost']; $prod_price = $_REQUEST['prod_price']; //Create UPDATE query $query = "UPDATE candy_product ". "SET prod_desc = '$prod_desc', ". "prod_cost = $prod_cost, ". "prod_price = $prod_price ". "WHERE prod_id = $prod_id"; //Execute the query $result = mysql_query($query) or die("SQL Error: ". mysql_error(). " "); //Display page content echo $msg = "Record successfully updated"; //Add code to retrieve and display CANDY_PRODUCT table contents //here so user can see result of update From UpdateProduct.php Place character data type value in single quotes

Selecting Data Using a Hyperlink Table Page 1: Product_Hyperlink_Table.php Page 3: UpdateProduct.php Confirmation (show message, updated product information or error message) Page 2: ViewProductRecord.php Display single record layout for selected record

Deleting Data From a business standpoint, be very cautious about allowing users to delete data  Better to archive You can't delete parent records that are referenced as foreign keys

Deleting Data Delete succeeds Delete fails Page 1 ProductListDelete.html Page 2 ProductDelete.php

PHP With DELETE Command <?php //Put code here to connect to MySQL and specify the database //save passed parameter as variable $prod_id = $_REQUEST['prod_id']; //Create DELETE query $query = "DELETE FROM candy_product ". "WHERE prod_id = $prod_id"; //Execute the query $result = mysql_query($query) or die("SQL Error: ". mysql_error(). " "); //Display page content echo $msg = "Record successfully deleted"; //Add code to retrieve and display CANDY_PRODUCT table contents //here so user can see result of delete From ProductDelete.php

PHP/Database Debugging Strategies Develop incrementally!  Add one line of code  Test  Repeat Use method="GET" to view parameter names and values View the page source code  Make sure you are testing the most current version of your PHP page Display SQL queries directly on the Web page  Then run the query in MySQL Query Workbench

To debug a query, comment out all other code and display it on the Web page: <?php //Put code here to connect to MySQL and specify the database //assign the parameter values to variables $prod_desc = $_REQUEST['prod_desc']; $prod_cost = $_REQUEST['prod_cost']; $prod_price = $_REQUEST['prod_price']; //Specify the query $query = "INSERT INTO candy_product (prod_desc, prod_cost, prod_price) ". "VALUES ('$prod_desc',$prod_cost,$prod_price)"; //Store the result //$result = mysql_query($query) //Show alternate failures message when you are done with development //or die("SQL Error: ". mysql_error(). " "); //Display success message //echo $msg = "Record successfully inserted"; echo $query ?>

Additional PHP Resources Tutorials  Basic PHP  MySQL Search Google for specific topics…