Never trust data sent by a user. A form field can contain malicious code. Three reflexes cover the essentials.
1. Protecting against SQL injection
Never insert a form value straight into a query. Use prepared statements (PDO): the value is treated as plain data, never as SQL code.
2. Protecting against XSS flaws
Before displaying data that was typed in, run it through htmlspecialchars(). This turns tags into harmless text and blocks script injection.
3. Validating and filtering
- Check the expected format (does an email actually look like an email?).
- Use filter_var() to validate or clean up.
- Check the length and the type of the data.
Key point: prepared statements against SQL injection, htmlspecialchars() against XSS. The golden rule: never trust user input.