Escape at the sink
A value is safe for one sink and unsafe for another. HTML escaping does not protect SQL, and neither protects a shell. Guard each place the value is consumed, at that place, with that sink's tool (bind, arg list, escape).
Prefer APIs over strings
Where a sink has an API that takes data (bind parameters, an argv list, a DOM setter), use it instead of building a string. String building is where injection hides; the API removes the door entirely.