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
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
如果您查看文档( http://php.net/manual/zh/pdo.prepare.php ),您会发现您应该在插入语句的值列表中使用:id,:nome,:estado
直接插入值的过程。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.