One handy method to do it is called typecasting (force a variable to have a specific variable type, not a varient/anything).
For example, instead of checking for an integer or using intval, it would be fine to use:
$id = (int) $_GET['id'];
This has a few benefits.
It always ensures $id is an integer, it cannot possibly be anything else.
If $_GET['id'] is undefined/blank, $id will == 0 (since converting '' to an integer is zero). Pretty much the same with any string/non-numeric value provided.
Finally, if its a string value, you need to escape it if its for use in a database.
If magic_quotes_gcp is OFF (check php manual to determine if its on or off), you need to escape stuff manually:
$string = addslashes($string);
Otherwise, you get the variables in escaped form already and you dont need to do anything (unless your wanting to remove the escaping, for example echoing back to the user).
A simple test is a page with:
<?php
$string = $_GET['string'];
echo "Got: $string";
?>
If ...?string=a'b
Comes back as:
Got: a'b
Then magic quotes is OFF (the ' is not escaped).
If it comes back as:
Got: a\'b
Then its escaped (and can just insert into the database as is).
As for query inserting, always quote field values (eg:
$query = "INSERT INTO table (field) VALUES ('$data')";
So $data cannot break out (when escaped) of the field.
Thats the jist of it!