0

Fatal error: Call to a member function add_content() on a non-object

here is my index:

<?php
include ('lib/connect.php');

include('lib/nav.php');

if($_POST['add']):
  $obj->add_content($_POST);
endif
?>

Here is my function:

<?php
    function add_content($p) {
        $title = mysql_real_eacape_string($p['title']);
       $body = mysql_real_eacape_string($p['body']);

       if(!$title || !$body):

            if(!$title):
                echo"The Title is required";
            endif;
            if(!body):
                echo"The Body is required";
            endif;

             echo"<a href='add_content.php'>Try again</a>";
       else:
            $sql = "INSERT INTO content VALUES(null, '$title', '$body')";
            $res = mysql_query($sql) or die (mysql_error());
            echo"Added Successfuly!";
       endif;
    }
?>

here is my add_content.php:

<?php
include ('lib/connect.php')

?>
<html>
    <head>

    </head>
    <body>
        <form method="post" action="index.php">
            <input type="hidden" name="add" value="true" />
                <div>
                    <label for="title">Title: </label>
                    <input type="text" name="title" id="title" />
                </div>
                <div>
                    <label for="title">Title: </label>
                    <textarea name="body" id="body" rows="8" cols="40"></textarea>
                </div>
            <input type="submit" name="submit" value="Add Content" />
        </form>
    </body>
</html>

can some please give me some information of what i am doing wrong to keep getting this error

5
Contributors
6
Replies
10
Views
4 Years
Discussion Span
Last Post by daniel.conlinjr.1
1

seeing from you code
maybe in this line

 $obj->add_content($_POST);

remove $obj-> because your not dealing on an object you just declare a function

0

@daniel.conlinjr.1

can some please give me some information of what i am doing wrong to keep getting this error

function add_content($p)

What is $p? Another words what is in $p, what does it represent?

1

'On a non-object' means that you haven't instantiated a class and therefore no object has been created.

Looking at your code though the function you are trying to use is just a function, it is not within a class and therefore you cannot use -> to access the function (that is for classes / objects only).

To access the function, you just need to use add_content($p);

If you are getting the error Call to undefined function mysql_real_eacape_string() It is because you cannot use that function until you have made a connection to your database so check that you connection has been made before running the function.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.