At the thank you page you could extract the search engine name with code like this (not checked):
// do not remove intval() - here it is against SQL injections!!!
$referred_by_id = intval($_POST["referred_by_id"]);
$query = "SELECT referred_by_name FROM referredby WHERE referred_by_id=$referred_by_id";
$res = mysql_query($query);
$row = mysql_fetch_assoc($res);
$referred_by_name = (false!==$row)?$row['referred_by_name']:'';
I supposed here that your DB is MySQL and that you are using mysql_* functions (not e.g. mysqli_* functions or PDO) to work with it. Otherwise the code would be different of course but I think it could give you an idea anyway.
I mean you could extract the search engine name by number from the DB (please be careful not to use the posted data directly in the query to avoid SQL injection attacks) and then use it in the code as you see fit.