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

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

No sir if i do that it sends Call to undefined function mysql_real_eacape_string()

Member Avatar for LastMitch

@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?

 $obj->add_content($_POST['add']);

use this may be help

'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.

nope ill just find the video and re make the whle thing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.