Memberships Move to a specific group on expiry

Hi,
I have a free trial subscription group, and 5 paid subscription groups. I also have a group where I want people who expire from the free trial go, upon expiry. This group will only be populated by expired trial subscribers. I have tried to write script to fire by a cron looking for expired members, who are not yet in the group I want to deliver them to, but having trouble there.

Thanks for any ideas,
Mike

-- script --
<?php
$qry = mysql_query("SELECT user_id FROM wp_m_membership_relationships WHERE level_id = 8");
while($row = mysql_fetch_array($qry))
{
$ids .= $row['user_id'].",";
}

$iids = rtrim($ids,',');

$loop = mysql_query("SELECT rel_id, user_id, sub_id, level_id, startdate, updateddate, expirydate, order_instance, usinggateway FROM wp_m_membership_relationships WHERE DATE(expirydate) > DATE(now()) AND user_id not in (".$iids.") or die (mysql_error()");

$exp_date = DATE('Y-m-d', strtotime('+2years'));
while ($row = mysql_fetch_array($loop))

{

mysql_query("INSERT INTO wp_m_membership_relationships (user_id, sub_id, level_id, startdate, updateddate, expirydate, order_instance, usinggateway) VALUES (".$row('user_id').", 8, 6, DATE(NOW()), DATE(NOW()), $exp_date, ".$row('order_instance').", 'admin') or trigger_error(mysql_error()");

}

mysql_close($con);

?>