Php Post Approval System

0

I'm creating a medium post approval system in "Gambiarra", what I did is, when the user posts, it gets value '0' in the 'Status' table in the database.

So I have a restricted page for the site's 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 in each post that when clicking will change the table Status from '0' to '1'. The problem is that when I click, it modifies all posts and not just what I'm clicking. I need something that just identifies that post.

For this I used a javascript script, the sequence of everything goes on:

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 ajax file:

<?php
  include_once('../_inc/conexao.inc.php');

  $up = $conexao->query("UPDATE vagas SET status = 1");
  $up->execute();
 ?>

Any advice to just select that post?

    
asked by anonymous 15.10.2018 / 16:03

1 answer

1

You need to pass the id of the element you want to update as a parameter in the function call:

PHP Code:

        <?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" data-id="<?php echo $row['id']; ?>" onclick="myAjax($(this).data('id'))" >aprovar</button>
            </div>
        <?php endwhile; ?>

JS Code:

<script type="text/javascript">
    function myAjax(idPostagem = null) {
      if (idPostagem) {
         $.ajax({
              type: "POST",
              url: '_controller/approvajax.php',
              data:{id: idPostagem}, // esse parametro é o id que vai ser usado na query?
              success:function(html) {
                alert(html);
              }
         });
      }
    }
</script>

SQL code:

<?php
  include_once('../_inc/conexao.inc.php');

  $up = $conexao->query("UPDATE vagas SET status = 1 where id = {$_POST['id']}");
  $up->execute();
 ?>
    
15.10.2018 / 16:47