2012-05-10 54 views
0

我想要做的是添加一個用戶跟隨系統(微博喜歡)我的腳本。如何在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應該更改爲followunfollow,具體取決於用戶是否已經關注該用戶(通過檢查我認爲的查詢)。

$subscribe_text應該是FollowUnfollow取決於當前登錄的用戶($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"; 
} 
+2

之前,強制性http://bobby-tables.com/ – Amadan

+0

謝謝。我已經知道mysql_real_escape_string等等...... – m3tsys

+0

對不起,這是我本能的答案,當我看到'mysql_query(「DELETE FROM \'user_follow \'WHERE \'follower \'='$ follower'AND \'跟隨\'='$ follow';「);' – Amadan

回答

1

下面是一些代碼:

$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"; 
} 

編輯:

所以,這是您所使用的表單代碼:

<?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 } ?> 

基於這一點,我會改變

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

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

,然後創建一個處理程序文件與此:

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

這是準系統....但它應該工作了。怎麼樣?

+0

謝謝你,我用這個代碼,但我應該如何添加查詢更新(遵循或取消關注)的代碼? – m3tsys

+0

@ m3tsys我會在一會兒追加它。 – citruspi

+0

@ m3tsys新代碼已被追加。告訴我你的想法。 – citruspi