$idname_wild = $idname . "%";
$query = "select id, flag from flags where flag like '$idname_wild'";
Alternatively,
$query = "select id, flag from flags where flag like '${idname}%'";
I think the problem is that your code is looking for a variable called $idname%.
A good tip:
If you are not getting the results you want from your SQL, echo the SQL statement and then run it against your DB with phpmyadmin or something.
If you echo out your, I guess it looks like this:
select id, flag from flags where flag like '';
assuming you don't have a variable called $idname%.
Does that make sense? Does my suggestion work?