0

What i am trying to do is to add a user following system (twitter like) to my script.

This is how the db structure looks like:

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).

Also $subscribe_text should be Follow or Unfollow depending if the currently logged in user ($follower) is already following that user or not.

Can anybody help me please?

EDIT 2 (based on Mihir Singh's answer)

$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";
}
6
  • 2
    Before anything, obligatory bobby-tables.com Commented May 10, 2012 at 22:14
  • thank you. i am already know about mysql_real_escape_string and so on... Commented May 10, 2012 at 22:16
  • Sorry, that's my instinctive answer when I see something like mysql_query(" DELETE FROM `user_follow` WHERE `follower` = '$follower' AND `following` = '$following'; "); Commented May 10, 2012 at 22:18
  • 2
    Okay. Now what exactly is the question? How to find out what $subscribe_text is, or...? I really can't tell. It seems you have all you need. Commented May 10, 2012 at 22:35
  • 1
    Please don't use mysql_* functions in new code. They were removed from PHP 7.0.0 in 2015. Instead, use prepared statements via PDO or MySQLi. See Why shouldn't I use mysql_* functions in PHP? for more information. Commented May 10, 2012 at 23:01

1 Answer 1

2

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?

Sign up to request clarification or add additional context in comments.

4 Comments

thank you, i used this code but how should i add the query to update (follow or unfollow) to the code?
@m3tsys I'll append it in a moment.
@m3tsys the new code has been appended. Tell me what you think.
thank you very much for your time. i had to modify the code like following because if i used your example at a simple refresh of the page a unfollow was made if ($_POST['action'] == "follow"){/* follow query */} elseif ($_POST['action'] == "unfollow"){/* unfollow query */} i basically added a else if condition. thank you again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.