NMD202 Web Scripting Week3. What we will cover today Assignment 1 Email Database Concepts MySQL...

31
NMD202 Web Scripting Week3

Transcript of NMD202 Web Scripting Week3. What we will cover today Assignment 1 Email Database Concepts MySQL...

Page 1: NMD202 Web Scripting Week3. What we will cover today Assignment 1 Email Database Concepts MySQL Exercises Server side validation Exercises.

NMD202 Web Scripting

Week3

Page 2: NMD202 Web Scripting Week3. What we will cover today Assignment 1 Email Database Concepts MySQL Exercises Server side validation Exercises.

What we will cover today

Assignment 1 Email Database Concepts MySQL Exercises Server side validation Exercises

Page 3: NMD202 Web Scripting Week3. What we will cover today Assignment 1 Email 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

Page 4: NMD202 Web Scripting Week3. What we will cover today Assignment 1 Email Database Concepts MySQL Exercises Server side validation Exercises.

Assignment 1

Examples of what you can do:•Budget tracker•Mini-Blog•Address Book•Event Planner

3 tables at least

Page 5: NMD202 Web Scripting Week3. What we will cover today Assignment 1 Email Database Concepts MySQL Exercises Server side validation Exercises.

Email

PHP Can connect to a mail transfer agent to send emails.

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)

Page 6: NMD202 Web Scripting Week3. What we will cover today Assignment 1 Email Database Concepts MySQL Exercises Server side validation Exercises.

Email

Send mail:

mail function:

mail ($to,$subject,$message);

This is the basic usage of mail, often you need to set headers as well.

Page 7: NMD202 Web Scripting Week3. What we will cover today Assignment 1 Email Database Concepts MySQL Exercises Server side validation Exercises.

Email

Setting Headers:

<?php

$to = '[email protected]';

$subject = 'the subject';

$message = 'hello';

$headers = 'From: [email protected]' . "\r\n" .

'Reply-To: [email protected]' . "\r\n" .

'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);

?>

Page 8: NMD202 Web Scripting Week3. What we will cover today Assignment 1 Email Database Concepts MySQL Exercises Server side validation Exercises.

Email

Multiple lines in the message:

<?php

$message = “

Hello \n\r

This is another line \n\r

This is the third line \n\r

”;

?>

Page 9: NMD202 Web Scripting Week3. What we will cover today Assignment 1 Email Database Concepts MySQL Exercises Server side validation Exercises.

Email

It is also possible to send html email:

<?php

$message = “<h1>Hello</h1>”;

$headers = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

?>

Check http://au2.php.net/manual/en/function.mail.php

Page 10: NMD202 Web Scripting Week3. What we will cover today Assignment 1 Email Database Concepts MySQL Exercises Server side validation Exercises.

Email

PHP mail function is cumbersome, the php community has developed better solutions:

•LibMail •HTML Mime Mail

Page 11: NMD202 Web Scripting Week3. What we will cover today Assignment 1 Email Database Concepts MySQL Exercises Server side validation Exercises.

Email

PHP mail function is cumbersome, the php community has developed better solutions:•LibMail •HTML Mime mail

Allow: Send multiple attachments, receipts, etc...

Page 12: NMD202 Web Scripting Week3. What we will cover today Assignment 1 Email Database Concepts MySQL Exercises Server side validation Exercises.

Email

Security Considerations:

Sanitize user inputs before sending mail:•Header injection•Multiple recipient injection

Avoid using mail function unless you know what you are doing

Page 13: NMD202 Web Scripting Week3. What we will cover today Assignment 1 Email Database Concepts MySQL Exercises Server side validation Exercises.

Email

Some email applications tag emails sent from php as Spam if headers are not properly setup, ie:Yahoo, Google.

If you targeting clients that use these emails you need to tweak the headers.

Page 14: NMD202 Web Scripting Week3. What we will cover today Assignment 1 Email Database Concepts MySQL Exercises Server side validation Exercises.

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.

