title: Jquery favorites star button
body: <p>I'm new with jquery and I have some problems with this code. I'm trying to make work a favorite button (in this case a star image): in my website, each item (cd) has a favorite button and, if you click on it (and it is not colored), the item is inserted in the favorite items table and the button becomes colored (yellow); if you click on the button and it is already colored (so this means it's a favorite), the item is removed from the table and the button returns with no color.
  I have two problems with this code:</p>
  <ol>
  <li>Using <code>$(&quot;.star&quot;)</code> it obvoiusly makes all stars (even other items' stars) become yellow, instead I want only the star I've clicked on to become yellow;</li>
  <li>How can I make the changes persistent? In other words, I would like that if an item is added to favorites, its star keeps being yellow in the future, unless I obviously click another time on that.</li>
  </ol>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>$(".star").on('click',function(){
    $.ajax({
      url:'homepage/homepage_query.php',
      data: {ins:true, title:$("#right_title").text()},     
      datatype:'json',
      success: function(json){
        if(json == 0){  //it has been removed from favorites
          $(".star").css({"background-color":"initial"});
        }else{    //it has been added to favorites
          $(".star").css({"background-color":"#ffdb4d"});
        }
      },
      error: function(e){
        console.log(e.message);
      }
    });
  });</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>if(isset($_GET["ins"]) &amp;&amp; isset($_GET["title"])){  
          $db = new PDO("mysql:host=localhost;dbname=music", "user", "pwd");
          $username = $_SESSION["name"];
          $us = $db-&gt;quote($username);
          $titolo = $_GET["title"];
          $ti = $db-&gt;quote($titolo);
          $rows = $db-&gt;query("SELECT * FROM favorites WHERE username=$us AND titolo=$ti");
          $res = $rows-&gt;fetchALL(PDO::FETCH_ASSOC);
          if($res){   
              $rows = $db-&gt;query("DELETE FROM favorites WHERE username=$us AND titolo=$ti");
              $res = $rows-&gt;fetchALL(PDO::FETCH_ASSOC);
              var_dump($res);
              echo 0;
          }else{     
              $db-&gt;query("INSERT INTO favorites VALUES ($us, $ti)");
              echo json_encode($res);
          }
      }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;img class="star" src="img/stella.png"/&gt; &lt;!--the star I click on--&gt;</code></pre>
  </div>
  </div>
  </p>