I found a similar question, but it didn't really give me the answer I was looking for. This tutorial explains the most important sanitation functions WordPress uses.
I'm using PDO to store form data into custom tables. A lot of this data contains names like l'this
and Cotélac & Blancé
.
If I put aside e-mail and url fields, is it enough to use esc_attr
and esc_html
to avid SQL injection and cross site scripting?
Should I use this before data is saved to DB? Or just before I out put it to screen?
//From Wordpress
esc_html -> Escaping for HTML blocks
esc_attr -> Encodes the <, >, &, " and '
This is a typical query I use:
$sql = " SELECT name
FROM name
WHERE user_name = :user_name
AND fk_ID = :some_id ";
$stmt = $this->db->prepare($sql);
$stmt->bindParam(':user_name', $name, PDO::PARAM_STR);
$stmt->bindParam(':some_id', $some_id, PDO::PARAM_STR);
$stmt->execute();
$result = $stmt->fetchAll();
return $result;
// alternative (from another query)
$stmt = $this->db->prepare($sql);
$stmt->execute(array(':id'=> $id));
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $result;