0
I am getting error trying to make an insert in the database. Below.
<?php
define("CMD_CRIAR_POST", 0);
define("CMD_BUSCAR_POSTS", 1);
define("CMD_EDITAR_POST", 2);
define("CMD_EXCLUIR_POST", 3);
define("CMD_PUBLICAR_POST", 4);
define("CMD_REVERTER_POST", 5);
define("CMD_CRIAR_CATEG", 6);
define("CMD_EXCLUIR_CATEG", 7);
define("CMD_ADICIONAR_CATEG_POST", 8);
define("CMD_REMOVER_CATEG_POST", 9);
define("CMD_OBTER_POSTS_CATEG", 10);
define("CMD_ADICIONAR_AUTOR", 11);
define("CMD_REMOVER_AUTOR", 12);
$param_autor;
$bd_ok = false;
function acessa_bd($comando){
global
$bd_ok,
$param_autor,
$param_titulo,
$param_conteudo;
try {
if(!$bd_ok){
$servidor = "localhost";
$usuario = "root";
$senha = "*****";
$nomeDB = "sistema_blog";
$conn = new PDO("mysql:host=$servidor;dbname=$nomeDB", $usuario, $senha);
// configura o modo de erro para exceção
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$inserir = $conn->prepare("
insert into :tabela(:colunas)
values(:valores);
");
$consultar = $conn->prepare("
select :colunas
from :tabela;
");
$deletar = $conn->prepare("
delete from :tabela
where :coluna_cond=:valor_cond;
");
$alterar_titulo_post = $conn->prepare("
update postagens
set titulo=:novo_titulo
where post_id=:post_id;
");
$alterar_conteudo_post = $conn->prepare("
update postagens
set conteudo=:novo_conteudo
where post_id=:post_id;
");
$publicar_reverter_post = $conn->prepare("
update postagens
set publicado=:estado
where post_id=:post_id;
");
$bd_ok = true;
}
switch($comando){
case CMD_CRIAR_POST:
//$tabela = "postagens";
//$colunas = "autor_id, titulo, conteudo";
//$valores = $param_autor . $conn->quote($param_titulo) . $conn->quote($param_conteudo);
$inserir->execute(array(":tabela"=>"postagens", ":colunas"=>"autor_id, titulo, conteudo", ":valores"=>$param_autor . $conn->quote($param_titulo) . "," . $conn->quote($param_conteudo)));
break;
}
}
catch(PDOException $e){
// $conn->rollback();
echo "Erro: " . $e->getMessage();
die("\nErro ao conectar com a base de dados");
}
}
$param_autor = 1;
$param_titulo = "Olá Mundo";
$param_conteudo = "Este é um texto";
acessa_bd(CMD_CRIAR_POST);
?>
Error: SQLSTATE[42000]: Syntax error or access Violation: 1064 You have an error in your SQL syntax; check the manual that Corresponds to your Mysql server version for the right syntax to use near 'posts' ('autor_id, title, content') values('1 'Hello World ', 'This ' at line 1 Error when connecting with database
Cara has several unwarranted commas in her querys.
insert into :tabela(:colunas), values(:valores);
that comma before values is incorrect. Inselect :colunas, from :tabela;
, that comma before the from is also improper. Revise your querys.– user28595
I removed those commas and keeps making the same mistake.
– Carlos
I think quotes are showing up where I shouldn’t. In 'posts', for example, I did not quote and in error appears with quotes.
– Carlos
print the sql before running and see exactly where the error is in the query.
– Nelson Teixeira
A comma is missing between the values of
autor_id
andtitulo
:$param_autor . "," . $conn->quote($param_titulo)
– vmartins