4

I've made a 'like' button for my product pages with this code:

<?php
if('POST' == $_SERVER['REQUEST_METHOD']) {
$sql = "UPDATE table set `likes` = `likes`+1 where `product_id` = '1'";
$result=mysql_query($sql);
}
?>

<form action="<?php echo $_SERVER['PHP_SELF']?>" method="POST">
<input type = "submit" value = "like"/>
</form>

Works like a charm excpet for one minor problem being that every visit to the page registers a 'like'.

Could someone help explain what i need to chnage/add in order that new 'likes' are only registered when the actual form is submitted?

Thanks Dan

1
  • 3
    With the default REQUEST_METHOD being GET I don't see how this could cause a like when visiting the page. Unless you mean the user pressing F5? Commented Sep 13, 2011 at 11:34

5 Answers 5

9

A better solution rather than submitting the page and the whole page reloading would be to make an AJAX request, this is how Facebook 'likes' work.

This can be achieved using the jQuery JavaScript library.

The general outline would be:-

1) Click button

2) Send AJAX request

3) Update HTML to show button has been clicked and prevent reclicking of button.

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

2 Comments

Thanks Bruce, i'll look into that now actually but for the time being i'll stick with this. Thanks for the suggestion though, it's certainly a better way of doing it, i just need to see if i can put it together.
@Dan I know this is an older question, but AJAX would definitely be better. Not that hard, either :) When the like button is clicked, just do something like: $.post('like.php',{ parameter1: 'val1', parameter2: 'val2', anotherParameter: 'test' }, function(data) { /* data is the response data from the request */ }); (you don't actually need to pass anything in the second argument, it is just to pass post data). Then, you could make a file called like.php and do the mysql/php stuff there.
4
<?php
if($_POST['like']) {
$sql = "UPDATE table set `likes` = `likes`+1 where `product_id` = '1'";
$result=mysql_query($sql);
}
?>

<form action="<?php echo $_SERVER['PHP_SELF']?>" method="POST">
<input type = "submit" value = "like" name='like'/>
</form>

This should work ;-)

6 Comments

Although not a dealbreaker by any stretch, this doesn't seem to update the figure shown on the page. On a refresh or return visit, the number has increased correctly, but not on form submission. Is there a fix for this or should i just live with it? Thanks
@Dan, can you show the code for displaying the like count. A blind guess would be that the SELECT occurs before the UPDATE..?
Sure Bruce, its just a simple <p><?php echo $result['likes'];?> People Like This</p> and it is displayed below my query at the mo. But yes, my SELECT does appear above my UPDATE...i'll switch them
Moving UPDATE above SELECT brings back my original problem unfortunatley
I think that might be for another question Bruce :) Thanks for all your help.
|
4
<?php
if ($_POST['like']){
   $sql = "UPDATE table set `likes` = `likes`+1 where `product_id` = '1'";
   $result=mysql_query($sql);
}
?>

<form action="<?php echo $_SERVER['PHP_SELF']?>" method="POST">
   <input type = "submit" name="like" value = "like"/>
</form>

Comments

2

First of all - in your sql you have:

`product_id` = '1'

do not use id value as a string:

`product_id` = 1

About your problem:

Add another condition:

if ('POST' == $_SERVER['REQUEST_METHOD']) {
    if ( !empty($_POST['submitType']) && ( $_POST['submitType'] == 'like' ) ) {
        $sql = "UPDATE table set `likes` = `likes`+1 where `product_id` = '1'";
        $result=mysql_query($sql);
    }
}

and in html:

<input type = "submit" name="submitType" value = "like"/>

1 Comment

Ah good spot, i was testing in PHPmyAdmin first and once i'd got the query qorking just pasted over without thinking, thanks for picking me up on that.
1

Sounds like some kind of old question, but I wonder why noone has said, that op's approach doesn't sound quite right. You try to just count likes (set likes=likes+1). It has many disadvantages:

  • You miss information, who gave the like. Thus you won't be able to reconstruct the whole picture
  • Users won't be able to "undo" likes (as you don't record who liked the post)
  • In case of many concurrent likes I feel like you'd get some kind of data race or a long delays, because MySQL would need to process every request on a single field in order.

Much better idea is to create separate table in the DB named "product_likes" with columns like product_id, user_id, date. Of course, product id and user id should be unique together. Thus you'll always know the full picture and will be able to see who liked the product. Even if accidentally you'll issue the second like from the same user about the same product, it won't be stored due to db constraints.

Also it will be possible to extend it to i.e. emotions-reactions, just by adding new column like "like_type" and updating the constraint correspondingly.

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.