简体   繁体   English

如何在php脚本后创建用户?

[英]How to create a user following php script?

What i am trying to do is to add a user following system (twitter like) to my script. 我想要做的是在我的脚本中添加一个用户跟随系统(twitter like)。

This is how the db structure looks like: 这就是db结构的样子:

CREATE TABLE IF NOT EXISTS `user_follow` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `follower` int(11) NOT NULL,
  `following` int(11) NOT NULL,
  `subscribed` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `follow_unique` (`follower`,`following`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 ;

If i want create i should use this code: 如果我想创建我应该使用此代码:

mysql_query(" INSERT INTO `user_follow` (`follower`, `following`, `subscribed`) VALUES ('$follower', '$following', CURRENT_TIMESTAMP); ");

For the unsubscribe it should look like this: 对于取消订阅,它应该如下所示:

mysql_query(" DELETE FROM `user_follow` WHERE `follower` = '$follower' AND `following` = '$following'; ");

.

$follower = $_SESSION[user_id]; // the user_id for the one who is currently logged id 
$following = $user_id; // the user_id for the user profile where the script will be on

On the profile page i have a form like this: 在个人资料页面上,我有一个这样的表单:

<?php if (isset($_SESSION[user_id]) && $_SESSION[user_id] != $user_id) { ?>
<form action="" method="post">
<input name="action" type="hidden" value="<?php echo $subscribe_status; ?>"/>
<button name="subject" type="submit" value="<?php echo $subscribe_text.' '.$username; ?>"><?php echo $subscribe_text.' '.$username; ?></button>
</form>
<?php } ?>

What i don't know is how to link all of those codes... 我不知道的是如何链接所有这些代码......

EDIT: 编辑:

$subscribe_status should change to follow or unfollow depending if the user is already following that user or not (by checking with a query i think). $subscribe_status应该更改为followunfollow具体取决于用户是否已经关注该用户(通过检查我认为的查询)。

Also $subscribe_text should be Follow or Unfollow depending if the currently logged in user ( $follower ) is already following that user or not. 另外, $subscribe_text应该是FollowUnfollow具体取决于当前登录的用户( $follower )是否已经跟随该用户。

Can anybody help me please? 有人能帮帮我吗?

EDIT 2 (based on Mihir Singh's answer) 编辑2(基于Mihir Singh的回答)

$user_follow = dbquery(" SELECT * FROM `user_follow` WHERE `follower` = '$follower' AND `following` = '$following'; ");
$check_status = dbrows($user_follow);

$sub = false; //Boolean var which states if subscribed or not

if ( $check_status !== 0 ){ //Pseudo code
    $sub = true; //If row is found, they are subscribed, so set $sub to true
}

if($sub){
     $subscribe_status = "follow";
     $subscribe_text = "Follow";
}

else{
     $subscribe_status = "unfollow";
     $subscribe_text = "Unfollow";
}

Here's some code: 这是一些代码:

$sub = false; //Boolean var which states if subscribed or not

if (row in table exists --> subscribed ){ //Pseudo code
    $sub = true; //If row is found, they are subscribed, so set $sub to true

if($sub){
     $subscribe_status = "Follow";
     $subscribe_text = "Unfollow";
}

else{
     $subscribe_status = "Unfollow";
     $subscribe_text = "Follow";
}

EDIT: 编辑:

So, this is the form code that you're using: 所以,这是您正在使用的表单代码:

<?php if (isset($_SESSION[user_id]) && $_SESSION[user_id] != $user_id) { ?>
<form action="" method="post">
<input name="action" type="hidden" value="<?php echo $subscribe_status; ?>"/>
<button name="subject" type="submit" value="<?php echo $subscribe_text.' '.$username; ?>">       
<?php echo $subscribe_text.' '.$username; ?></button>
</form>
<?php } ?>

Based on that, I would change 基于此,我会改变

<form action="" method="post">

to

<form action="handler.php" method="post">

and then create a handler file with this: 然后用这个创建一个处理程序文件:

<?
    if ($_POST['action'] == "Follow")
        //Perform Unfollow Query to Database
    else
        //Perform Follow Query to Database
?>

That's barebones.... but it should work out. 这是准系统......但它应该成功。 How's that? 怎么样?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM