Data not being written to DB by PHP

0

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.

Last setting:

Catchable fatal error: Argument 1 passed to DaoImage :: Insert () must be an instance of Image, instance of DaoImage given, called in C: \ wamp \ www \ SystemUpload \ teste2.php on line 34 and defined in C: \ wamp \ www \ SystemUpload \ image_functions.php on line 25

    
asked by anonymous 18.01.2017 / 17:26

2 answers

0

I finally got it ... after catching a lot, I discovered that there were some errors in the class funcoes_imagem and in the test I needed to instantiate another class and define the methods ... Here's the result, maybe I can help other people: p>

conexão.php (não foi alterado)

funcoes_imagem.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 $nomeCerto){
            try {
                $sql = "INSERT INTO imagem (        
                    tipo,
                    nome,
                    caminho) 
                    VALUES (
                    :tipo,
                    :nome,
                    :caminho)";

                $p_sql = Conexao::getInstance()->prepare($sql);
                $p_sql->bindValue(":nome", $nomeCerto->getNome_img());
               // $p_sql->bindValue(":nome", $imagem->getNome_img());
                $p_sql->bindValue(":caminho", $nomeCerto->getNome_img());
               // $p_sql->bindValue(":caminho", $imagem->getCaminho_img());
                $p_sql->bindValue(":tipo", $nomeCerto->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');
?>
<!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'])){
            $nomeCerto = new Imagem();
            $nomeCerto->setNome($_POST['nome']);
            //$nome = new DaoImagem();
            $envia = new DaoImagem();
            //echo $_POST['nome'];
            print $nome->Inserir($nomeCerto);
        }
    ?>
    </body>
</html>
    
19.01.2017 / 17:43
0

To fix the error you mentioned in the comment, leave your form as follows:

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <?php $formulario = new DaoImagem(); ?> <?php echo $formulario->inputText('nome',$_POST['nome']);?> <input type="submit" value="Enviar" /> </form>

Then, I saw that you mentioned the parameter ': id_img' in your sql string, but did not add value to it with bindValue (). Take a look at this too.

    
18.01.2017 / 21:03