In each foreach
loop, you’re setting the first item’s ID as the value for $selected
which will obviously match the ID of the first item.
Before running any of the foreach
loops fetch the stored value from the DB then use that for the comparison.
global $wpdb;
$selected = $wpdb->query( "SELECT something FROM $wpdb->table_name" );
// ... foreach loops