I want to make a kind of Generic Crud and I implemented the insert function, I know that the way I did it is not the best way to do it and I do not want to use any kind of frameworks and so I would like help, tips. >
What else is causing me confusion as I will do a correct and efficient treatment of the data before being inserted / edited, I did a cleaning function but I know it is more for a "gambiarra", because I like to use the php filters sanitize and I'm not finding a way to use them very well with object orientation and generic.
I've seen some topics similar to mine, but I have not found what I'm looking for.
Class students:
<?php
require_once 'crud.php';
require_once 'usuarios.php';
require_once 'conexao.php';
class Alunos extends Usuarios {
private $matricula;
private $curso;
private $turma;
private $dados_cadastrais;
private $con;
//Métodos
public function cadastrarAluno($dados) {
$verifica = $this->con->conectar()->prepare("SELECT * FROM usuarios WHERE email = :email");
$verifica->bindValue(":email", $dados['email']);
$verifica->execute();
if ($verifica->rowCount() > 0) {
return false;
} else {
$this->dados_cadastrais = $this->limpeza($dados);
$sql = "INSERT INTO usuarios (nome, nomepai, nomemae, datanasc, naturalidade, nacionalidade, estadocivil, sexo, endereco, complemento, bairro, cidade, estado, cep, telefone, celular, celular2, email, cpf, rg, orgaoe, estadoe, datae, formacao, cursof, senha, nivel) values (:nome, :nomepai, :nomemae, :datanasc, :naturalidade, :nacionalidade, :estadocivil, :sexo, :endereco, :complemento, :bairro, :cidade, :estado, :cep, :telefone, :celular, :celular2, :email, :cpf, :rg, :orgaoe, :estadoe, :datae, :formacao, :cursof, :senha, :nivel)";
$cadastro = new Crud();
if($cadastro->cadastrar($sql, $this->dados_cadastrais)){
return true;
}else {
return false;
}
}
}
function limpeza($dados){
foreach ($dados as $key => $valor) {
if(($key == "cpf") || ($key == "rg")){
$_POST[$key] = filter_var($this->limpaDoc($valor), FILTER_SANITIZE_STRING);
}else if(($key == "data") || ($key == "datae") || ($key == "datanasc")){
$_POST[$key] = filter_var($this->formataDataEua($valor), FILTER_SANITIZE_STRING);
}else if(($key == "senha")){
$_POST[$key] = filter_var(md5($valor), FILTER_SANITIZE_STRING);
}else{
$_POST[$key] = filter_var($valor, FILTER_SANITIZE_STRING);
}
}
return $dados;
}
function formataDataEua($data){
$data = date("Y-m-d", strtotime(str_replace('/', '-', $data)));
return $data;
}
function limpaDoc($valor) {
$valor = preg_replace('#[^0-9]#', '', $valor);
return $valor;
}
//Métodos especiais
function __construct() {
$this->nivel = 0;
$this->con = new Conexao();
}
Crud Class
<?php
require_once 'conexao.php';
class Crud {
private $con;
private $insert;
private $update;
private $delete;
//Métodos especiais
function __construct() {
$this->con = new Conexao();
}
function insert($sql, $dados) {
$this->insert = $this->con->conectar()->prepare($sql);
foreach ($dados as $key => $valor) {
if(is_numeric($valor)){
$this->insert->bindValue(":$key", $valor, PDO::PARAM_INT);
}else{
$this->insert->bindValue(":$key", $valor, PDO::PARAM_STR);
}
}
if ($this->insert->execute()) {
return true;
} else {
return false;
}
}
}