[英]How to add a Follow - UnFollow Button on my Website?
我想在我的网站上创建一个按钮,以便用户可以在某个游戏上单击“关注”或“取消关注”。
这是我的关注表: 关注按钮表
当用户单击与游戏ID关联的按钮时,应在该ID的game_follow数据库中插入1。
(我已经有一个游戏和用户表,而且我知道如何获取每个ID)
我知道这是一个广泛的问题,但是也许有人可以指出正确的方向,或者向我展示另一个教程。
编辑:到目前为止,这是行不通的:
这是我的按钮:
<button class="follow-game" data-game_id="<?php echo $game_id['id']; ?>">Follow!</button>
这是我的ajax电话:
$(function(){ $('.follow-game').click(function(){ follow_game(this); }); }); function follow_game(obj){ var game_id = $(obj).attr('data-game_id'); jQuery.ajax({ url: 'follow.php', type: 'POST', data: { game_id : game_id }, success: function(data) { alert("You Followed!"); }, error: function () { alert("Something went wrong with Follow Button."); } }); }
这是我的follow.php
<?php require_once 'core/init.php'; // Set user_id to the currently logged in user. $user_id = $user_data['id']; // Set $game_id to the current ID of the game (coming from ajax call) $game_id = $_POST['game_id']; // Grab the game (ID) from the games table, then Query it. //$SQL_SELECT_GAME = "select * from games where id = '$game_id'"; //$db->query($SQL_SELECT_GAME); // Do an update on game_follow table, and set follow = 1 (means that this game is being followed) where the user_id = $user_id and game_id = $game_id, then Query it. $SQL_UPDATE = "update game_follows set follow = 1 where user_id = '$user_id' and game_id = '$game_id'"; $db->query($SQL_UPDATE);
我的桌子是上面的图片链接
绝对是一个广泛的问题,因此,广泛的答案:
我的建议是将一个Javascript侦听器附加到该按钮,该按钮将调用运行php脚本的ajax函数,执行mysql查询以相应地更新您的数据库。
对于html:
<button class="follow-game" data-game_id="1">Follow!</button
对于javascript:(jquery是我的偏爱,所以这是我的示例)
$(function(){
$('.follow-game').click(function(){
follow_game(this);
});
});
function follow_game(obj){
game_id = $(obj).attr('data-game_id');
$.ajax({
url: '/follow.php',
type: 'POST',
dataType: 'json',
data: {
game_id : game_id
},
success: function(rs){
alert("yay");
}
});
}
对于PHP / MYSQL:
$user_id = $_SESSION['user_id'];
$game_id = $_POST['game_id'];
$sql = "UPDATE `game_follows` SET follow = 1 WHERE user_id='{$user_id}' AND $game_id='{$game_id}'";
然后使用您正在使用的任何mysql连接/方法运行sql。 当然,这根本不考虑安全性,等等,但是一旦基本功能失效,就不必担心。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.