I have tried to make like and like system using ajax and mysql. Click like, like is added and Click again, like min 1. I want, when I click unlike, it will back to like. But this, unlike until minus..
我试图使用ajax和mysql制作类似的系统。点击喜欢,就像添加一样,然后再次点击,就像min 1.我想,当我点击不同时,它会回归喜欢。但这个,不像直到减去..
This is my mysql
这是我的mysql
<?php
include 'connect.php';
session_start();
$ip=$_SESSION['id'];
if ($_POST['id'])
{
$id=$_POST['id'];
$ip_sql=mysql_query("select id_user from social where track_id='$id' and id_user='$ip'");
$count=mysql_num_rows($ip_sql);
if ($count==0)
{
$sql = "update track set jumlah_like=jumlah_like+1 where track_id='$id'";
mysql_query($sql);
$sql_in = "insert into social (id_user,track_id) values ('$ip','$id')";
mysql_query($sql_in);
$result=mysql_query("select jumlah_like from track where track_id='$id'");
$row=mysql_fetch_array($result);
$love=$row['jumlah_like'];
?>
<span class="broke_love" align="left"><?php echo $love; ?></span>
<?php
}
else
{
$sql = "update track set jumlah_like=jumlah_like-1 where track_id='$id'";
mysql_query($sql);
//$sql_in = "insert into social (id_user,track_id) values ('$ip','$id')";
//mysql_query($sql_in);
$result=mysql_query("select jumlah_like from track where track_id='$id'");
$row=mysql_fetch_array($result);
$love=$row['jumlah_like'];
echo "<span class=on_img align=left>$love</span>";
}
}
?>
2 个解决方案
#1
0
The problem is algorithmic that is causing your button to be stuck on 'unlike'.
问题是算法导致你的按钮卡在“不像”上。
Your basic condition is: if ($count==0)
which always returns greater than 0 after the statement $sql_in = "insert into social (id_user,track_id) values ('$ip','$id')"; is run.
您的基本条件是:if($ count == 0)在语句$ sql_in =“insert into social(id_user,track_id)values('$ ip','$ id')”之后总是返回大于0的值;运行。
Therefore, your code gets stuck in always executing the else, and continously diminishing the likes.
因此,你的代码总是在执行else时陷入困境,并不断减少喜欢。
You need to change your logic to be more:
您需要将逻辑更改为更多:
- Check if likes exist and add for specific user.
- If like already exists, remove like record.
检查是否存在喜欢并为特定用户添加。
如果已经存在,请删除类似记录。
#2
0
I think you are missing something from your script as you do not appear to remove a record from the table social.
我认为你遗漏了脚本中的内容,因为你似乎没有从表格社交中删除记录。
However most of your selects and updates can be done with a single piece of SQL. Something like:-
但是,大多数选择和更新都可以使用单个SQL完成。就像是:-
UPDATE track a
LEFT OUTER JOIN social b ON a.track_id = b.track_id AND id_user='$ip'
SET a.jumlah_like = a.jumlah_like + IF(b.track_id IS NULL, 1, -1)
It would probably be better to have a votes table rather than adding and deleting records on the social table. One row per vote.
拥有一个投票表而不是在社交桌上添加和删除记录可能会更好。每票一排。
#1
0
The problem is algorithmic that is causing your button to be stuck on 'unlike'.
问题是算法导致你的按钮卡在“不像”上。
Your basic condition is: if ($count==0)
which always returns greater than 0 after the statement $sql_in = "insert into social (id_user,track_id) values ('$ip','$id')"; is run.
您的基本条件是:if($ count == 0)在语句$ sql_in =“insert into social(id_user,track_id)values('$ ip','$ id')”之后总是返回大于0的值;运行。
Therefore, your code gets stuck in always executing the else, and continously diminishing the likes.
因此,你的代码总是在执行else时陷入困境,并不断减少喜欢。
You need to change your logic to be more:
您需要将逻辑更改为更多:
- Check if likes exist and add for specific user.
- If like already exists, remove like record.
检查是否存在喜欢并为特定用户添加。
如果已经存在,请删除类似记录。
#2
0
I think you are missing something from your script as you do not appear to remove a record from the table social.
我认为你遗漏了脚本中的内容,因为你似乎没有从表格社交中删除记录。
However most of your selects and updates can be done with a single piece of SQL. Something like:-
但是,大多数选择和更新都可以使用单个SQL完成。就像是:-
UPDATE track a
LEFT OUTER JOIN social b ON a.track_id = b.track_id AND id_user='$ip'
SET a.jumlah_like = a.jumlah_like + IF(b.track_id IS NULL, 1, -1)
It would probably be better to have a votes table rather than adding and deleting records on the social table. One row per vote.
拥有一个投票表而不是在社交桌上添加和删除记录可能会更好。每票一排。