Php post approval system

Asked

Viewed 403 times

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?

  • 3

    If you don’t put one WHERE id = ? all rows of your table will be changed.

  • 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

  • As @rray said, if you do not specify the vacancy id, all table records will be updated..

  • Ok, put an example of how it should look in the loop and in the query.

  • 1

    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?

  • 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.

Show 1 more comment

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();
 ?>
  • Thank you very much Thiago, and answering the friend up there, this is the goal of the site, rather than spending too much time neglecting text, just posting a practical example the problem is solved.

  • 1

    If it helped you, mark your partner’s answer as the accepted/correct answer. Hug!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.