CSE 154 Extra Sessions

Lecture 2: Advanced MySQL, Validation, and URL Rewriring

Reading: (none)

Except where otherwise noted, the contents of this document are Copyright 2012–2013 Morgan Doocy. All rights reserved. Any redistribution, reproduction, transmission, or storage of part or all of the contents in any form is prohibited without the author's expressed written permission.

Valid HTML5 Valid CSS

1. More SQL: Updating Databases

Creating a Table: CREATE TABLE

CREATE TABLE blackbook (
	id int(11) unsigned NOT NULL AUTO_INCREMENT,
	first_name varchar(100) NOT NULL,
	last_name varchar(100) DEFAULT NULL,
	phone varchar(10) DEFAULT NULL,
	owes_money enum('yes','no') NOT NULL DEFAULT 'yes',
	PRIMARY KEY (id)
) ENGINE=MyISAM DEFAULT CHARSET=utf8

Creating New Records: INSERT

INSERT INTO `tablename` (col1name, col2name, ...) VALUES (
	value1,
	value2,
	...
);

Updating Existing Records: UPDATE

UPDATE tablename SET
field1name = field1value,
field2name = field2value,
...
WHERE condition;
UPDATE blackbook SET
owes_money = 'no'
WHERE first_name = 'Joey';

Deleting Records: DELETE

DELETE FROM tablename WHERE conditions...;
DELETE FROM blackbook
WHERE first_name = "Jack 'The Hat'"
AND last_name = "McVitie";
-- he's been "taken care of"

2. Advanced Regex & Form Validation

Using alternative delimiters

if (preg_match('/http:\/\//', $str)) { ... }
if (preg_match('|http://|', $str)) { ... }

Capturing text with ()

$str = '<title lang="en">Pride and Prejudice and Zombies</title>';
if (preg_match("!<([^\s>]+)[^>]+>([^<]+)</\\g{1}>!", $str, $captures)) {
	list($entire, $element, $content) = $captures;
	print "The $element of this book is: '$content'";
	// "The title of this book is: 'Pride and Prejudice and Zombies'"
}

Capturing multiple matches: preg_match_all

$str = "<10><20><30><40>";
if (preg_match_all("/<(\d\d)>/", $str, $occurrences)) {
	$n = 1;
	foreach ($occurrences as $captures) {
		list($entire, $number) = $captures;
		print "number $n is: $number\n";
		$i++;
	}
	// number 1 is: 10\n
	// number 2 is: 20\n
	// ...
}

Regular expressions in HTML forms html5

How old are you?
<input type="text" name="age" size="2" pattern="[0-9]+" title="an integer" />
<input type="submit" />

3. URL Rewriting with mod_rewrite

Recall: A Basic URL

http://www.example.com/foo/bar.php
~~~~   ~~~~~~~~~~~~~~~ ~~~~~~~~~~~
protocol    host          path

Limitations of “Real” URLs

Solution: “Virtual” URLs

URL rewriting with mod_rewrite

# Turn on URL rewriting
RewriteEngine on

# Change requests for 'foo.html' to 'bar.html'
RewriteRule foo\.html bar.html

The RewriteRule directive

Rewrites any URLs that match a regular expression pattern.

RewriteRule pattern replacement [flags]
RewriteRule fakename\.html realdir/realname.html
RewriteRule movedfile\.php http://www.anotherserver.com/movedfile.php [QSA,R=301]
RewriteRule stillworks\.html - [L] # don't show maintenance page
RewriteRule .+\.(html|php) maintenance.html # down for maintenance!

Capturing and anchoring in RewriteRules

.htaccess file precedence and URL matching

RewriteRule chaining and options

RewriteRule ^foo$ abc/foo [S=1]
RewriteRule ^abc/(\w+)$ def/$1 [L]
RewriteRule ^(\w+)/(foo|bar)$ script.php?$2=$1 [QSA]

Multiple rules may apply in sequence, with the output of one feeding the input of another. [S=n] can be used to skip n rules

The RewriteCond directive

Specifies the conditions under which subsequent RewriteRules will be evaluated.

RewriteCond condition [flags]
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]