0
votes

How i can insert this query in database PDO?

    public function insertPessoa($estado){

                $this->$estado = $estado;

                echo "<pre>";
                $linkDeputados =

 file_get_contents('https://dadosabertos.camara.leg.br/api/v2/deputados?siglaUf='.$estado.'&ordem=ASC&itens=100&ordenarPor=nome&pagina=1');
            $json = json_decode($linkDeputados);
            $deputados = (object) $json;
            $connectar = new PDO("pgsql:host=localhost; dbname=postgres", "postgres", "123456");


            foreach ($deputados->dados as $objeto => $o)
            {
                $sql = " INSERT INTO  deputados 
                                            (   id,
                                                'nome', 
                                                'estado',
                                                'web_partido', 
                                                'sigla_partido',
                                                'url_foto'
                                            ) 

                        VALUES
                                            (   
                                                 {$o->id},
                                                '{$o->nome}', 
                                                '{$o->siglaUf}', 
                                                '{$o->uriPartido}', 
                                               '{$o->siglaPartido}',
                                                '{$o->urlFoto}'

                                            )";

            }

            $query = $connectar->prepare($sql);
            $query->bindValue(':id', $o->id);
            $query->bindValue(':nome', $o->nome);
            $query->bindValue(':estado', $o->siglaUf);
            $query->bindValue(':web_partido', $o->uriPartido);
            $query->bindValue(':sigla_partido', $o->siglaPartido);
            $query->bindValue(':url_foto', $o->urlFoto);

            $query->execute();

            if($query){
                echo 'Registro Inseridos com Sucesso!<br>';
            }

        }

This give me a error:

Warning: PDOStatement::bindValue(): SQLSTATE[HY093]: Invalid parameter number: :id in C:\xampp\htdocs\pesquisaDeputados\pessoa.class.php on line 79

2
This function is part of a class, are you creating a new pdo connection within every function, isso que você está fazendo? - Stark
Yes, this function is part of my class and i use de PDO connection for the ohter functions too...it's wrong? - MP91
I think the foreach loop should close after $query->execute(); not before $query = $connectar->prepare($sql); - ASR
You should create a new class for the db connection and then just inject the connection into your class, that way you're not creating a new connection over and over. Boa sorte. - Stark

2 Answers

0
votes

Mysqli doesn't find the :id string in your sql query because you are injecting the object not using prepared statements

You sql statement should be:

$sql = " INSERT INTO  deputados 
                                        (   id,
                                            'nome', 
                                            'estado',
                                            'web_partido', 
                                            'sigla_partido',
                                            'url_foto'
                                        ) 

                    VALUES
                                        (   
                                             :id,
                                            :nome, 
                                            :estado, 
                                            :uri_partido, 
                                            :sigla_partido,
                                            :url_foto

                                        )";

and you need to bind the param not the values

0
votes

If you look at the docs (http://php.net/manual/en/pdo.prepare.php) you will see that you should be using :id,:nome,:estado in the values list of the insert statement instead of inserting values directly.