1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Call To A Member Function Fetch_object() On A Non-object

Discussion in 'PHP' started by KevinK, Jan 21, 2013.

  1. #1
    Good evening all.
    I have written a little "program" that grabs news items and adds them to a MySQL database. Everything is working well. Now I am working on adding functionality that will allow me to add a comment to each item I save - GetEmAll_Comment.php.
    My code pulls the existing comments without a problem, but when I submit a new comment I get a strange error message. It is a bit strange because the UPDATE works... but I still get a that strange error. Any ideas?

    This is the error message:

    ( ! ) Fatal error: Call to a member function fetch_object() on a non-object in C:\wamp\www\NewsToolv1\GetEmAll_Comment.php on line 39



    GetEmAll_Comment.php:

    <?php
     
    $id = filter_input(INPUT_POST, 'id', FILTER_SANITIZE_STRING);
    $comment = filter_input(INPUT_POST, 'comment', FILTER_SANITIZE_STRING);
     
    $existing_comment = '';
     
    // Check if the comment was set (posted)
    if(isset($comment)){
        addComment($id, $comment);
        echo "record should be added";
    } elseif (empty($comment)) {
        $existing_comment = checkForComment($id);
    }
     
    // Update the database with the new comment
    function addComment($id, $comment){
       
        $db = new mysqli('localhost', 'root', '', 'newstoolv1');
        $sql = "UPDATE tbl_news SET clmn_comment='".$comment."' WHERE id=".$id;
        $db->query($sql);
        $db->close();   
       
        // Go back to page
        $callingPage = $_SERVER['HTTP_REFERER'];
        header('location:' . $callingPage);
     
    }
     
    function checkForComment($id){
       
        $db = new mysqli('localhost', 'root', '', 'newstoolv1');
        $sql = "SELECT * FROM tbl_news WHERE id=".$id;
        $results = $db->query($sql);
        $db->close();
       
        $r = '';
       
        while($output_item = $results->fetch_object()){
            $r = $output_item->clmn_comment;
        }
       
        return $r;
       
    }
     
     
    ?>
     
    <form class="form" method="POST" action="GetEmAll_Comment.php">
    <input type="hidden" name="id" value="<?PHP echo $id;?>">
    <textarea rows="1" cols="60" name="comment"><?PHP echo $existing_comment;?></textarea>
    <input type="submit" value="Submit">
    </form>
    PHP:
     
    KevinK, Jan 21, 2013 IP
  2. phpexperts

    phpexperts Greenhorn

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    21
    #2
    I think you wrote a wrong statement on line no. 39

    It should be
    while($output_item = $db->fetch_object()){
     
            $r = $output_item->clmn_comment;
     
        }
    PHP:
     
    phpexperts, Jan 21, 2013 IP
  3. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #3
    At line 34, try
    $result = $mysqli->query($sql) {
    PHP:
     
    Rukbat, Jan 22, 2013 IP
  4. KevinK

    KevinK Well-Known Member

    Messages:
    101
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    135
    #4
    Thank you for the help.

    Interesting, it seems as though the form post doesn't work, or the POST values are not captured. They show up as empty, in which case the SQL query can't run correctly.

    I've tried to separate the form code a bit.

    <?php
     
    $id = filter_input(INPUT_POST, 'id', FILTER_SANITIZE_STRING); //  Gets passed in from GetEmAll.php via form POST
     
    // Gets passed form this file "GetEmAll_Comment.php" via form POST
    $id_form = filter_input(INPUT_POST, 'id_form', FILTER_SANITIZE_STRING);
    $comment_form = filter_input(INPUT_POST, 'comment_form', FILTER_SANITIZE_STRING);
     
     
    echo "id: ".$id."<br/>";
    // echo "comment: ".$comment."<br/>";
    echo "id_form: ".$id_form."<br/>";
    echo "comment_form: ".$comment_form."<br/>";
     
     
     
    // Check if the comment was set (posted)
    if(isset($comment_form) && isset($id_form)){
       
        // if both $comment_form and $id_form are set it must be submitted from the form
        // if it is submitted from the form, update the database record
        addComment($id_form, $comment_form);
        echo "added: ".$comment_form." to ".$id."<br />";
       
    } else {
        $existing_comment = checkForComment($id);
    }
     
    // Update the database with the new comment
    function addComment($id, $comment){
       
        $db = new mysqli('localhost', 'root', '', 'newstoolv1');
        $sql = "UPDATE tbl_news SET clmn_comment='".$comment."' WHERE id=".$id;
        $db->query($sql);
        $db->close();   
     
        // Go back to page
        $callingPage = $_SERVER['HTTP_REFERER'];
        header('location:' . $callingPage);
     
    }
     
    function checkForComment($id){
       
        echo "checkForComment() Function<br/>";
        echo "id: ".$id."<br/>";
       
        $db = new mysqli('localhost', 'root', '', 'newstoolv1');
        $sql = "SELECT * FROM tbl_news WHERE id=".$id;
        $results = $db->query($sql);
        $db->close();
       
        echo "The SQL statement:<br/>";
        echo "sql var: ".$sql."<br/>";
       
        var_dump($results);
       
       
        $r = '';
       
        while($output_item = $results->fetch_object()){
            // $r = $output_item->clmn_comment;
            echo "Output from DB".$output_item->clmn_comment."<br/>";
        }
       
        /*
       
        return $r;
        */
    }
     
     
    ?>
     
    <form class="form" method="POST" action="GetEmAll_Comment.php">
    <input type="hidden" name="id_form" value="<?PHP echo $id;?>">
    <textarea rows="1" cols="60" name="comment_form"><?PHP echo $existing_comment;?></textarea>
    <input type="submit" value="Submit">
    </form>
    PHP:
     
    KevinK, Jan 22, 2013 IP
  5. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #5
    Try this:

    <?php
    if(strlen($_POST['id_form'] == 0) {
     
    echo '<form class="form" method="POST" action="GetEmAll_Comment.php">
    <input type="hidden" name="id_form" >
    <textarea rows="1" cols="60" name="comment_form" />
    <input type="submit" value="Submit">
    </form>';
    exit;
    }
     
    $id = filter_input(INPUT_POST, 'id', FILTER_SANITIZE_STRING); //  Gets passed in from GetEmAll.php via form POST
     
    // Gets passed form this file "GetEmAll_Comment.php" via form POST
    $id_form = filter_input(INPUT_POST, 'id_form', FILTER_SANITIZE_STRING);
    $comment_form = filter_input(INPUT_POST, 'comment_form', FILTER_SANITIZE_STRING);
     
    echo "id: ".$id."<br/>";
    // echo "comment: ".$comment."<br/>";
    echo "id_form: ".$id_form."<br/>";
    echo "comment_form: ".$comment_form."<br/>";
     
    // Check if the comment was set (posted)
    if(isset($comment_form) && isset($id_form)){
     
        // if both $comment_form and $id_form are set it must be submitted from the form
        // if it is submitted from the form, update the database record
        addComment($id_form, $comment_form);
        echo "added: ".$comment_form." to ".$id."<br />";
     
    } else {
        $existing_comment = checkForComment($id);
    }
     
    // Update the database with the new comment
    function addComment($id, $comment){
     
        $db = new mysqli('localhost', 'root', '', 'newstoolv1');
        $sql = "UPDATE tbl_news SET clmn_comment='".$comment."' WHERE id=".$id;
        $db->query($sql);
        $db->close();
     
        // Go back to page
        $callingPage = $_SERVER['HTTP_REFERER'];
        header('location:' . $callingPage);
     
    }
     
    function checkForComment($id){
        echo "checkForComment() Function<br/>";
        echo "id: ".$id."<br/>";
     
        $db = new mysqli('localhost', 'root', '', 'newstoolv1');
        $sql = "SELECT * FROM tbl_news WHERE id=".$id;
        $results = $db->query($sql);
        $db->close();
     
        echo "The SQL statement:<br/>";
        echo "sql var: ".$sql."<br/>";
     
        var_dump($results);
     
        $r = '';
     
        while($output_item = $results->fetch_object()){
            // $r = $output_item->clmn_comment;
            echo "Output from DB".$output_item->clmn_comment."<br/>";
        }
     
        /*
     
        return $r;
        */
    }
     
    ?>
    
    PHP:
    The way you did it, the PHP code ran, then created the form and sent it to the user. There was nothing submitted yet, so the code was running with no POST values. (I didn't check the rest of the code, just the fact that you were running the PHP code by connecting to the site, not by submitting the form.) BTW, you can't fill the values into the form before you submit the form.

    (You do understand how client-server works, right? When you connect to the site, the server-side code runs. The client-side code doesn't exist until the server-side code sends the client-side code [like the form] to the client. You can't just mix them together - they're not running at the same time, or in the same place.)
     
    Rukbat, Jan 23, 2013 IP
  6. KevinK

    KevinK Well-Known Member

    Messages:
    101
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    135
    #6
    Thank you, you are on-to something.

    When the page loads, it has 1 value to start with, which is passed in from GetEmAll.php, it is the ID of the news item. If the "id" is set, I want to see if there is already a comment to that news item i the database, if there is, I want to pull it out and populate the text area (checkForComment($id)).

    The problem occurs when checkForComment($id) is executed after the form was submitted, there is no $id_form value set, for some reason.

    If I remove

    // Go back to page
    $callingPage = $_SERVER['HTTP_REFERER'];
    header('location:' . $callingPage);
    PHP:
    Then it works great, it just doesn't go back to the GetEmAll.php page. But I solved that by passing through the referring page :)
     
    KevinK, Jan 24, 2013 IP
  7. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #7
    Check the value of $id right at the top of the file (after line 3 in your first post). If it's blank there after submitting the form, a value for $id isn't being POSTed by the calling file (or the value is blank).
     
    Rukbat, Jan 24, 2013 IP