在db中验证和插入详细信息

I need to insert the details (name, id , number )of many documents into database if they are not already existing and if they exist I just need to do a update for any changed information. I have arrived at the following code but it doesn't work. I am new to this and need help on this.

// setting up the connection to db

    foreach($A->Documents -> Document as $Document)
{


   $query = "SELECT * from table where id = '".$Document->id."'";

   $outcome = mysql_query($query) or die(mysql_error());
      if(($outcome)&&(mysql_num_rows($result)>0)){
       echo "Document already available" ;
         while($row = mysql_fetch_object($outcome)){
           if(!($outcome->name == $document->name)){
                  $update= "UPDATE table SET name= '.$Document->Name.'";
                  mysql_query($update) or die(mysql_error());
                  }
           else 
                  {
                  $insert= "INSERT table SET name= '.$Document->Name.'";
                  mysql_query($update) or die(mysql_error());
                  }

          }
    }
}