Page 15: NMD202 Web Scripting Week3. What we will cover today Assignment 1 Email Database Concepts MySQL Exercises Server side validation Exercises.

Database Concepts

Page 16: NMD202 Web Scripting Week3. What we will cover today Assignment 1 Email Database Concepts MySQL Exercises Server side validation Exercises.

Database Concepts

Anomalies: logical or structural problems in the data model

Database normalization: Fixing anomalies, Normal forms (1nf, 2nf, 3nf)

Page 17: NMD202 Web Scripting Week3. What we will cover today Assignment 1 Email Database Concepts MySQL Exercises Server side validation Exercises.

Database Concepts

Page 18: NMD202 Web Scripting Week3. What we will cover today Assignment 1 Email Database Concepts MySQL Exercises Server side validation Exercises.

Database Concepts

http://www.geekgirls.com/databases_from_scratch_3.htm

Page 19: NMD202 Web Scripting Week3. What we will cover today Assignment 1 Email Database Concepts MySQL Exercises Server side validation Exercises.

Exercise:

Normalize the following Table:

Student Email Subject Name

Subject Description

Ass 1 Grade

Ass 2Grade

Exam Grade

Luis [email protected] Maths1 Initial Maths 12

Luis [email protected] Maths1 Initial Maths 14

Peter [email protected] Web

ScriptPHP, javascript

15

Peter [email protected] Maths 11 12 15

Page 20: NMD202 Web Scripting Week3. What we will cover today Assignment 1 Email Database Concepts MySQL Exercises Server side validation Exercises.

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+

Page 21: NMD202 Web Scripting Week3. What we will cover today Assignment 1 Email Database Concepts MySQL Exercises Server side validation Exercises.

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

Page 22: NMD202 Web Scripting Week3. What we will cover today Assignment 1 Email Database Concepts MySQL Exercises Server side validation Exercises.

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

);

Page 23: NMD202 Web Scripting Week3. What we will cover today Assignment 1 Email Database Concepts MySQL Exercises Server side validation Exercises.

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

);

Page 24: NMD202 Web Scripting Week3. What we will cover today Assignment 1 Email Database Concepts MySQL Exercises Server side validation Exercises.

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);

Page 25: NMD202 Web Scripting Week3. What we will cover today Assignment 1 Email Database Concepts MySQL Exercises Server side validation Exercises.

MySQL

Query Database:

SELECT name,age FROM example;

Or

SELECT * FROM example WHERE name=‘Peter’;

Page 26: NMD202 Web Scripting Week3. What we will cover today Assignment 1 Email Database Concepts MySQL Exercises Server side validation Exercises.

MySQL

Update Database:

UPDATE

example

SET

name=‘Ryan’,

age = 35

WHERE

id=2;

Page 27: NMD202 Web Scripting Week3. What we will cover today Assignment 1 Email Database Concepts MySQL Exercises Server side validation Exercises.

MySQL

Using MySQL:

Start MySQL Server

From the command prompt go to:

C:\temp\xampp\mysql\bin

Start the client:

mysql –u root

Page 28: NMD202 Web Scripting Week3. What we will cover today Assignment 1 Email Database Concepts MySQL Exercises Server side validation Exercises.

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 ‘;’

Page 29: NMD202 Web Scripting Week3. What we will cover today Assignment 1 Email Database Concepts MySQL Exercises Server side validation Exercises.

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

Page 30: NMD202 Web Scripting Week3. What we will cover today Assignment 1 Email Database Concepts MySQL Exercises Server side validation Exercises.

MySQL

To Restore the database:

mysql -u root < [backupfile.sql]

In our example:

mysql -u root < back.sql

Page 31: NMD202 Web Scripting Week3. What we will cover today Assignment 1 Email Database Concepts MySQL Exercises Server side validation Exercises.

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 email