开发者

PHP/MySQL Like Button

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 r开发者_开发技巧egisters 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


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.


<?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 ;-)


<?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>


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"/>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