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