How can I sanitise a form entry before inserting the data into a database table?

I have a form which adds records to a table.
It has some text fields.
How do I make the form trim any leading and trailing whitespace from these text fields?