PHP Queries and HTML Forms Lecture 23

Similar documents
PHP Querying. Lecture 21. Robb T. Koether. Hampden-Sydney College. Fri, Mar 2, 2018

Insertions, Deletions, and Updates

Aggregation. Lecture 7 Section Robb T. Koether. Hampden-Sydney College. Wed, Jan 29, 2014

Basic PHP. Lecture 19. Robb T. Koether. Hampden-Sydney College. Mon, Feb 26, 2108

Relational Databases

Views. Lecture 15. Robb T. Koether. Fri, Feb 16, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) Views Fri, Feb 16, / 28

XPath Lecture 34. Robb T. Koether. Hampden-Sydney College. Wed, Apr 11, 2012

Minimal Spanning Trees

Views. Lecture 15 Section 5.3. Robb T. Koether. Hampden-Sydney College. Mon, Feb 18, 2013

Selections. Lecture 4 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 22, 2014

The Critical-Path Algorithm

Triggers. Lecture 14. Robb T. Koether. Hampden-Sydney College. Wed, Feb 14, 2018

Views. Lecture 15 Section 5.3. Robb T. Koether. Hampden-Sydney College. Mon, Feb 18, 2013

Introduction to Databases

XPath. Lecture 36. Robb T. Koether. Wed, Apr 16, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, / 28

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

Form Validation. Lecture 25. Robb T. Koether. Hampden-Sydney College. Wed, Mar 23, 2018

Sampling Distribution Examples Sections 15.4, 15.5

Density Curves Sections

XQuery FLOWR Expressions Lecture 35

The Decreasing-Time Algorithm

Introduction to Databases

Solving Recursive Sequences by Iteration

while Loops Lecture 13 Sections Robb T. Koether Wed, Sep 26, 2018 Hampden-Sydney College

Scheduling and Digraphs

The Pairwise-Comparison Method

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 27, 2013

Rotations and Translations

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 26, 2014

Functional Dependencies and Normal Forms

Functional Dependencies and Normal Forms

Webpage Navigation. Lecture 27. Robb T. Koether. Hampden-Sydney College. Mon, Apr 2, 2018

LR Parsing - Conflicts

Implementing Linked Lists

Recursive Descent Parsers

XML and AJAX Lecture 28

Scope and Parameter Passing

The Traveling Salesman Problem Nearest-Neighbor Algorithm

Relational Databases Lecture 2

The Plurality-with-Elimination Method

Basic PHP Lecture 17

The string Class. Lecture 21 Sections 2.9, 3.9, Robb T. Koether. Wed, Oct 17, Hampden-Sydney College

Recursion. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 17, 2018

The Traveling Salesman Problem Brute Force Method

Building the Abstract Syntax Trees

Boxplots. Lecture 17 Section Robb T. Koether. Hampden-Sydney College. Wed, Feb 10, 2010

Boolean Expressions. Lecture 31 Sections 6.6, 6.7. Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

Scope and Parameter Passing

Street-Routing Problems

XML Attributes. Lecture 33. Robb T. Koether. Hampden-Sydney College. Wed, Apr 25, 2018

Function Definition Syntax Tree

Binary Tree Applications

Total Orders. Lecture 41 Section 8.5. Robb T. Koether. Hampden-Sydney College. Mon, Apr 8, 2013

The Coefficient of Determination

Recursive Linked Lists

PHP Arrays. Lecture 18. Robb T. Koether. Hampden-Sydney College. Mon, Mar 4, 2013

The Graphics Pipeline

PHP Arrays. Lecture 20. Robb T. Koether. Hampden-Sydney College. Wed, Feb 28, 2018

DTDs and XML Attributes

The Class Construct Part 1

Inheritance: The Fundamental Functions

Mipmaps. Lecture 35. Robb T. Koether. Hampden-Sydney College. Wed, Nov 18, 2015

jquery Lecture 34 Robb T. Koether Wed, Apr 10, 2013 Hampden-Sydney College Robb T. Koether (Hampden-Sydney College) jquery Wed, Apr 10, / 29

