0

I can't seem to figure out why the button that is generated increments the vote_count values of every post in the database rather than just the one it is generated with. Does anyone know a way to make my code so that it will only increment the vote_count value in the same row as that specific post. I am using MySQL.

Here is my code

echo "<button class='button' id='press_me' >Press Me</button>";


<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<script>
    $(function (){
            $('#press_me').click(function(){
            var request = $.ajax({
                                    type: "POST",
                                    url: "server_code_increment.php"                               
                                  });
                                  request.done(function( msg ) {

                                        alert('Success');
                                        return;

                                  });
                                  request.fail(function(jqXHR, textStatus) {
                                        alert( "Request failed: " + textStatus );
                                    });
            });
    });
    </script>

Here is server_code_increment.php

<?php
// Connection to database
  $connection=mysqli_connect("localhost","root","","phplikes");
// Check connection
  if (mysqli_connect_errno())
    {
    echo 'NOT_OK';
    //echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

// Increasing the current value with 1
  $query = mysqli_query($connection,"UPDATE meme_vote SET vote_count = (like_count + 1)
    ");
 

  mysqli_close($connection);

  echo 'OK';   ?>
Luca
  • 49
  • 1
  • 5
  • Hi, your query doesn't have `where condition` so its updating all records. – Swati Oct 14 '20 at 04:19
  • How would I use where condition to make it only update the record corresponding to the buttons post? – Luca Oct 14 '20 at 04:23
  • You need to pass post_id as well with your ajax.. Can you show html code? – Swati Oct 14 '20 at 04:31
  • I don't have any html code right now, apart from the code to upload the videos, but thats not really relevant i don't think. This is just a prototype of a button that can act as a voting button. How would passing post_id work? – Luca Oct 14 '20 at 04:36
  • That value will update only those records with matching id and you need to pass same under where clause. – Swati Oct 14 '20 at 05:05
  • Can your provide an example of passing post_id with my ajax as im not too sure how to actually do it – Luca Oct 14 '20 at 05:16
  • You need to pass like this `data :{ id : postid}` here `postid` is value which you need to pass to backend..try with passing dummy value see if that works then try passing actual values .Also , get that value using `$_POST['id']` at your backend. Check for correct syntax [here](https://api.jquery.com/jquery.ajax/) – Swati Oct 14 '20 at 13:44
  • – Luca Oct 14 '20 at 19:13
  • These are the parts of my code that I edited but i can't seem to figure out what i did wrong as its not passing over the id – Luca Oct 14 '20 at 19:14

0 Answers0