Insert data from class into database using PHP

I don't understand this at all.

I have a class that looks like 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 end up with:

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

      

Ultimately with Notice : Undefined property: pageData :: $ getBookmark in ...

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

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

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

If I type

print($data->getBookmark());

      

prints the bookmark. if i print

echo $data->getBookmark();

      

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

+3


source to share


2 answers


When using anything other than a normal variable in a string, you must add curly braces around the values:



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

      

+1


source


It is interpreted as a data item

Variable $data->getBookmark

followed by()



Do

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

      

+1


source







All Articles