schwim's suggestion is likely to work, but it leaves open the small chance that the user could somehow add the same link twice because of say, a repeated form submission, such that the checks for the existing linkurl happen just before the insertions.
To avoid this, one approach is to create a unique constraint on the username and linkurl. Then, instead of doing a select before the insertion, you can just insert, then handle the unique constraint violation, if any.