1. did not quite understand what exactly you want your usernames look like. You say:
"however i would like the username to be Letters First(upper or lowercase), followed by numbers and underscore and spaces in the username. "
And right after that you are saying:
123abc = this IS CORRECT
Making the string have only one space would look like this:
$username = eregi("^[a-zA-Z0-9_]+[ ]{0,1}$", $username)
2. Again confused on what you want:
$insertquery = sprintf("INSERT INTO tablename (`username`, `password`, `firstname`) VALUES ('%s', '%s', '%s')",
what is this? If you are asking whether you should apply the mysql_real_escape_string() function to the username - definitely. You should apply it to all items you insert. If you want to remove the ` or any other character, than you might use eregi_replace() or ereg_replace() to remove the symbols.
3. Yes, mysql-real-escape-string() is the best. Though you could definitely use addslashes() function.
4. You should avoid pickups through usage of mysql_real_escape_string() while selection from the database. Also you should use system like: if you enter password more than 5 times, your password will be reset and new password would be emailed to you.
About storing the passwords - I would suggest you to encrypt your passwords with md5() or/and sha1() functions. Then when getting a username in order to authorize him/her, you just execute your select query using the username only, and then you compare your passwords from database and the one provided by the user.
Tell me if you need any further help,
Pratamishus
_________________
www.eb-downloads.com
FREE E-BOOKS DOWNLOADS.