For a accept or decline a friendshiprequest i'm updating the rows where the friendship_id is the same as the friendshiprequest. But when I click on accept OR decline it throws the error (Fatal error: Call to undefined function AcceptFriendRequest). But I defined the function? (i think). At the first click he throws the error. At the second click he throws the error and executes the sql. This is the sql syntax:
UPDATE friendship SET friendship_status = 'Accepted' WHERE friendship_id ='89'
These is my function.
// ACCEPT friend request
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;
}
This is what I'm printing:
<?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']);
}
?>
This is what I do when I click on the button:
if (isset($_POST["Accept"])) {
try {
$requestnumber = $_POST['acceptID'];
$friendship -> AcceptFriendRequest($requestnumber);
$feedback = "Awesome, You just added a friend!";
} catch(Exception $e) {
$feedback = $e -> getMessage();
}
}
I'm making some assumptions as I can't see your entire codebase.
A function called fetch_assoc() would typically be used to return an associative array of fields from a SELECT statement.
Because you're deleting and updating there is no real data to return.
I think updating your code to simple say:
return;
or
return true;
Will have the desired effect without breaking functionality.
fetch result
would be work with select query.
You should user Affected rows
function with UPDATE/DELETE
queries
$result->affected_rows(); //
And with INSERT
query
$result->insert_id(); // insert data id
With SELECT
queries
$result->fetch_assoc(); //query result return
$result->num_rows(); //number of rows return
You can only use fetch_assoc() if your are doing "SELECT". In your case you are updating / deleting records that is why you encountered the error. These queries are not returning a list of records deleted, but instead it returns an object resource for you to check if the query was done successfully or not.
So if you want to check if your query (update/delete) was successfull you can do:
if($result) {
die("success");
}else {
die("failed");
}
You may refer to this documentation on how to use fetch_assoc()