well first thing to do is you need to have a backup plan for your site, daily basis backup can save a lot of time when disaster happen. "prevention is better than cure".
second thing to do create a script than would rebuild the PrimPhoto
<?
set_time_limit(0);
if ( !$dblnk )
mysql_connect("localhost","<username>","<password>");
$DB = "<database>";
$res = mysql_query("select ID,PrimPhoto, NickName from ".$DB.".Profiles $limit");
while ( $arr = mysql_fetch_array($res) )
{
echo "\n $arr[ID] $arr[NickName]";
$res_photo = mysql_query("select * from ".$DB.".media where med_prof_id='$arr[ID]' and med_id='$arr[PrimPhoto]'");
$arr_photo = mysql_fetch_array($res_photo);
if ( (int)$arr_photo[med_id] == 0)
{
$res_photo = mysql_query("select * from ".$DB.".media where med_prof_id='$arr[ID]' LIMIT 1");
$arr_photo = mysql_fetch_array($res_photo);
if ( (int)$arr_photo[med_id] == 0)
{
$sQuery = " UPDATE ".$DB.".Profiles SET `Picture` = '0' WHERE `ID` = '$arr[ID]' LIMIT 1";
}
else
{
$sQuery = " UPDATE ".$DB.".Profiles SET `PrimPhoto` = '$arr_photo[med_id]', `Picture` = '1' WHERE `ID` = '$arr[ID]' LIMIT 1";
}
if (!$restmp=mysql_query( $sQuery ))
{
echo "\n".mysql_error();
exit;
}
}
}
?>