I am not a native speaker of English. Could anyone please tell me which sentence is correct?
If I had money, I would fly to Spain.
If I had money, I would have flown to Spain.
Well, both sentences are grammatical, but they mean different things. It depends on what you're trying to say.
The first means, that if you had money, you'd fly there now or some point in the future.
The second can mean one of two things, depending on whether "had" is intended to be 'hypothetical' or a genuine 'past reference': so it could mean either (a) if you had money (now), you would at some point in the past have flown there; (b) if, at some point in the past being referred to, you had had money, then at that moment or some moment thereafter, you would have flown to Spain.
As I say, it's not really a question of one sentence being grammatical and the other not, rather that they have different interpretations.
The following are correct (the second one is a correction of your second possibility)
This means that I probably don't have money now, but in the alternate universe where I do have money, I think I want to fly there.
This means that, in the past, I probably didn't have any money, went to Spain by some other means, but in the alternate universe, the presence of extra money might have allowed me to indulge in flying.