PHP求助:微博关注系统
PHP help: Follow System in Microblogging
我正在制作一个类似于 Instagram 的网站。我对 php 很陌生。我在用户的个人资料中创建了一个关注按钮。
如何在已经关注用户后让关注按钮消失?
如何用取消关注按钮替换它?
// 我的 php 关注代码
if (isset($_POST['addfriend'])){
$fromuser = $user;
$touser = $username;
if($fromuser == $username){
$Msg = "You cannot follow yourself<br/>";
}
else
{
$getID= mysql_query("SELECT userID FROM user WHERE username='$user'");
$get_ID_row = mysql_fetch_assoc($getID);
$ID_db = $get_ID_row['userID'];
$sql = "insert into following (userID, fromUser, toUser)
values ('$ID_db','$fromuser', '$touser')";
$result = mysql_query($sql);
$Msg= "Success! <br/>";
}
}
else{
//Do nothing
}
//我的关注按钮代码
<form action="<?php $user;?>" method ="POST">
<?php echo $Msg; ?>
<input type = "submit" name ="addfriend" value = "Follow"/>
</form>
使用 OO PHP 会更容易。但是,如果您选择程序,假设我们有 table 个朋友。它保留了我每个朋友的 ID。
例如:史密斯跟随约翰
然后你做类似的事情
$following = mysql_query("SELECT COUNT(*) FROM followers WHERE followerid = ".$_SESSION['id']." AND followeeid = ".$username);
检查您是否已经关注此人:
if($following){//$following == true
}
在您要显示“关注”或“取消关注”按钮的页面上,首先 运行 一个 MySQL 查询以查明您是否已经关注此人:
$sql = "select * from following
where userID = $user
and fromUser = $fromUser
and toUser = $toUser";
$result = mysql_query($sql);
if( $result) {
if( mysql_num_rows($result) > 0) {
// if we get here we know we are already following that person
....[see below]
现在动态创建您需要的按钮:-
if( mysql_num_rows($result) > 0) {
// if we get here we know we are already following that person
echo '<input type = "submit" name ="removefriend" value = "Un-follow"/>';
}
else
{
echo '<input type = "submit" name ="addfriend" value = "Follow"/>';
}
在您获取表单结果的下一页上,检查两个按钮:
if (isset($_POST['addfriend'])) {
...[do what you already have]
}
else
if (isset($_POST['removefriend'])) {
...[do SQL to remove the record from the following table]
}
另请注意,从 PHP v5.5 开始,这种 MySQL 的风格已被弃用。在未来的某个阶段,您必须将您的程序转换为 MySQLi 或 PDO_MySQL 扩展,直到它们最终停止支持。请参阅 PHP 手册,例如 http://php.net/manual/en/mysqlinfo.api.choosing.php。
我正在制作一个类似于 Instagram 的网站。我对 php 很陌生。我在用户的个人资料中创建了一个关注按钮。
如何在已经关注用户后让关注按钮消失? 如何用取消关注按钮替换它?
// 我的 php 关注代码
if (isset($_POST['addfriend'])){
$fromuser = $user;
$touser = $username;
if($fromuser == $username){
$Msg = "You cannot follow yourself<br/>";
}
else
{
$getID= mysql_query("SELECT userID FROM user WHERE username='$user'");
$get_ID_row = mysql_fetch_assoc($getID);
$ID_db = $get_ID_row['userID'];
$sql = "insert into following (userID, fromUser, toUser)
values ('$ID_db','$fromuser', '$touser')";
$result = mysql_query($sql);
$Msg= "Success! <br/>";
}
}
else{
//Do nothing
}
//我的关注按钮代码
<form action="<?php $user;?>" method ="POST">
<?php echo $Msg; ?>
<input type = "submit" name ="addfriend" value = "Follow"/>
</form>
使用 OO PHP 会更容易。但是,如果您选择程序,假设我们有 table 个朋友。它保留了我每个朋友的 ID。 例如:史密斯跟随约翰
然后你做类似的事情
$following = mysql_query("SELECT COUNT(*) FROM followers WHERE followerid = ".$_SESSION['id']." AND followeeid = ".$username);
检查您是否已经关注此人:
if($following){//$following == true
}
在您要显示“关注”或“取消关注”按钮的页面上,首先 运行 一个 MySQL 查询以查明您是否已经关注此人:
$sql = "select * from following
where userID = $user
and fromUser = $fromUser
and toUser = $toUser";
$result = mysql_query($sql);
if( $result) {
if( mysql_num_rows($result) > 0) {
// if we get here we know we are already following that person
....[see below]
现在动态创建您需要的按钮:-
if( mysql_num_rows($result) > 0) {
// if we get here we know we are already following that person
echo '<input type = "submit" name ="removefriend" value = "Un-follow"/>';
}
else
{
echo '<input type = "submit" name ="addfriend" value = "Follow"/>';
}
在您获取表单结果的下一页上,检查两个按钮:
if (isset($_POST['addfriend'])) {
...[do what you already have]
}
else
if (isset($_POST['removefriend'])) {
...[do SQL to remove the record from the following table]
}
另请注意,从 PHP v5.5 开始,这种 MySQL 的风格已被弃用。在未来的某个阶段,您必须将您的程序转换为 MySQLi 或 PDO_MySQL 扩展,直到它们最终停止支持。请参阅 PHP 手册,例如 http://php.net/manual/en/mysqlinfo.api.choosing.php。