php - Undefined function is working at second click (with an error)
Get the solution ↓↓↓I'm trying to add someone when they get a friendshiprequest. I created a function to accept or decline that person. But when I klik on fe 'accept' he does nothing, the second time he executes the sql function en sets the friendshipstatus from 'pending' to 'accepted' BUT I always get the following error:
Fatal error: Call to undefined function AcceptFriendRequest() in C:\wamp\www\Stimulus\friendrequests.php on line 82
SQL
UPDATE friendship SET friendship_status = 'Accepted' WHERE friendship_id ='101'
FUNCTION (in the class friendship)
public function AcceptFriendRequest($requestnumber){
$db = new Db();
$select = "UPDATE friendship SET friendship_status = 'Accepted' WHERE friendship_id ='" . $requestnumber . "'";
echo $select;
$result = $db->conn->query($select);
return $result;}
PHP
$user = new User();
$friendship = new Friendship();
$user_id = $_GET['user_id'];
$email = $_SESSION["email"];
$numberoffriendrequests = $friendship->GetTotalOfFriendRequests($user_id);
$friendrequests=$friendship->GetAllFriendRequests($email);
if (isset($_POST["Accept"])) {
try {
$requestnumber = mysql_real_escape_string($_POST['acceptID']);
$friendship -> AcceptFriendRequest($requestnumber);
$feedback = "Awesome, You just added a friend!";
} catch(Exception $e) {
$feedback = $e -> getMessage();
}
}
if (isset($_POST["Decline"])) {
try {
$requestnumber = mysql_real_escape_string($_POST['declineID']);
$friendship -> DeleteFriendRequest($requestnumber);
$feedback = "You just deleted a friend!";
} catch(Exception $e) {
$feedback = $e -> getMessage();
}
}
MY PRINT OUT, my error has to be on this line:$accepted = AcceptFriendRequest($_POST['acceptid']);
<?php
foreach ($friendrequests as $request) {
echo "
<div><p>
<a href='profile.php?user_id=".$request['friendship_applicant_id'] . "'>
<img src='uploads/" . $request['friendship_applicant_avatar'] . " " . " ' alt='' />" . $request['friendship_applicant_surname'] . $request['friendship_id'] . " " . $request['friendship_applicant_name'] . "
</a> has send you a friend request" . "
<form action='" . $_SERVER['REQUEST_URI'] . "' method='post'>
<button type='submit' name='Accept'>Accept</button>
<button type='submit' name='Decline'>Decline</button>
<input type='hidden' name='acceptID' value='".$request['friendship_id']."' />
<input type='hidden' name='declineID' value='".$request['friendship_id']."' />
</form>
</p></div>";
}//foreach
if (isset($_POST['Accept'])){
$accepted = AcceptFriendRequest($_POST['acceptid']);
//echo $accepted;
}
if (isset($_POST['Decline'])){
$decline = DeclineFriendRequest($_POST['declineid']);
}
Answer
Solution:
You have an error here-
if (isset($_POST['Accept'])){
$accepted = AcceptFriendRequest($_POST['acceptid']);
//echo $accepted;
}
Its outside the class and only an object of that class can make a call to that method! Until ofcoasre if you define the method static, after which you can call it by scope resolution operator!
You can Either use an object to access that method or just define you method static and access by-
classname::functionname();
If you like to call the method by object-
if (isset($_POST['Accept'])){
$accepted = $friendship->AcceptFriendRequest($_POST['acceptid']);
//echo $accepted;
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: xml parsing error: no root element found
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.