title: SQL/PHP - Check if user already liked post and if yes unlike?
body: <p>So I have these tables.</p>
  
  <p><strong>Table: Likes</strong></p>
  
  <p><strong>user_id :</strong> The user that logged in</p>
  
  <p><strong>post_id :</strong> The article where the user clicked like.</p>
  
  <p><a href="https://i.stack.imgur.com/ALhvg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ALhvg.png" alt="enter image description here"></a></p>
  
  <p>&nbsp;</p>
  
  <p>Then I have a Like.class.php which stores the getters and setters and the functions. </p>
  
  <p>I have two right now, <strong>Addlike();</strong> which enters the id (auto increment) and the user_id and post_id of the article/posts he liked.</p>
  
  <p>Countlike(); counts the entries with a simple <code>rowCount();</code> of all the rows.</p>
  
  <pre><code>public function Addlike($postid){
  
          $conn = db::getInstance();
          $query = "insert into likes (post_id, user_id) values (:post_id, :user_id)";
          $statement = $conn-&gt;prepare($query);
          $statement-&gt;bindValue(':post_id',$postid);
          $statement-&gt;bindValue(':user_id',$this-&gt;getUserId());
          $statement-&gt;execute();
          $result = $statement-&gt;fetchAll(PDO::FETCH_ASSOC);
          return $result;
      }
  
      public static function Countlike(){
          $conn = db::getInstance();
          $statement = $conn-&gt;prepare("SELECT * from likes");
          $statement-&gt;execute();
          $likecount =  $statement-&gt;rowCount();
          return $likecount;
      }
  </code></pre>
  
  <p>&nbsp;</p>
  
  <p><strong>My question</strong>: How can I now check if the user already liked the post (out of the DB) and if he presses like again it should remove his entry from the DB, any ideas on how I can turn this into code? Also the likes need to be bonded to the posts for example, every post has it own like counter/number</p>