Presentation is loading. Please wait.

Presentation is loading. Please wait.

Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.

Similar presentations


Presentation on theme: "Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy."— Presentation transcript:

1 Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy

2 Agenda Homework/Lab Review Homework/Lab Review Presentations Presentations SQL Update SQL Update Creating SQL Tables Creating SQL Tables Validation Validation Mid-term Project Mid-term Project

3 Database Infrastructure

4 IMPORTANT NOTE A Database Server can not have a DB with the same name. Since we use ONE DB Server, all our DBs have to have unique names.

5 Information needed for DB Need server name/IP (e.g. mysql..com or localhost) Need server name/IP (e.g. mysql..com or localhost) Need username Need username Need password Need password Need DB name Need DB name Need table names Need table names Need column names Need column names

6 IMPORTANT NOTE SQL Commands (SELECT, INSERT, UPDATE, etc) are almost always written in CAPS and other pieces of a SQL statement are written as normal case.

7 IMPORTANT NOTE Never do a DELETE statement, instead “archive” the date.

8 PHP / mySQL Syntax // Connect to server $con = mysql_connect(“SERVER",“USER",“PASSWORD"); // Connect to DB mysql_select_db(“DATABASE", $con); // Execute SQL Query $result = mysql_query("SELECT * FROM TABLE"); // The RECORDSET will be an object (e.g. $result) // Need to break the object into an array (e.g. $row) while($row = mysql_fetch_assoc($result)) { // The WHILE runs until END OF RECORD echo $row['FirstName']. " ". $row['LastName‘]." "; } // Close connection mysql_close($con); http://www.w3schools.com/PHP/php_mysql_connect.asp

9 Connecting to your DB Server: mysql..com Server: mysql..com Username: sccstudent_ Username: sccstudent_ Password: Maricopa_ Password: Maricopa_ DB name: sccstudent_ DB name: sccstudent_ --------------------------------------------- --------------------------------------------- Table: week4 Table: week4 Columns: id, username, password, firstname, lastname, phone, email, created Columns: id, username, password, firstname, lastname, phone, email, created

10 PHP / SQL Update Steps SELECT statement to pull ONE record SELECT * FROM persons WHERE id = 1; SELECT statement to pull ONE record SELECT * FROM persons WHERE id = 1; Use the result from step one to populate the HTML form fields ” /> Use the result from step one to populate the HTML form fields ” /> After user submits, take value and UPDATE the table UPDATE person SET fname=”$_POST[“first”]” WHERE id=1; After user submits, take value and UPDATE the table UPDATE person SET fname=”$_POST[“first”]” WHERE id=1;

11 Form Data Validation Client side: Javascript Client side: Javascript Make sure information is filled out BEFORE sending to the server. Make sure information is filled out BEFORE sending to the server. Saves unnecessary traffic. Saves unnecessary traffic. Maintains user entry seamlessly. Maintains user entry seamlessly. Sever side: PHP Sever side: PHP Users can disable Javascript. Users can disable Javascript. Content can be created maliciously access/update the DB. Content can be created maliciously access/update the DB. More control closer to the SQL statement. More control closer to the SQL statement.

12 Validation Test Is Null or Empty Is Null or Empty Type text vs. numeric Type text vs. numeric Is an email address, phone, social security, etc. Is an email address, phone, social security, etc. Limited number of characters Limited number of characters HTML code HTML code Special characters Special characters Malicious code (e.g. WHERE 1=1) Malicious code (e.g. WHERE 1=1)

13 JavaScript Validation function fValid(){ var x=document.forms[“form1"][“first"].value; if (x==null || x=="") { alert("First name must be filled out"); return false; } } function fValid(){ var x=document.forms[“form1"][“first"].value; if (x==null || x=="") { alert("First name must be filled out"); return false; } } First: First: http://www.w3schools.com/js/js_form_validation.asp

14 IMPORTANT NOTE The life cycle in PHP is the length of time it takes the server to find the file, process the information, and send the completed static HTML back to the client.

15 Sort DB records SELECT * FROM persons ORDER BY fname ASC; SELECT * FROM persons ORDER BY fname ASC; SELECT * FROM persons ORDER BY fname DESC; SELECT * FROM persons ORDER BY fname DESC; The ORDER BY keyword is used to sort the result-set by a specified column. The ORDER BY keyword is used to sort the result-set by a specified column. The ORDER BY keyword sort the records in ascending order by default. The ORDER BY keyword sort the records in ascending order by default. If you want to sort the records in a descending order, you can use the DESC keyword. If you want to sort the records in a descending order, you can use the DESC keyword. http://www.w3schools.com/sql/sql_orderby.asp

16 phpMyAdmin

17 mySQL Data Types Numeric Numeric Float Float Integer Integer String String Char Char Varchar Varchar Date Date http://dev.mysql.com/doc/refman/5.0/en/data-types.html

18 Create a new mySQL Table Log into the phpmyadmin site Log into the phpmyadmin site Select a DB from right column Select a DB from right column Create a table name and pick number of columns Create a table name and pick number of columns

19 Create mySQL Table Structure Name each field Name each field Select a Type Select a Type Assign a length for VARCHAR or CHAR types Assign a length for VARCHAR or CHAR types Design default values Design default values Determine if NULL values allowed by using NULL checkbox Determine if NULL values allowed by using NULL checkbox Pick one field as ID and select INT type and A_I checkbox Pick one field as ID and select INT type and A_I checkbox

20 phpmyadmin screen

21 In class Update SQL DB Update SQL DB Sort SQL Recordsets Sort SQL Recordsets Create Table Create Table

22 Questions?

23 Lab Display the “create” field from the table Display the “create” field from the table Update the form to allow the user to modify firstname, lastname, phone, and email address Update the form to allow the user to modify firstname, lastname, phone, and email address Add sort by lastname (sort for firstname from class should also work) Add sort by lastname (sort for firstname from class should also work) Send email to rob.loy@gmail.com with URL to input form file before 6pm on October 12. Send email to rob.loy@gmail.com with URL to input form file before 6pm on October 12.rob.loy@gmail.com

24 Mid-term Project Create mySQL table that has at least 5 fields Create mySQL table that has at least 5 fields One field should be ID with Auto Increment One field should be ID with Auto Increment Display all records from the table Display all records from the table Create a web form that has INSERT and UPDATE functionality for the table Create a web form that has INSERT and UPDATE functionality for the table Add a sort functionality Add a sort functionality At least 2 have to be required and display error messages where appropriate At least 2 have to be required and display error messages where appropriate Send email to rob.loy@gmail.com with URL to input form file before 6pm on October 19. Send email to rob.loy@gmail.com with URL to input form file before 6pm on October 19.rob.loy@gmail.com


Download ppt "Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy."

Similar presentations


Ads by Google