Most people would just give their child a bit of cuddle and comfort mixed in with a lot of "there there" and "did you bump your head?" and "Daddy'll kiss it better" might be words like:
Now we know why we don't stand up underneath the table, don't we.
You won't do that again.
or (as in comments)
You'll remember not to do that next time!
An older child might be told to "learn from your mistakes", but more likely in the context of something more complex than banging one's head
But in the context you give, there is not much need to give instruction. If they are old enough to understand the instruction, they are old enough not to need it.
There is also saying
Once bitten, twice shy
But this is more adult, meaning that you will be particularly risk-averse if you have lost something the first time you tried it.