I'm studying object-oriented PHP and, from some tutorials taken from the net (pdo, pojo, Dao ...), I made a very simple system in which I get the value entered in <input type="text">
and write to the database. data.
By the way, you should do that. The big problem is this, it's not recording the record. I put echo
in the html with the variable to see if at least it was catching, and yes, it is, but in the bd it does not record ...
Is something wrong in my connection file or class? At the moment I think I'm not instantiating right inserir
, would it?
PS : Later I want to store the image or video path, type a mini image gallery manager so I'm using that foreach in test.php
They are the following files: conexao.php
, classes.php
, funcoes.php
and teste.php
.
connection.php
class Conexao {
public static $instance;
private function __construct() {
//
}
public static function getInstance() {
if (!isset(self::$instance)) {
self::$instance = new PDO('mysql:host=localhost;dbname=nomedobd', 'root', '',
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
self::$instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
self::$instance->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_EMPTY_STRING);
}
return self::$instance;
}
}
classes.php
class Imagem {
private $id;
private $nome;
private $caminho;
private $tipo_obj;
public function getId_img() {
return $this->id;
}
public function getNome_img() {
return $this->nome;
}
public function getCaminho_img() {
// return $this->caminho;
return $this->caminho;
}
public function getTipo_obj() {
// return $this->tipo_obj;
return $this->tipo;
}
public function setNome($nome) {
/* $this->id = $id_img;
$this->nome = $nome_img;
$this->caminho = $caminho_img;
$this->tipo_obj = $tipo_obj; */
$this->nome = $nome;
}
public function setCaminho($nome) {
$this->caminho = $nome;
}
public function setTipo($nome) {
$this->tipo = $nome;
}
}
funcoes.php
require_once "conexao.php";
require_once "geralog.php";
require_once "classes.php";
class DaoImagem {
public static $instance;
public function __construct() {
//
}
function inputText($nome) {
return "<input type='text' id='$nome' name='$nome' value='$nome' />";
}
public static function getInstance() {
if (!isset(self::$instance))
self::$instance = new DaoImagem();
return self::$instance;
}
public function Inserir(Imagem $nome){
try {
$sql = "INSERT INTO imagem (
tipo,
nome,
caminho)
VALUES (
:tipo,
:nome,
:caminho)";
$p_sql = Conexao::getInstance()->prepare($sql);
$p_sql->bindValue(":nome", $nome->getNome_img());
// $p_sql->bindValue(":nome", $imagem->getNome_img());
$p_sql->bindValue(":caminho", $nome->getNome_img());
// $p_sql->bindValue(":caminho", $imagem->getCaminho_img());
$p_sql->bindValue(":tipo", $nome->getNome_img());
// $p_sql->bindValue(":tipo", $imagem->getTipo_obj());
return $p_sql->execute();
} catch (Exception $e) {
print "Ocorreu um erro ao tentar executar esta ação, foi gerado
um LOG do mesmo, tente novamente mais tarde.";
GeraLog::getInstance()->inserirLog("Erro: Código: " . $e->getCode() . " Mensagem: " . $e->getMessage());
}
}
test.php
<?php
include_once('funcoes_imagem.php');
/* foreach ($_FILES["files"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["files"]["tmp_name"][$key];
$name = $_FILES["files"]["name"][$key];
move_uploaded_file($tmp_name, "public/$name");
}
} */
?>
<!DOCTYPE html>
<html>
<head lang="pt-br">
<meta charset="UTF-8">
<title>Home</title>
</head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>" enctype='multipart/form-data'>
<?php $nome = new DaoImagem(); ?>
<?php echo $nome->inputText('nome',$_POST['nome']);?>
<input type="submit" value="Enviar" />
</form>
<?php
if(isset($_POST['nome'])){
//$nome = new DaoImagem();
$envia = new DaoImagem();
//echo $_POST['nome'];
print $envia->Inserir($nome);
}
?>
</body>
</html>
Thanks in advance.