Readerik
Readerik

Reputation: 37

Deleting specific article from database with PHP

I'm creating a blog site and I got stuck in this phase where I want to allow authors to delete their own posts, they can already see the delete button below each of their posts. In the DB table I have an author, content and ID of the article. Now I need to get the specific ID of the article that they want to delete, but I have no idea how to do this, the delete button is the same for every article, so I can't use that for help. I have this code to get articles from my DB and the button click code ready. For better imagination here is an image of the site -

https://i.sstatic.net/Szldx.png

 ... previous code
 <?php } 

    $articles->ArticlesConn();
    $result = mysql_query("SELECT * FROM articles_table");

    while($row = mysql_fetch_array($result)){    // loop to generate content
    ?>  
    <div class="container"> // responsive stuff
     <div class="row">      // responsive stuff
      <div class="twelve columns">  // responsive stuff
    <?php
    echo       "<h3>" . $row['Title'] . "</h3>
                <p>" . $row['Content'] . "</p>"; ?>
      </div>
     </div>
     <div class="row">
      <div class="eight columns">
                <address><?php echo "Author - " . $row['Author']; ?> </address><br><br><br> /// nick of the author below each article
      </div>
      <div class="four columns">

                <?php
                if ($row['Author'] == $_SESSION["username"]) // show delete button if the logged user is an author of generated article
                { ?>
                    <input type="button" name="delete" value="Delete article"/>         
          <?php }   ?>
      </div>    
      </div>
     </div>
    <?php
        }
    ?>

Upvotes: 2

Views: 809

Answers (1)

pchmn
pchmn

Reputation: 534

To solve your problem, you should add a form to wrap your input.

This form will call the page itself. In this form there is a hidden input with the value of the article id. So when you submit the form, you can get the article id via $_POST['name of the hidden input'].

I will show you the code :

<?php } 

   if (isset($_POST['article-id'])) {
       // prevent SQL injection
       $articleId = mysql_real_escape_string($_POST['article-id']);
       $res = mysql_query('DELETE FROM articles_tab WHERE id='.$articleId);

       if (!$res) {
          die(mysql_error());
       }
   }

   $articles->ArticlesConn();
   $result = mysql_query("SELECT * FROM articles_table");

   while($row = mysql_fetch_array($result)){    // loop to generate content
   ?>  
   <div class="container"> // responsive stuff
    <div class="row">      // responsive stuff
     <div class="twelve columns">  // responsive stuff
   <?php
   echo       "<h3>" . $row['Title'] . "</h3>
               <p>" . $row['Content'] . "</p>"; ?>
     </div>
    </div>
    <div class="row">
     <div class="eight columns">
                <address><?php echo "Author - " . $row['Author']; ?> </address><br><br><br> /// nick of the author below each article
     </div>
     <div class="four columns">

               <?php
               if ($row['Author'] == $_SESSION["username"]) // show delete button if the logged user is an author of generated article
               { ?>
           <form method="post">
               <input type="hidden" name="article-id" value="<?php echo $row['ID'] ?>"/>
               <input type="submit" value="Delete article"/>    
           </form>     
         <?php } ?>
     </div>    
     </div>
    </div>
   <?php
       }

But you should use mysqli or PDO instead of the old mysql extension, look at this : http://php.net/manual/en/function.mysql-query.php

Upvotes: 1

Related Questions