AJAX. Lecture 26. Robb T. Koether. Fri, Mar 21, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) AJAX Fri, Mar 21, / 16

The Projection Matrix

Linked Lists. Lecture 16 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 22, 2017

Magnification and Minification

Regular Expressions. Lecture 10 Sections Robb T. Koether. Hampden-Sydney College. Wed, Sep 14, 2016

XML. Lecture 29. Robb T. Koether. Fri, Mar 28, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) XML Fri, Mar 28, / 23

Pointers. Lecture 1 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 14, 2015

The CYK Parsing Algorithm

Abstract Data Types. Lecture 23 Section 7.1. Robb T. Koether. Hampden-Sydney College. Wed, Oct 24, 2012

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

Displaying Distributions - Quantitative Variables

Javascript Lecture 23

The Constructors. Lecture 7 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 1, 2017

Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017

MySQL Creating a Database Lecture 3

Nondeterministic Programming in C++

Stacks and their Applications

Operators. Lecture 12 Section Robb T. Koether. Hampden-Sydney College. Fri, Feb 9, 2018

The Graphics Pipeline

Recognition of Tokens

The Normal Distribution

Friends and Unary Operators

COSC Assignment 2

Basic CSS Lecture 17

Recursion. Lecture 26 Sections , Robb T. Koether. Hampden-Sydney College. Mon, Apr 6, 2015

Inheritance: The Fundamental Functions

The Traveling Salesman Problem Cheapest-Link Algorithm

Introduction to Compiler Design

List Iterators. Lecture 34 Section Robb T. Koether. Hampden-Sydney College. Wed, Apr 24, 2013

CSC 742 Database Management Systems

Programming Languages

Programming Languages

The x86 Instruction Set

Specular Reflection. Lecture 19. Robb T. Koether. Hampden-Sydney College. Wed, Oct 4, 2017

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 18, 2013

List Iterators. Lecture 27 Section Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

CS 348 Introduction to Database Management Assignment 2

Transcription:

PHP Queries and HTML Forms Lecture 23 Robb T. Koether Hampden-Sydney College Wed, Mar 14, 2018 Robb T. Koether (Hampden-Sydney College) PHP Queries and HTML FormsLecture 23 Wed, Mar 14, 2018 1 / 15

1 Retrieving Data from the Database 2 Inserting into a Database 3 Updating the Database Robb T. Koether (Hampden-Sydney College) PHP Queries and HTML FormsLecture 23 Wed, Mar 14, 2018 2 / 15

Outline 1 Retrieving Data from the Database 2 Inserting into a Database 3 Updating the Database Robb T. Koether (Hampden-Sydney College) PHP Queries and HTML FormsLecture 23 Wed, Mar 14, 2018 3 / 15

Retrieving Data from the Database We will use the company database for our examples. Suppose we want to retrieve all the information available about an employee and display it. In the HTML form, we need a text box in which to enter the employee s Social Security Number (the primary key). The PHP program (action file) will query the database to retrieve the information and then echo HTML code that will produce the display. Robb T. Koether (Hampden-Sydney College) PHP Queries and HTML FormsLecture 23 Wed, Mar 14, 2018 4 / 15

Outline 1 Retrieving Data from the Database 2 Inserting into a Database 3 Updating the Database Robb T. Koether (Hampden-Sydney College) PHP Queries and HTML FormsLecture 23 Wed, Mar 14, 2018 5 / 15

Inserting into a Database Suppose that we want to add an employee to the database. We must get from the user the employee s First and last names. Social Security number. Sex. Birthday. Salary. Department. Data about any dependents. Data about any projects. Then insert these data into the employees table, the dependents table, and the works_on table. Robb T. Koether (Hampden-Sydney College) PHP Queries and HTML FormsLecture 23 Wed, Mar 14, 2018 6 / 15

The HTML Form In the HTML form, we need Text boxes for the first name, last name, Social Security number, birthday, and salary. A radio button for the sex. A select element for the department. And so on. We could also use three select elements for the birthday: one for the month, one for the day, and one for the year. Robb T. Koether (Hampden-Sydney College) PHP Queries and HTML FormsLecture 23 Wed, Mar 14, 2018 7 / 15

