2

i was following a tutorial on youtube to make a simple Like/unlike button for my status system, i got most of it done, but it will not UPDATE my likes and not INSERT the like into the database, please help me say whats wrong, i tried so much now..

Function to get status:

 function getStatus($conn) {
       $sql = "SELECT * FROM status ORDER BY sid DESC";
       $query = mysqli_query($conn, $sql);
       while ($row = $query->fetch_assoc()) {
           echo "<div class='post'>".$row['message']."<br>";

                $result = mysqli_query($conn, "SELECT * FROM status_like WHERE uid=1 and sid=".$row['sid']."");
                if (mysqli_num_rows($result) == 1) {
                    echo "<span><a href='' class='unlike' id='".$row['sid']."'>unlike</a></span>";
                } else {
                    echo "<span><a href='' class='like' id='".$row['sid']."'>like</a></span></div>";
                }
                }


       }

jquery code

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
          <script type="text/javascript">
            $(document).ready(function(){
                $('.like').click(function(){
                    var sid = $(this).attr('id');
                    $.ajax({
                        url: 'test.php',
                        type: 'post',
                        async: false,
                        data: {
                            'liked': 1,
                            'sid': sid

                        },
                        success:function(){

                        }
                    });
                });
            });
          </script>

and the last php code where i think the problem is:

if (isset($_POST['liked'])) {
        $sid = $_POST['sid'];
        $sql = "SELECT * FROM status WHERE sid=$sid";
        $query = mysqli_query($conn, $sql);
        $row = mysqli_fetch_array($query);
        $n = $row['likes'];
        $uid = 1;

        $sql2 = "UPDATE status SET likes=$n+1 WHERE sid=$sid";
        $sql3 = "INSERT INTO status_like (uid, sid, username) VALUES (1, '$sid', '$uid')";
        mysqli_query($conn, $sql2);
        mysqli_query($conn, $sql3);
        exit();


    }
8
  • 1
    set alert in click function.. and echo in if (isset($_POST['liked'])) { } and tell me Commented Mar 2, 2017 at 2:55
  • okey i added alert('test'); inside the click function and i get the alert up when i click on the "like button" but i still dont understand where to put the echo. @vSugumar Commented Mar 2, 2017 at 3:55
  • Inside if. Also Turn on error reporting in problem code and code network tab and click on response Commented Mar 2, 2017 at 4:29
  • 1
    WARNING: When using mysqli you should be using parameterized queries and bind_param to add user data to your query. DO NOT use string interpolation or concatenation to accomplish this because you have created a severe SQL injection bug. NEVER put $_POST, $_GET or any user data directly into a query, it can be very harmful if someone seeks to exploit your mistake. Commented Mar 2, 2017 at 5:39
  • @tadman can you please explain this better for me? where did i do something bad? and how should i do it? :) Commented Mar 2, 2017 at 5:44

1 Answer 1

1
if (isset($_POST['liked'])) {
        $sid = $_POST['sid'];
        $sql = "SELECT * FROM status WHERE sid=$sid";
        $query = mysqli_query($conn, $sql);
        $row = mysqli_fetch_array($query);
        //$n = $row['likes']; // your code            
        $n = (int) $row['likes']; // try like this.. might be likes in string so convert to int
        $uid = 1;

        //$sql2 = "UPDATE status SET likes=$n+1 WHERE sid=$sid"; // Your code

        // Do like this `status` in query because status is reserved keyword of MySql for more details you could visit this link https://dev.mysql.com/doc/refman/5.7/en/keywords.html
        $sql2 = "UPDATE `status` SET likes=$n+1 WHERE sid=$sid";
        $sql3 = "INSERT INTO status_like (uid, sid, username) VALUES (1, '$sid', '$uid')";
        mysqli_query($conn, $sql2);
        mysqli_query($conn, $sql3);
        exit();


    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.