gpt4 book ai didi

PHP 帮助 : Follow System in Microblogging

转载 作者:行者123 更新时间:2023-11-28 08:22:24 24 4
gpt4 key购买 nike

我正在制作一个类似于 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>

最佳答案

在您要显示“关注”或“取消关注”按钮的页面上,首先运行 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。 .

关于PHP 帮助 : Follow System in Microblogging,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28699572/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com