The HTML Form For the department, we should construct the list of options from the database, using the departments table, and present them in a select element, but how do we do that? Robb T. Koether (Hampden-Sydney College) PHP Queries and HTML FormsLecture 23 Wed, Mar 14, 2018 8 / 15

The HTML Form For the department, we should construct the list of options from the database, using the departments table, and present them in a select element, but how do we do that? We should also get the projects that the employee works on, and present them, but they are specific to a department and we do not know yet which is his department. Robb T. Koether (Hampden-Sydney College) PHP Queries and HTML FormsLecture 23 Wed, Mar 14, 2018 8 / 15

The HTML Form For the department, we should construct the list of options from the database, using the departments table, and present them in a select element, but how do we do that? We should also get the projects that the employee works on, and present them, but they are specific to a department and we do not know yet which is his department. Later on, JQuery will allow us to do that: when the user selects a department, the list of projects will be immediately updated. Robb T. Koether (Hampden-Sydney College) PHP Queries and HTML FormsLecture 23 Wed, Mar 14, 2018 8 / 15

Inserting into the Database Insert into the Database $fname = $_POST[ fname ]; $lname = $_POST[ lname ]; $ssn = $_POST[ ssn ]; $sex = $_POST[ sex ]; $bdate = $_POST[ bdate ]; $salary = $_POST[ salary ]; $dept = $_POST[ dept ]; $query = "SELECT * FROM employees WHERE ssn= $ssn "; $result = mysqli->query($query); We must get the form information and find out whether the employee is already in the database. Robb T. Koether (Hampden-Sydney College) PHP Queries and HTML FormsLecture 23 Wed, Mar 14, 2018 9 / 15

Inserting into the Database Insert into the Database if ($result->num_rows > 0) echo "$fname $lname is already in the database"; else { $query = "INSERT INTO employees". " VALUES ( $fname, $lname, $ssn, $sex,". " $bdate, $salary, $dept)"; $mysqli->query($query); echo "$fname $lname has been added to the database"; } If he is not already in the database, then add him and then report success. Robb T. Koether (Hampden-Sydney College) PHP Queries and HTML FormsLecture 23 Wed, Mar 14, 2018 10 / 15

Creating a Select List of Departments Create a Select List of Departments <select name="dept"> <?php $query = "SELECT dname, dept FROM departments"; $result = $mysqli->query($query); for ($i = 0; $i < $result->num_rows; $i++) { $row = $result->fetch_array() $dname = $row[ dname ]; $dept = $row[ dept ]; echo "<option value= $dept >$dname</option>"; }?> </select><br/> Robb T. Koether (Hampden-Sydney College) PHP Queries and HTML FormsLecture 23 Wed, Mar 14, 2018 11 / 15

Outline 1 Retrieving Data from the Database 2 Inserting into a Database 3 Updating the Database Robb T. Koether (Hampden-Sydney College) PHP Queries and HTML FormsLecture 23 Wed, Mar 14, 2018 12 / 15

Updating the Database We want to update the data for an employee. We must Request the SSN from the user (using the form). Select the employee s data from the employees table. Display the current employee s information in (editable) text boxes, preselected radio button, preselected checkboxes, and preselected select elements. After clicking Submit, delete the old record and insert the new record. Report success or failure. Robb T. Koether (Hampden-Sydney College) PHP Queries and HTML FormsLecture 23 Wed, Mar 14, 2018 13 / 15

Updating the Database When updating the database, it is unnecessarily complicated to try to update only the fields that were changed. Instead, we should update all fields with whatever values are in the edited form by deleting the old record and inserting the new record. Robb T. Koether (Hampden-Sydney College) PHP Queries and HTML FormsLecture 23 Wed, Mar 14, 2018 14 / 15

Updating the Database In this example, we choose to use two PHP files. The first file will present the employee s data and allow the user to edit it. The second file will update the database. Robb T. Koether (Hampden-Sydney College) PHP Queries and HTML FormsLecture 23 Wed, Mar 14, 2018 15 / 15