Link shortening script
The are a number of problems here.


You do a poor job of validating user input before working with it.  I would strongly suggest you look at using PHP's filter_input() or filter_input_array() to provide basic data validation and error handling. You can use these to easily validate URL formats for example, or provide you own custom validation rule. You probably need to further sanitize some of your input against cross-site scripting vectors depending on how you are using the data added to your database in your application.
You should be leveraging prepared statements for inserting your data to help mitigate against sql injection.
You have a lot of conditional nesting complexity.  You should consider adopting a coding style where you look for early exits and design away nested conditionals to whatever extent is possible.


For example:

if (empty($link1) || empty($link2) || empty($flink)) {
    // error and exit
}

// continue with rest of code, now without nesting.


This minimizes your number of code paths making your application less complex, easier to debug, and easier to maintain. Actively try to design away else conditions, as they are usually avoidable.


Note also, in my example above that I reduced conditionals to only use emtpy() instead of isset() and empty().  empty() would fail for same conditions but is also more specific than isset(), so it is redundant to have both.  Just use the more specific criteria.
You repeat yourself unnecessarily.  Look to factor things like you link re-writing logic into a function to minimize repeated code.  By the way, I am not sure exactly why you use preg_match() and concatenation here.  Won't that break your links?  Perhaps preg_replace() instead?
It appears to me that you a falling into classic programming anti-pattern by naming variables like $somename1, $somename2, etc.  Naming like this is a possible code smell that you should be thinking about using arrays or other data structures to describe these logically-grouped values.
Your lines of code are too long.  It makes it hard to read your code.
Variable names seem to be somewhat poorly named (not very meaningful in some cases).
Why are you closing $db, when you are working with $conn?  There is obviously context missing from example, but this seem like an error.
It is unclear from this code example how you are validating that user making the request should be allowed to modify your database in this manner. Perhaps that is in check_user()?  But typically, at a minimum any form-posting like this would have cross-site request forgery (CSRF) mitigation (i.e. session token) if not some form of user authorization.