Data is not being written to the BD by PHP

Asked

Viewed 168 times

0

I’m studying PHP object oriented and, from some tutorials taken from the net (Pdo, pojo, Dao...), I made a very simple system in which I get the value inserted in <input type="text"> and write to the database.

By the way, I should do this. The big problem is this, it’s not recording the record. I put a echo html with the variable to see if it was at least picking up, and yes, it is, but in the comic it does not save...

Is there something wrong with my connection file or class? At the moment I don’t think I’m instantiating right the inserir, that would be it?

PS: Later I want to store the image or video path, like a mini image gallery manager so I’m using that foreach in the test.php

Are the following files: conexao.php, classes.php, funcoes.php and teste.php.


php connection.

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;
    }
}

php classes.

    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;
    }

}

php functions.

    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());
        }
    }

php test.

        <?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>

From now on, thank you.

Last adjustment:

Catchable fatal error: Argument 1 passed to Daoimagem::Inserir() must be an instance of Imagem, instance of Daoimagem Given, called in C: wamp www Sistemaupload teste2.php on line 34 and defined in C: wamp www Sistemaupload funcoes_imagem.php on line 25

  • To start, change the command "SET name utf8" to "SET NAMES utf8". You can paste the error message(s) to us?

  • Whoa, thanks for the answer. I have here the php error log... line 5 and 29 of teste2.php (test.php in the case), which is in my if and where it prints the input on the screen...

  • [18-Jan-2017 17:41:37 America/Sao_paulo] PHP Notice: Undefined variable: PHP_SELF in C: wamp www Sistemaupload teste2.php on line 27 [18-Jan-2017 17:41:37 America/Sao_paulo] PHP Stack trace: [18-Jan-2017 17:41:37 America/Sao_paulo] PHP 1. {main}() C: wamp www Systemsupload teste2.php:0 [18-Jan-2017 17:41:41 America/Sao_paulo] PHP Notice: Undefined variable: PHP_SELF in C: wamp www Systemsupload teste2.php on line 27 [18-Jan-2017 17:41:41 America/Sao_paulo] PHP Stack Trace: [18-Jan-2017 17:41:41 America/Sao_paulo] PHP 1. {main}() C: wamp www Sistemaupload teste2.php:0

2 answers

0


I finally managed... after getting a lot, I found that I had some errors in the class functions_image and in the test I needed to instantiate another class and define the methods... Here’s the result, maybe I can help other people:

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());
            }
        }

php test.

<?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>

0

To correct 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 you did not add value to it with bindValue(). Take a look at this too.

  • Hi Luiz, thanks for the reply... The form yesterday after posting vi that lacked the $_server[]... but the sql already changed, because the id is PK and automatica, I removed from the sql string, so: INSERT INTO image (type, name, path) VALUES (:type,:name,:path)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.