0
I am creating an approval system of posts middle in "Gambiarra", what I did is, when the user makes a post, it receives the value '0' in the table 'Status' in the database.
So, I have a restricted page for the site Adm, on this page I have a loop that displays the posts with Status 0 (Posts not yet approved), in this loop I also have a button on each post that clicking will change the Status table from '0' to '1'. The problem is that when I click, it modifies all the posts and not just what I’m clicking. need something that idenfique just that post.
For this I used a javascript script, the sequence of everything will follow:
The loop on the restricted page:
<?php while ($row = $sql->fetch(PDO::FETCH_ASSOC)) { ?>
<div class="post">
<h2 id="titulo"><?php echo $row['titulo']; ?></h2>
<p>POSTAGEM</p>
<button type="button" name="button" onclick="myAjax()" >aprovar</button>
</div>
<?php } ?>
Javascript on the restricted page:
<script type="text/javascript">
function myAjax() {
$.ajax({
type: "POST",
url: '_controller/approvajax.php',
data:{id:$("#ajax").val()},
success:function(html) {
alert(html);
}
});
}
</script>
The file of the ajax:
<?php
include_once('../_inc/conexao.inc.php');
$up = $conexao->query("UPDATE vagas SET status = 1");
$up->execute();
?>
Any advice to select just that post?
If you don’t put one
WHERE id = ?
all rows of your table will be changed.– rray
And to send this to Update, you’ll have to put this in your element at the time of while, so that javascript sees this ID and can insert this in the update
– Ruggi
As @rray said, if you do not specify the vacancy id, all table records will be updated..
– renanvm
Ok, put an example of how it should look in the loop and in the query.
– user117052
Emerson, this is not the purpose of this site. The goal is not to "make the code" for you. You’ve already read the site guide? Tour / What kind of questions should I ask?
– Luiz Felipe
It’s for me to understand how to do it, Where id = What value do I put here? What reference should I make to this id in the loop? The purpose of the site is to discuss the problem and help the friend here, if exemplify how the correct code should be, the purpose of the site is concluded.
– user117052