You can only enter values and never SQL commands, which includes fields;
Example of how you could do your query:
$sqlupdate = " UPDATE eventos SET nome = ? , cod_tipo_evento = ? , cod_municipio_evento = ? WHERE num_codigo_pk = ?";
$sth = $db->prepare($sqlupdate);
$sth->bindValue(1 ,$_POST["nome_evento"], PDO::PARAM_STR);
$sth->bindValue(2 ,$_POST["cod_tipo_evento"], PDO::PARAM_INT);
$sth->bindValue(3 ,$_POST["cod_municipio_evento"], PDO::PARAM_INT);
$sth->bindValue(4 , $_POST["num_codigo_pk "], PDO::PARAM_INT);
$sth->execute();
I also recommend doing some validation on the dandos before inserting them directly; for example:
$codEvento = intval($_POST["cod_tipo_evento"]);
// Ou validar algo para ver se nao esta vazio
$nomEvento = isset($_POST["nome_evento"]) ? $_POST["nome_evento"] : '';
To have all the fields dynamically:
$campos = array();
if (isset($_POST["nome_evento"])) {
$campos[] = 'nome';
}
if (isset($_POST["cod_tipo_evento"])){
$campos[] = 'cod_tipo_evento';
}
if (isset($_POST["cod_municipio_evento"])){
$campos[] = 'cod_municipio_evento';
}
if(count($campos) == 0) {
die('Nao foi selecionado nenhum campo para atualizar!');
}
$sql = 'UPDATE eventos SET ';
$sql .= implode(" = ?,", $campos);
$sql .= ' = ? WHERE num_codigo_pk = ?';
This way you only generate the UPDATE of the fields that came by the POST;
You can see here in syntax Highlight that your code does not compile. Missing to close the quotes in the first line. A good IDE will warn you of this... I will publish a reply just for you to see the difference.
– brasofilo
If you intend to do so, I recommend using ORM to build QUERY.
– Papa Charlie
+1 the question why you are using the PDO library instead of the deprecated mysql functions_*
– jlHertel
Do you want to know if data has been informed or new name. is equal to old name.? If you tested my answer give feedback on it so I can adjust the answer - edit your question can change the sense of the answers.
– Papa Charlie
I have a reply that can help you
– Fábio Lemos Elizandro
@Papa Charlie I will test your solution now.
– Diego Filipe Pedro Santos
@Fábio Lemos Elizandro, put there all help is welcome
– Diego Filipe Pedro Santos
@Diegosantos the link to my answer is here for you who already use PDO is a smooth transition. If this is a mini tutorial for DBAL Doctrine, it will help you a lot to perform dynamic updates
– Fábio Lemos Elizandro