NMD202 Web Scripting Week3
What we will cover today Assignment 1 Database Concepts MySQL Exercises Server side validation Exercises
Assignment 1 Milestone 1 – due week 5 Project Brief – What do you pretend to do Data Model – ER Diagram must be submitted in a VISIO file
Assignment 1 Examples of what you can do: Budget tracker Mini-Blog Address Book Event Planner 3 tables at least
PHP Can connect to a mail transfer agent to send s. Mail settings are configured in the php.ini file. linux mail can be used (ie:postfix or sendmail) or you can use SMTP (platform independent)
Send mail: mail function: mail ($to,$subject,$message); This is the basic usage of mail, often you need to set headers as well.
Setting Headers: <?php $to = $subject = 'the subject'; $message = 'hello'; $headers = 'From: "\r\n". 'Reply-To: "\r\n". 'X-Mailer: PHP/'. phpversion(); mail($to, $subject, $message, $headers); ?>
Multiple lines in the message: <?php $message = “ Hello \n\r This is another line \n\r This is the third line \n\r ”; ?>
It is also possible to send html <?php $message = “ Hello ”; $headers = 'Content-type: text/html; charset=iso '. "\r\n"; ?> Check
PHP mail function is cumbersome, the php community has developed better solutions: LibMail HTML Mime Mail
PHP mail function is cumbersome, the php community has developed better solutions: LibMail HTML Mime mail Allow: Send multiple attachments, receipts, etc...
Security Considerations: Sanitize user inputs before sending mail: Header injection Multiple recipient injection Avoid using mail function unless you know what you are doing
Some applications tag s sent from php as Spam if headers are not properly setup, ie:Yahoo, Google. If you targeting clients that use these s you need to tweak the headers.
Database Concepts A Database is a structured collection of data Usually databases are relational: The basic data structure of the relational model is a table where information about a particular entity (say, an employee) is represented in columns and rows columns enumerate the various attributes of an entity They obey to strict rules such as: a table cannot contain 2 identical rows.
Database Concepts
Anomalies: logical or structural problems in the data model Database normalization: Fixing anomalies, Normal forms (1nf, 2nf, 3nf)
Database Concepts
Exercise: Normalize the following Table: Student Subject Name Subject Description Ass 1 Grade Ass 2 Grade Exam Grade Luis Maths1Initial Maths12 Luis Maths1Initial Maths14 Peter m Web Script PHP, javascript 15 Peter m Maths111215
MySQL Relational Database System – Query language ANSI SQL 99 Different type of tables: ISAM, InnoDB, etc. ISAM – fast, but does not enforce referential integrity. (default) InnoBD – enforces referential integrity, only available in MySQL v4+
MySQL Commands: show databases; - display all available databases use #database; - sets the current database show tables;-display all tables from current database describe #table;-display table information create... – create table drop...- delete table/database alter #table... – update a table structure Insert into... – inserts data into a table update #table set... – update data from a table Select... from #table where... – query database
MySQL Create Statement: CREATE DATABASE sampleDatabase; USE sampleDatabase; CREATE TABLE example( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), name VARCHAR(30), age INT );
MySQL Create Statement: CREATE DATABASE sampleDatabase; USE sampleDatabase; CREATE TABLE example( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), name VARCHAR(30), age INT );
MySQL Insert Statement: INSERT INTO example (name,age) VALUES (‘Luis’,12); INSERT INTO example (name,age) VALUES (‘Peter’,13); INSERT INTO example (name,age) VALUES (‘John’,26); INSERT INTO example (name,age) VALUES (‘Kim’,78); INSERT INTO example (name,age) VALUES (‘Peter’,45);
MySQL Query Database: SELECT name,age FROM example; Or SELECT * FROM example WHERE name=‘Peter’;
MySQL Update Database: UPDATE example SET name=‘Ryan’, age = 35 WHERE id=2;
MySQL Using MySQL: Start MySQL Server From the command prompt go to: C:\temp\xampp\mysql\bin Start the client: mysql –u root
MySQL Keep your commands in scite in case you need to redo them. (it is easy to drop a table and start again instead of using the ALTER statement). Make sure all commands are terminated with a semicolon ‘;’
MySQL After you finished either keep your scite file with all your commands or do an sql dump of your work, run from the dos prompt (inside mysql bin folder) mysqldump --databases [databasename] -u root >[backupfile.sql] In our example: mysqldump --databases sampleDatabase -u root > backup.sql
MySQL To Restore the database: mysql -u root < [backupfile.sql] In our example: mysql -u root < back.sql
Exercise: Create the data model that you normalized in your previous exercise Insert some more records into the tables (students, subjects and grades) Update all Luis grades to 100 Delete all peter records Backup your database, send me the dump by