1

I have a problem with my PHP class, when the user wants to follow another user the follow method is called and when the user wants to stop following the delete_follow is called:

class Follow {

    protected static $table_name = "interests";


    public function follow() {
        global $dbh;
        $sql = "INSERT INTO ".self::$table_name." (company_id,user_id,likedate) VALUES (:company_id,:user_id,NOW())";
        $follow = $dbh->prepare($sql);
        $follow->bindParam(':user_id',$_SESSION['user_id']);
        $follow->bindParam(':company_id',$_GET['company']);
        if($follow->execute() == true){
            header("Location: profile.php?company=".$_GET['company']."");
            exit;
        } else {
            header("Location: error.php");
            exit;
        }
    }

    public function delete_follow() {
        global $dbh;
        $sql = "DELETE FROM ".self::$table_name." WHERE company_id = :company_id AND user_id = :user_id LIMIT 1";
        $delete_follow = $dbh->prepare($sql);
        $delete_follow->bindParam(':user_id',$_SESSION['user_id']);
        $delete_follow->bindParam(':company_id',$_GET['company']);
        if($delete_follow->execute() == true) {
            header("Location: profile.php?company=".$_GET['company']."");
            exit;
        } else {
            header("Location: error.php");
            exit;
        }
    }   


}

My problem is that when the delete_follow method is called it actually calls the follow method I have no idea what is going on.

Here is the code for the follow buttons:

if(isset($_POST['follow'])) {
    $follows = new Follow();
    $follows->follow();
}

if(isset($_POST['delete_follow'])) {
    $follows = new Follow();
    $follows->delete_follow();
}

Help please.

0

2 Answers 2

4

The name of your class is Follow. The first method in your class is called follow(). PHP is case insensitive in this aspect and treats that follow() method as the constructor. So this statement--$follows = new Follow()--actually calls the follow() method from your class. Therein could lie your problem.

Read more about PHP constructors here.

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

3 Comments

No. The constructor is __construct()
@ColeJohnson No, "For backwards compatibility, if PHP 5 cannot find a __construct() function for a given class, it will search for the old-style constructor function, by the name of the class." php.net/manual/en/language.oop5.decon.php
@Cole Johnson: In PHP, constructors can be both the new __construct() method or the name of the class itself. Look here, example 2: php.net/manual/en/language.oop5.decon.php
0

I would imagine that there is an error in your form. Perhaps it would be better to have one field follow with a boolean value, say yes or no.

1 Comment

It's not the form, becasue before I had the code in a normal function and it worked fine, when I moved it to a class the problem occured

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.