Drew
Drew

Reputation: 204

insert data from a class into a database using PHP

I don't understand this at all.

I have a class which is roughly the following:

<?php
class pageData
{
    private $Bookmark;
    private $Program;
    private $Agency;

    //With appropriate setters/getters
}
?>

Then I try to create a new object, pass it around a bit, and eventually end up with:

mysql_query("INSERT INTO Records (Bookmark, Program, Agency)
VALUES ('$data->getBookmark()', '$data->getProgram()', '$data->getAgency()')");

I end up with Notice: Undefined property: pageData::$getBookmark in...

Notice: Undefined property: pageData::$getProgram in...

Notice: Undefined property: pageData::$getAgency in...

Using PhpMyAdmin it looks like Bookmark becomes 0 and Program becomes () and Agency is empty.

If I type

print($data->getBookmark());

it prints out the bookmark. if I type

echo $data->getBookmark();

it prints out. Why doesn't it work when I try to insert it into the database, too?

Upvotes: 3

Views: 2644

Answers (2)

Spontifixus
Spontifixus

Reputation: 6660

When using anything other than a normal variable in a string you should add curly brackets around the values:

mysql_query("INSERT INTO Records (Bookmark, Program, Agency)
             VALUES ('{$data->getBookmark()}', '{$data->getProgram()}', '{$data->getAgency()}')");

Upvotes: 1

Anirudh Ramanathan
Anirudh Ramanathan

Reputation: 46728

It is getting interpreted as a data-member

The variable $data->getBookmark followed by ()

Do

mysql_query("INSERT INTO Records (Bookmark, Program, Agency)
VALUES ('".$data->getBookmark()."',...

Upvotes: 1

Related Questions