year<$config['FAIRYEAR']) { $complete="yes"; $active="yes"; } else { $complete="no"; $active="yes"; } //see if a user exists with this email $uq=mysql_query("SELECT * FROM users WHERE username='".mysql_real_escape_string($r->email)."' AND year='$r->year'"); if($r->email && $ur=mysql_fetch_object($uq)) { $user_id=$ur->id; echo "Using existing users.id=$user_id for award_contacts.id=$r->id because email address/year ($r->email/$r->year) matches\n"; //update any info we have thats missing $sqlset=""; foreach($userfields AS $f) { //if its NOT in their USER record, but it IS in their AWARD_CONTACTS record, then bring it over, else, assume the users record has priority if(!$ur->$f && $r->$f) { $sqlset.="`$f`='".mysql_real_escape_string($r->$f)."', "; } } $sql="UPDATE users SET $sqlset `types`='{$ur->types},sponsor' WHERE id='$user_id'"; mysql_query($sql); echo mysql_error(); echo " Updated user record\n"; } else { $sql="INSERT INTO users (`types`,`username`,`".implode("`,`",$userfields)."`,`year`) VALUES ("; $sql.="'sponsor','".mysql_real_escape_string($r->email)."'"; foreach($userfields AS $f) { $sql.=",'".mysql_real_escape_string($r->$f)."'"; } $sql.=",'".mysql_real_escape_string($r->year)."')"; mysql_query($sql); echo mysql_error(); $user_id=mysql_insert_id(); //and link it to themselves as a starting record mysql_query("UPDATE users SET uid='$user_id' WHERE id='$user_id'"); echo "Creating new users.id=$user_id for award_contacts.id=$r->id\n"; } echo " Linking $user_id to users_sponsor record\n"; mysql_query("INSERT INTO users_sponsor (`users_id`,`sponsors_id`,`sponsor_complete`,`sponsor_active`,`primary`,`position`,`notes`) VALUES ( '".$user_id."', '".$r->award_sponsors_id."', '$complete', '$active', '".mysql_real_escape_string($r->primary)."', '".mysql_real_escape_string($r->position)."', '".mysql_real_escape_string($r->notes)."')"); echo mysql_error(); } } ?>