1.04k likes | 1.05k Views
HTML Forms & PHP & MySQL Database Database Systems CSCI-3343. Dr. Tom Hicks Computer Science Department. Import Database University1 with MySQL Workbench. It Should Have 3 Tables. Create Folders. Create Folders Faculty-Add & Faculty-Display.
E N D
HTML Forms & PHP & MySQL Database Database Systems CSCI-3343 Dr. Tom Hicks Computer Science Department
Create Folders Faculty-Add & Faculty-Display Download Faculty-Display.php &Put It In Folder Faculty-Display
Create User UniversityUser With These Privileges ALTER USER 'UniversityUser'@'%' IDENTIFIED WITH mysql_native_password BY 'trinity'
Test Faculty-Display Uses UniversityUser!
Add The Following Documentation BlockTo The Top your Name & Date <!------------------------------------------------------------------------------ -------------------------------------------------------------------------------- -- Faculty-Add.php -- -- -- -- Purpose: Prompt the user to fill all of the data fields necessary to -- -- add a new faculty member to the Faculty Table of the University1 -- -- MySQL database. -- -- -- -- When the submit button is pushed, control shall transfer to -- -- page Faculty-Add-Confirmation.php; it will be this page which -- -- physically adds the record to the database. -- -- -- -- This will be a first attempt at adding records; no error -- -- processing will be done. -- -- -- -- Written By: Dr. Tom Hicks Date: xx/xx/xx -- -------------------------------------------------------------------------------- ------------------------------------------------------------------------------->
Save A Copy Of Faculty-Add.php As Faculty-Add-Confirmation.php Alter The Banner
Add The Following Documentation BlockTo The Top your Name & Date <!------------------------------------------------------------------------------ -------------------------------------------------------------------------------- -- Faculty-Add-Confirmation.php -- -- -- -- Purpose: Create a query from the data transferred from page -- -- Faculty-Add-Confirmation.php; use the query to add a new record -- -- to table Faculty of the University1 database. -- -- -- -- This will be a first attempt at adding records; no error -- -- processing will be done. -- -- -- -- Written By: Dr. Tom Hicks Date: xx/xx/xx -- -------------------------------------------------------------------------------- ------------------------------------------------------------------------------->
FORM • HTML forms are used to pass data to a server. • An HTML form can contain input elements like text fields, checkboxes, radio-buttons, submit buttons and more. A form can also contain select lists, textarea, fieldset, legend, and label elements. • The form element begins with <FORM> and ends with </FORM> • Add the form to the body of the Faculty-Add.php <FORM> </FORM> .
FORM METHOD = GET • METHOD = GET • Cacheable Does not automatically re-request info • Should be used only if form is Indepotent • Browser will reprocess with no warning • Would Re-Bill a credit card with no browser warning • May require POST for Indepotent if URL is long • For getting/retrieving data only! <FORM METHOD = "GET" > </FORM> .
FORM METHOD = POST • METHOD = POST • Browser will generally reprocess with a warning • Don’t want credit card rebilled twice • I always use with database queries I always want the most recent data • Many folks simply use POST all of the time <FORM METHOD = "POST" > </FORM> .
FORM GET Illustration • METHOD = GET • Get can also expose more of the sensitive information because it is appended to the URL. (See Below!) • More Of A Security Risk Than Post ! <FORM METHOD = “GET" > </FORM> . I USE POST!
FORM ACTION = AddFaculty-Confirmation.php • ACTION = Form Process Page URL Relative or Absolute • Suppose the data collection page Faculty-Add.php • The contents of the form will often be sent to another page for processing; i.e. adding this faculty member record to the database. • The data collection page & the process page can be one and the same, but this is often a bit more complex and limiting. • I would call the page to add the confirmation page AddFaculty-Confirmation.php • It is not the purpose of this presentation to add the record to the database; we are only examining the HTML transfer of information. <FORM METHOD = "POST"ACTION = "Faculty-Add-Confirmation.php"> </FORM> .
FORM ID = form1 NAME = form1 • NAME = form1 ID = form1 • HTML offers the ability to logically navigate to named regions on the page. • You might choose to automatically move the cursor into one of the form textboxes when a form is loaded – you will need the NAME. <FORM METHOD = "POST"ACTION = "Faculty-Add-Confirmation.php"ID = "form1" NAME = "form1"> </FORM> .
INPUT TYPE = SUBMIT • NAME = form1 ID = form1 • HTML offers the ability to logically navigate to named regions on the page. • You might choose to automatically move the cursor into one of the form textboxes when a form is loaded – you will need the NAME. <FORM METHOD = "POST" ACTION = "Login-Confirmation.php " ID = "form1" NAME = "form1"> <center><INPUT TYPE ="submit" VALUE ="Add Faculty Member Now!"></center> </FORM> .
Form Submit Can Navigate Control To Other Web Pages • The confirmation page may be a relative address as seen above. • The confirmation page may be absolute as shown to google below. <FORM METHOD="POST" ACTION="http://google.com"> <INPUT TYPE ="submit" VALUE = "Go To Google Now"> </form>
INPUT TYPE = Text • The INPUT element is used for collecting data entered by keyboard. • The data may be alpha or numeric • The data must be inside <Form> and </Form> • Add the code below to your form on Faculty-Add.php First <INPUT NAME = "First" TYPE = "text" SIZE = "15"><P> Contents Of First Are SentTo Confirmation On Submit!
Recover Data Passed To Confirmation Page • Data processed in the confirmation can be recovered from &_POST • First < INPUT NAME = "First" TYPE = "text" SIZE = "15"><P> • Add the following block of code to the confirmation page. <HR COLOR=Navy SIZE=5 NOSHADE /> <?PHP $First = $_POST['First']; print "First = " . $First . "<BR>"; ?>
INPUT TYPE = Text • The INPUT element is used for collecting data entered by keyboard. • The data may be alpha or numeric • The data must be inside <Form> and </Form> • Add the code below to your form on Faculty-Add.php Last <INPUT NAME = "Last" TYPE = "text" SIZE = "20"><P> Contents Of First & Last Are SentTo Confirmation On Submit!
Recover Data Passed To Confirmation Page • Data processed in the confirmation can be recovered from &_POST • First < INPUT NAME = "First" TYPE = "text" SIZE = "15"><P> • Add the following block of code to the confirmation page. <HR COLOR=Navy SIZE=5 NOSHADE /> <?PHP $First = $_POST['First']; $Last = $_POST['Last']; print "First = " . $First . "<BR>"; print "Last = " . $Last . "<BR>"; ?>
Consider Building Your Insertion Query As You Develop The Page
Add Connection - Confirmation Page - 1 <?PHP $testing = true; /*============================================================= === Connect To MySQL Database === =============================================================*/ $server ="localhost"; $username="UniversityUser"; $password="trinity"; //use your password $database="university1"; $con=mysqli_connect($server, $username, $password, $database);
Add Connection - Confirmation Page - 2 /*============================================================= === Gather The Data Transferred With POST === =============================================================*/ $First = $_POST['First']; $Last = $_POST['Last']; /*============================================================= === Build The Query === =============================================================*/ $Query = "INSERT INTO Faculty " . "(First, Last) " . "VALUES " . "(" . $First . ", " . $Last . "); " ;
Add Connection - Confirmation Page - 3 /*============================================================= === Testing === =============================================================*/ if ($testing) { print "First = " . $First . "<BR>"; print "Last = " . $Last . "<P>"; print "Query = <P>" . $Query . "<P>"; } ?>
This Will Be A Complex Query • Build It In Parts Test Things As You Go! Copy Query To Clipboard
Test Query • You may notice errors simply by visual examination IF SO FIX If it appears good, test it. What Is The Error?
Correct The Query /*============================================================= === Build The Query === =============================================================*/ $Query = "INSERT INTO Faculty " . "(First, Last) " . "VALUES " . "('" . $First . "', '" . $Last . "'); " ;
The Web Page Will Provide No Meaningful Database Insertion Error MessagesPasting The Query Into The Command Line, MySQL Workbench, Navicat, etc. Will Often Provide Meaningful Error Messages!