Problem with Like system - Help required

I’m having a little trouble with my liking system.

I have it set up so that once the logged in user has liked a status the button changes from a blue thumbs-up button (like button) to a red X button (unlike button)

Now what’s happening is that user1 can like a status and receive the red X button to unlike.

I log out and log in as user2.

User2 is going to be shown the blue thumbs-up button and when user2 likes the same status user1 has liked the red X button disappears for user1 and it shows up for user2.

Now what happens to user3? Once I like the same status both users have liked the red X button shows up for user3 but not for user1 or user2.

Query:
"SELECT * FROM likes WHERE status_id = '$row_feed[sql_id]'";

status_id is the sql id from the feed table

Please spare me the “MySQL is deprecated use MySQLi or PDO” talk.

To display the like and unlike button:
if ($row_likes['like_username_from'] == $session['username']) {
echo '<button style="padding: 2px 3px 2px 3px;" class="btn btn-danger" type="submit" data-toggle="tooltip" title="Like" name="like_button" id="like_button"><i class="icon wb-close" aria-hidden="true"></i></button>'; } else { echo '<button style="padding: 2px 3px 2px 3px;" class="btn btn-info" type="submit" data-toggle="tooltip" title="Like" name="like_butto" id="like_butto"><i class="icon fa-thumbs-up" aria-hidden="true"></i></button>'; }

Should I not be matching the session to the liker’s username?

1 Like

You haven’t included enough code to tell what is happening

What more is needed? That’s pretty much what I have…I’m sorry if that sounded a little weird/offensive but what else is needed? I need to get this to work :slight_smile:

The schema for the likes table would be a start.

i.e. what running SHOW CREATE TABLE likes returns.

I’m afraid I don’t understand what you’re asking…

Do you mean this?
CREATE TABLE likes ( like_sqlid int(11) NOT NULL AUTO_INCREMENT, like_username_from varchar(255) DEFAULT NULL, like_fname_from varchar(255) DEFAULT NULL, like_lname_from varchar(255) DEFAULT NULL, like_username_to varchar(255) DEFAULT NULL, like_fname_to varchar(255) DEFAULT NULL, like_lname_to varchar(255) DEFAULT NULL, date varchar(255) DEFAULT NULL, time varchar(255) DEFAULT NULL, ip_address varchar(255) DEFAULT NULL, status_id varchar(255) DEFAULT NULL, global_time timestamp NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (like_sqlid) ) ENGINE=MyISAM AUTO_INCREMENT=174 DEFAULT CHARSET=latin1

1 Like

Thanks that helps narrow things down some (supper called but I’m back now)

status_id is varchar(255) DEFAULT NULL which seems odd because I’m used to id fields being numeric, and being able to be NULL, empty or up to 255 characters is unusual.

And if you are only using the like_username_from field value I don’t know why you’re SELECTing all the fields (the *)

But anyway, there are two possible problem areas that should be looked at that I can think of off hand.

The code you are using to output the $row_feed sql_id CONSTANT you are using in the query.

And the beginning of the PHP file(s) that deal with SESSION to make sure they aren’t sending out HTTP headers before the SESSION code.

I’m doing the * because I’m going to be echoing the users first name and last name who liked so for instance lets say user1 liked status 55 and user1 also liked status 57, it keeps track of it so that it knows user1 liked status 55 and it can match that like to the status in the like area.

I will make status_id a numeric field, thanks for that…didn’t realize it honestly

It depends if the id is your own or from PHP, A PHP SESSION id can be
http://php.net/manual/en/function.session-id.php

characters in the range a-z A-Z 0-9 , (comma) and - (minus)!

in which case a varchar would be used.

Then you don’t need to use the asterisk wild card. Just specify the first name and last name field.

It seems to me that the problem is with the query - should you not be querying for a “like” based on the ID of the thing you’re liking, and the user id who is logged in? You have multiple rows in the likes table, an individual one for each like_username_from value, but you don’t show how you loop around the results from your query (which will return all of them, I think) to get the one for the logged-in user, so I might be guessing too much.

No need to jump on that - you weren’t showing enough code for anyone to know you’re still using the old-style calls. But we know now, of course…

Thank you guys for the help, I got it to work, although I don’t think I needed to do this, I just used two queries. One getting the users id from the likes table and matching it to the sessions, and the other matching the status id from the feeds table to the status id in the likes table, if there is a simpler solution please tell, all comments/criticism are welcomed. :blush::blush: Thank you guys again, I’m sure I’ll be bothering you again :grin::+1:t2:

There probably is a simpler solution, but without knowing the two table layouts and how they interact with each other it’s impossible to suggest one. If I were doing a “likes” system for a product table, I’d probably do something like:

  • Products table - product id and other information, the thing that will be liked.
  • Users table - user id and other information
  • Likes table - product id, user id, other information

When a user likes something, create an entry in the likes table containing the user id, the product id, and whatever else such as the date/time it was done, IP tracking or whatever. When the user logs in and retrieves the product details, query the likes table with the product id and user id - if you get a result, display the “already liked” icon and link to remove the like, if you don’t, display the “like” link.

What you just explained is exactly what I did, I added a user id row in the likes table, it didn’t have it before and I think that’s what was messing me up

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.