Custom code request

Hi I want to create for me the following code :
In invoice view I have two custom fields
MyidA ,MyidDetails

I want to put a value to MyidA when I press save button ( Right top button which is general button save for invoice ) then
to executed the folowing sql code
sql code correct Select MyidDetails from Mytable where MyidA=MyidA ( custom field value )
store the data and display result in MyidDetails custom field
Each time perform the save via button to executed above code
If MyidA is empty then to place null to MyidDetails
Payment via paypal