我想要做的是添加一個用戶跟隨系統(微博喜歡)我的腳本。如何在php腳本中創建用戶?
這是數據庫結構的樣子:
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 ;
如果我要創建我要使用此代碼:
mysql_query(" INSERT INTO `user_follow` (`follower`, `following`, `subscribed`) VALUES ('$follower', '$following', CURRENT_TIMESTAMP); ");
對於取消它應該是這樣的:
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
在個人資料頁,我有這樣的形式:
<?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 } ?>
我不知道的是如何將所有這些代碼...
編輯:
$subscribe_status
應該更改爲follow
或unfollow
,具體取決於用戶是否已經關注該用戶(通過檢查我認爲的查詢)。
還$subscribe_text
應該是Follow
或Unfollow
取決於當前登錄的用戶($follower
)是否已經跟隨該用戶。
有人可以幫我嗎?
EDIT 2(基於米希爾·辛格的答案)
$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";
}
之前,強制性http://bobby-tables.com/ – Amadan
謝謝。我已經知道mysql_real_escape_string等等...... – m3tsys
對不起,這是我本能的答案,當我看到'mysql_query(「DELETE FROM \'user_follow \'WHERE \'follower \'='$ follower'AND \'跟隨\'='$ follow';「);' – Amadan