Web Systems Development (CSC-215) Lecture 7: Working with Databases
localhost/phpMyAdmin
phpMyAdmin on Remote Host
Set up sample database
Set up sample database
Add sample entry
Confirmation of entry
Confirmation of entry
Process Connect to MySQL Select the database to use Build a query string Perform the query Retrieve results and output to a web page Repeat 3-5 until all desired data has been retrieved Disconnect from MySQL
Standard (Deprecated) vs. MySQLi If you’re looking up tutorials, make sure to use MySQLi as covered in the lecture The textbook includes deprecated version of the same code as well for reference
Creating a Login File
Connecting to MySQL Server
Building and executing a query
Fetch Result
fetch_array() Can return 3 types of arrays MYSQLI_NUM MYSQLI_ASSOC Numeric, columns appear in the array in the order defined in the table MYSQLI_ASSOC Associative, each key refers to a column (by name) MYSQLI_BOTH Associative and numeric array
Example
Make connection
Deleting a record
Inserting a record
Create input form
Get all entries
Display all entries
Close
Sanitize
End of example
SQL Injection User verification code
Preventing SQL Injection Normal usage
Preventing SQL Injection Entry for user field
Deletion Example
PHP’s Magic Quotes Feature Automatically adds back slash to characters like single and double quotes Bypass and use mysql_real_escape_string()
Solution Disable special characters by prepending slashes using real_escape_string()
Preventing HTML Injection Example of maliciously inserted code, via, e.g., a form that displays its input XSS: Cross-Site Scripting
Use htmlentities() to convert
Embedding PHP in HTML
Document head
Setting up table
Set up calculation
Class Activity Set up a script that is similar to the Fibonacci one but instead of Fibonacci numbers, calculate the sum to n for each number S0 = 0 S1 = 0 + 1 = 1 S2 = 0 + 1 + 2 = 3 S4 = 0 + 1 + 2 + 3 = 6 S5 = 0 + 1 + 2 + 3 + 4 = 10 Print only two columns, Sn and the sum Set different colors for alternating rows
Lecture content adapted from chapter 10 of Learning PHP, MySQL, JavaScript, CSS & HTML5.