Just a quick look... It appears your lower code should work, but the error you are getting means that the value cannot be entered into the datase because the column is supposed to be unique. If you drop your index on the "links" table, it should work.
Actually, what is the name of your table? What value is specified for $username_links. If your table were called links, then your SQL should like this:
$links="INSERT INTO links VALUES ('$pagetype[$a]')";
For clarity sakes, you should store your SQL code into $sql instead of $links. Technicaly speaking, the SQL is not actually links; the results of the query are, but not the SQL. But that's just me being nit-picky. I like my code to explain itself, rather than me explaining my code.
AaronZoller.com
If doesn't fly, let me know and I'll strap a whole case of bottle rockets to it's back. Really, I'll look into the code deeper if that doesn't work.