0
I have studied and seen that there are different ways of treating data before inserting them into a query which is executed in the database.
I have some questions about security and the issue of preventing some attacks like SQL Injection, etc...
I use some forms, much for recommendation, but without understanding why.
I would like to know the risks and the most recommended form among the 4 examples below.
OBS: I used SELECT in the examples, but could be a INSERT also.
1st form:
<?php
  $nome = $_POST['nome'];
  $senha = $POST['senha'];  
  $query = "SELECT * FROM tb_usuarios WHERE nome = '$nome' AND senha = '$senha'";
?>
second form:
<?php
  $nome = filter_input(INPUT_POST, 'nome', FILTER_SANITIZE_STRING);
  $senha = filter_input(INPUT_POST, 'senha', FILTER_SANITIZE_STRING);  
  $query = "SELECT * FROM tb_usuarios WHERE nome = '$nome' AND senha = '$senha'";
?>
3rd Form:
Suppose this code is within a class method and the name and password attributes are privates
<?php
  $query = "SELECT * FROM tb_usuarios WHERE nome = :nome and senha = :senha";
 $stmt = $this->conexao->prepare($query);
 $stmt->bindValue(':nome', $this->__get('nome'));
 $stmt->bindValue(':senha', $this->__get('senha'));
 $stmt->execute();
?>
4th Form:
Suppose this code is within a class method and the name and password attributes are privates
<?php
  $nome = $this->__get('nome');
  $senha = $this->__get('senha');
  $query = "SELECT * FROM tb_usuarios WHERE nome = :nome and senha = :senha";
  $stmt = $this->conexao->prepare($query);
  $stmt->bindParam(':nome', $nome);
  $stmt->bindParam(':senha', $senha);
  $stmt->execute();
?>
						
Thiaguinho, this addition that asked the question has no relation to all the rest of it and given that the original question has already been answered, I suggest you open a new question treating this other question.
– Woss