如何自动将 'strip_tags' 附加到所有 db select 查询?

How to append 'strip_tags' to all db select queries automatically?

我的 php db.php

里有这个
$mysqli = new mysqli($hostname, $user, $pass, $bd);

foreach($_POST as $key => $value){ 
  if (!is_array($value)){
   $_POST[$key] = strip_tags($value);
  }
}

首先是我的数据库连接,然后是一个简单的 foreach 从所有 POST 中删除 html 标签。此页面包含在所有其他需要数据库连接的页面中。

我可以更改此功能以将 'strip_tags' 应用于所有 mysqli 选择(而不是帖子)并删除那里的所有标签吗?

例如

1. db connection,
2. foreach/function to remove all tags from any mysqli select,
3. mysqli select query (with no html tags as result).

我认为没有办法自动执行此操作。你可以这样做:

  1. 数据库连接,
  2. 准备SQL查询
  3. Foreach 所有参数,干净的标签,
  4. 绑定参数执行查询

其他选择是使用一些 ORM。例如 Doctrine 允许指定在保存实体之前调用的函数。您可以在那里去除标签。

您可以将其包装在一个函数中:

function wrapp($sql){
  // You could add more stuff to change like htmlspeciclchars
  return strip_tags($sql);
}

每次查询都可以调用这个函数。

$mysqli->query(wrapp($sql));

如果您想进一步解决您的连接问题,为它构建一个 class 并在其中实现类似上面的功能可能是明智的。据我所知,如果不调用函数或类似的东西,就无法自动执行此操作。