SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    May 2012
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Notice: Undefined variable: link Error

    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.
    Code:
    <?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 {
    }
    ?>

  2. #2
    SitePoint Enthusiast
    Join Date
    Mar 2011
    Posts
    70
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    From the error, it looks as though $link is not a PDO instance. Is it declared in another file?

  3. #3
    SitePoint Member
    Join Date
    May 2012
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help

    Sorted out the error by adding in this piece of code just before the if statement and it's deleting the friends from the list now:

    Code:
       require_once 'db_connect.php';
            $dbinfo = "mysql:host=daneel:3306;dbname=N00090291";
            $user = "N00090291";
            $pass = "N00090291";
            $link = new PDO($dbinfo, $user, $pass);
            $link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

  4. #4
    SitePoint Enthusiast
    Join Date
    Mar 2011
    Posts
    70
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Your welcome, feel free to come back if you have any more questions.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •