Here are some answers.
1.
My syntax was "[0-9]+$" Any caret ^ that is NOT inside of brackets [] means the beginning. So, first I'm matching the beginning of the string. Then a number from 0-9, at least once (which is what the plus sign + means). And a dollar sign $ is the opposite of a caret , meaning the end. So: Beginning of string, a digit at least once, and then end of string. Any non-integer will return FALSE.
Their syntax was: "[0-9]" When inside of the brackets [], a caret ^ means "not". So literally, you're matching anything that is NOT 0-9. So, if you have a non-integer, this ereg will return TRUE.
It just depends on your preference. As far as I can tell, those two functions will do exactly the same thing (except, of course that one returns FALSE when something is wrong and the other returns TRUE).
2.
Usually, I don't limit the integer. The reason for this is that I like to let the database limit the integer. If I add a new row in the database (with a number of 13, for example), I don't want to have to change the code too. So what I do is after I'm sure I have an integer, I check and made sure it's a valid number in the database. Like this:
if(!ereg("^[0-9]+$",$id)) exit("Invalid ID!");
$result = mysql_query("select id from table_name where id='$id'");
if(!mysql_num_rows($result)) exit("That ID does not exist.");
That way, you're sure you have an integer, and a valid integer at that.
3.
One of the best places where I learned about SQL injection and stuff was hackthissite.org. Even though it doesn't really have plentiful reference material for how to do it, there are situations that they give you, and every one is breakable. So you really learn how to think like a hacker, and therefore how to protect yourself.
But what's the basic idea? Let me explain.
Let's say... you have a page that allows you to view/edit your contact data. This is common on lots of websites where you can have an account. So, that page would be something like editprofile.php?accountid=8, where 8 is the ID number for that account. And here's some sample code that's used to retrieve the contact data from the database.
if(empty($_GET['accountid'])) exit("You must have an account ID!");
$accountid = $_GET['accountid'];
$result = mysql_query("select firstname,lastname,address,phonenumber,email,etc1,etc2 from contact_info where accountid='$accountid'");
// retrieve and echo out account information, etc.
Seems nice and safe, right? The $accountid is in quotes, so nothing can happen to it... right?
What if some evil hacker goes to this address: editprofile.php?accountid=8' or accountid='6
Now, put it in context of the query:
select firstname,lastname,address,phonenumber,email,etc1,etc2 from contact_info where accountid='8' or accountid='6'
The owner of that website has a big problem. The evil hacker can now view and probably edit the private information for the person whose account number is 6. That's why it's critical to use mysql_real_escape_string, which would have escaped the single quotes that the evil hacker put into the address bar, and there wouldn't have been a problem.
Obviously, this is just a simple example. There are many other things that can damage your site with SQL injection, and a lot of people don't realize it.
You've got a big problem.