Prev | Current Page 53 | Next

Larry Ullman

"Building a Web Site with Ajax: Visual QuickProject Guide"


validate the form data (cont.)
5 To make them safe to use in a query, numeric
values are type-cast as integers.
6 Any failed validation routine results in an element
being added to the $errors array.
if (isset($_POST[???department_id??™]) && is_numeric($_
POST[???department_id??™]) && ($_POST[???department_id??™] >
0)) {
$did = (int) $_POST[???department_id??™];
} else {
$errors[] = ???department??™;
}
52 adding records
update the database
Assuming that the form data passed all the validation routines, an INSERT
query should be run.
1 If there were no errors, this conditional will be true (because the
$errors variable will be empty).
2 The INSERT query adds the new employee to the database using the
purifi ed data from the validation routines. (See extra bits on page 58.)
3 The mysql_affected_
rows() function returns the
number of, well, a?¬? ected
rows. For this script??™s query,
the number of a?¬? ected rows
should be 1, as one new
record should be added.
4 The results are reported
to the user.
??¦
$errors[] = ???phone extension??™;
}
if (!$errors) {
$q = ???INSERT INTO employees VALUES (NULL, $did,
???$fn??™, ???$ln??™, ???$e??™, $ext)???;
$r = mysql_query($q, $dbc);
if (mysql_affected_rows($dbc) == 1) {
echo ???

The employee has been added.


Pages:
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65