Data is not entered into the database using PDO

Asked

Viewed 32 times

0

With a problem, basically the data is not being entered in the database, and is not showing error, I am still beginner, and I can not understand where the error is:

Script:

 require_once ('config.php');



    $idAnime = 5025;
    while($idAnime < 8000)
    {


        $verificaAnime = $crud->select('idMal','animesmal','WHERE idMal = ?')->run([$idAnime]);
        $contaRow = $verificaAnime->rowCount();

        if($contaRow == 0)
        {

            $text = $apiMal->get_busca($idAnime);



            if(is_array($text))
            {

                $title = $text['title'];
                $id = $text['idAnime'];
                $score = $text['score'];
                $desc = $text['description'];
                $desc = $translate->runTranslate($desc);

                $dados = array($id,$title,$desc);

                $certo = $crud->insert('animesMal','idMAl = ?, name = ?, description = ?, score = ?')
                ->run(array($id,$title,$desc, $score));

                if($certo)
                {
                    echo 'sucesso ao Adicionar o anime '.'<span class="msg-sucesso">'.$title.'</span>'.' ao banco de dados.<br>';
                }else
                {
                    echo 'Erro ao inserir animes no banco de dados<br>Erro:  '.$certo;
                }


            }else
            {
                echo '<span class="msg-erro">'.$text.'</span><br>';
            }
        }else
        {
            echo '<span class="msg-erro">'."ERRO: O anime já existe no banco de dados</span><br>";            // echo var_dump($verificaAnime);
        }
    $idAnime++;
    }

CRUD:::

    <?php

 namespace Classes;

class Crud{

    private $query, $run;


    public function __construct()
    {
        openDatabase();
    }

    private function set_statement($s)
    {
        if(openDatabase()){

            $this->query = openDatabase()->prepare($s);

        } else{
            die(closeDatabase($conn));
        }
    }

    // Execulta
    private function do_run()
    {

        $this->query->execute($this->run);
    }

    public function run($r = [])
    {
        $this->run = $r;
        $this->do_run();

        return $this->query;
    }

    // Funçao que insert dados no banco de dados
    public final function insert($table, $values)
    {

        $this->set_statement('INSERT INTO '.$table.' SET '.$values.'');
        return $this;
    }

    public final function select($fields, $table, $condition = '')
    {

        $this->set_statement('SELECT ' .$fields.' FROM '.$table.' '.$condition);
        return $this;
    }

    public final function update($table, $values,$condition)
    {
        'UPDATE $tabela SET mail = ? WHERE id = ?';
        $this->set_statement();
        return $this;
    }
}
  • How is your Insert function?

  • pear ai grandpa add my crud

  • Your Insert is not correct. The way you wrote the insert serves for the function update

  • What do you mean? That’s the mistake?

  • Try the = and leave only the fields

  • Instead of sending like this: insert('animesMal','idMAl = ?, name = ?, description = ?, score = ?') boss like this insert('animesMal','idMAl, name, description, score')

  • I tried and still not working, and shows no error

  • Before if($certo) put that print_r($certo->errorInfo()); exit;

  • it had returned an error for having removed the = ? , after I put them back it only returns Array ( [0] => 00000 [1] => [2] => )

  • At position 1 of the array shows which message?

  • For a better display you put like this: echo "<pre>"; print_r($certo->errorInfo()); echo "</pre>"; exit;

  • So, hj ran the code the way it is in this post and everything worked perfectly kk. Now I’m very lost. PQ have no idea what happened and how it started again kk

  • Strange, because the syntax of Insert is INSERT INTO tabela (campo1, campo2, campo3) VALUES () and not INSERT INTO tabela SET campo1 = 1, campo2 = 2

  • pse kk, Vlw for help

Show 9 more comments
No answers

Browser other questions tagged

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