My friend and I have created a Poll voting site which allows a user to create a profile, add a friend(another user on the site) and create polls, vote on them and delete friends. We’re having an error on our friend page when we click the x button beside a friend it should execute the delete_friend.php page code, but instead we’re getting the following error:
Notice: Undefined variable: link in E:\Apache\N00090377\UVote\delete_friend.php on line 21 Fatal error: Call to a member function prepare() on a non-object in E:\Apache\N00090377\UVote\delete_friend.php on line 21
Here is the code for the delete_friend.php page anyone able to offer their expertise, we’re not the greatest of coders and would appreciate some help thanks for reading.
<?php
// if this page was requested using a GET request then delete the friend
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
// if friend id is not empty then try to delete the friend
if (!empty($_GET['memberId2'])) {
// read friend id from request
$memberId2 = $_GET['memberId2'];
// define DB connection data and connect to database
require_once 'db_connect.php';
// query string to execute including placeholder '?' for friend id
$sql = "DELETE FROM friendlist WHERE memberId2 = ?";
// friend id to be inserted into placeholder
$params = array($memberId2);
// prepare and execute the query using parameters
$stmt = $link->prepare($sql);
$status = $stmt->execute($params);
// if update executed ok then redirect the user to the view_friends page;
// redirection is used to prevent the request being accidently
// resubmitted if the response page is reloaded by the user
if ($status == true) {
header("Location: friends.php");
}
// else if update did not execute ok then send the user an error message
else {
$error_info = $stmt->errorInfo();
$error_message = "failed to delete friend: {$error_info[2]} - error code {$error_info[0]}";
require 'error.php';
}
}
// else if friend id is empty then send the user back to the view friends page
// with an error message
else {
$error_message = "friend id not specified";
require 'friends.php';
}
}
// if this page was not requested using a GET request then ignore it
else {
}
?>