the Powerpoint presentation

Download Report

Transcript the Powerpoint presentation

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
Assignment 1
Examples of what you can do:
•Budget tracker
•Mini-Blog
•Address Book
•Event Planner
3 tables at least
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)
Email
Send mail:
mail function:
mail ($to,$subject,$message);
This is the basic usage of mail, often you need
to set headers as well.
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);
?>
Email
Multiple lines in the message:
<?php
$message = “
Hello \n\r
This is another line \n\r
This is the third line \n\r
”;
?>
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
Email
PHP mail function is cumbersome, the php
community has developed better solutions:
•LibMail
•HTML Mime Mail
Email
PHP mail function is cumbersome, the php
community has developed better solutions:
•LibMail
•HTML Mime mail
Allow: Send multiple attachments, receipts,
etc...
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
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.
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
Database Concepts
Anomalies: logical or structural problems in the data
model
Database normalization: Fixing anomalies, Normal
forms (1nf, 2nf, 3nf)
Database Concepts
Database Concepts
http://www.geekgirls.com/databases_from_scratch_3.htm
Exercise:
Normalize the following Table:
Student Email
Subject
Name
Subject
Description
Ass 1
Grade
12
Luis
[email protected]
Maths1
Initial Maths
Luis
[email protected]
Maths1
Initial Maths
Peter
[email protected]
m
Web
Script
PHP,
javascript
Peter
[email protected]
m
Maths
Ass 2
Grade
Exam
Grade
14
15
11
12
15
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 email