First of all, I'd post the code in [php] tags and indent it so that it's easier to read. (See, I told ya!)
Second, I'd quote the associative array keys.
Third, I'd use SELECT COUNT(*) and return only the information I want from the table (the number of rows it contains), instead of the entire table.
Fourth, personally, I'd use date() instead of getdate()
Fifth, I'd be more likely to use $_SERVER['HTTP_USER_AGENT'] etc., instead of $HTTP_USER_AGENT.
Sixth, wrapping a single string variable in double quotes (e.g., "$HTTP_USER_AGENT") just wastes time: it creates a new string, searches it for variables, interpolates those variables with their string values, and returns the resulting string .... which in this case happens to be exactly the same as what the variable's string value was in the first place.
Seventh, since $sign is never used, the loop around it is a do-nothing. Maybe it's used elsewhere, but it's going to be the last character in the $id anyway, so the loop could be replaced with $sign=substr($id,-1);.
Eighth, if the id is an incrementing counter for the purposes of uniquely identifying rows, then MySQL (and most other DBMSs) have mechanisms for specifying such fields automatically (MySQL has AUTOINCREMENT, PostgreSQL has SERIAL, MS SQL Server has IDENTITY...)
Ninth, if the fields are numeric, then they oughtn't be quoted; if they're all strings then that will cause problems when ordering (because '10'<'2').
Tenth; what's $afid? Where does that come from? Oh, the querystring. So therefore it ought to be $_GET['afid'].