0
Good night, you guys,
I’m a beginner in the area, and I ask you to help me, I have a table with php database results, in one of the fields has a button that stores the id (primary key) and clicking that button calls a modal window, and I wanted to add that by clicking this id button, it updated a status from a field of 0 to 1 in the database. I’m trying to update with ajax, but it’s not working out, who can help me thank you mto.
My code is like this:
//Tabela
<table class="table">
<thead>
<tr>
<td><center>Visualizar</center></td>
<td><center>De</center></td>
<td><center>Assunto</center></td>
<td><center>Data</center></td>
</tr>
</thead>
<tbody>
<?php
include "config.php";
$stmt = $db->prepare("select id,msg_assunto,msg_text,lida_para from mensagens");
$stmt->execute();
while($row = $stmt->fetch()){
if($row > 0){
?>
<tr>
<td><center><button type="button" id="btn-destaque" class="btn btn-xs" data-toggle="modal" data-target="#modalLRForm<?php echo $row['id']; ?>"><img src="img\pesquisa.png"></button></center></td>
<td><?php echo $row['msg_de']?></td>
<td><?php echo $row['msg_assunto']?></td>
</tr>
</tbody>
</table>
//ajax
<script>
$("#btn-destaque").click(function () { //Quando o botão destaque for pressionado
var idAnun = $row['id']; //Aqui id do botão
$.ajax({
method: "post", //Escolhe o método de envio
url: "/fetch.php", //O endereço do script php que fará o update
data: {id: idAnun} //Envio da ID do anuncio e a ação (ativar)
}).done(function (answer) {
alert("sucesso");
}).fail(function (jqXHR, textStatus) {
alert("falhou");
});
});
</script>
My fetch.php:
<?php
include("config.php");
if (isset($_POST['id']) && $_POST['id'] != 0) { //$_POST['id'] != 0 (em caso de tabela auto incremento o id será do tipo inteiro e nunca será iniciada em 0) isso em MySQL não sei outros Bancos de dados
$db = connectdb(); //função do "script_BD.php"
$update = $db->prepare("UPDATE mensagens SET lida='1' WHERE id=:id");
$update->bindValue(":id",$_POST['id'],PDO::PARAM_INT);
$update->execute(); // executa update;
}
Thank you I managed to identify the error and I tidied up, but thank you!
– Joana