Jump to content

Saving into a custom table

Recommended Posts

I was trying the $database variable just for the purpose of figure it out. I have a custom table placed in a PW database and try to populate it with some data. I use this code. I don't want to use fields and page creation for this feature:

public function saveMessage(array $data): int
        $database = wire('database');
        $statement = "INSERT INTO pm (pageId, title, ownerId, correspondentId,message,thetime,ownerRead,correspondentRead)" .
        " VALUES (:pageId, :title, :ownerId, :correspondentId,:message,:thetime,:ownerRead,:correspondentRead)";
        $query = $database->prepare($statement);
        $query->bindValue(':pageId', $data[0], \PDO::PARAM_INT);
        $query->bindValue(':title', $data[1], \PDO::PARAM_STR);
        $query->bindValue(':ownerId', $data[2], \PDO::PARAM_INT);
        $query->bindValue(':correspondentId', $data[3], \PDO::PARAM_INT);
        $query->bindValue(':message', $data[4], \PDO::PARAM_INT);
        $query->bindValue(':thetime', $data[5], \PDO::PARAM_INT);
        $query->bindValue(':ownerRead', $data[6], \PDO::PARAM_BOOL);
        $query->bindValue(':correspondentRead', $data[5], \PDO::PARAM_BOOL);

        try {
            $result = $query->rowCount();
        } catch (\Exception $e) {
            $result = 0;
        return $result;

If I dump the $query statement after binding the values, I get this, which shows that the values are not bound.

object(PDOStatement)#279 (1) {
  string(201) "INSERT INTO pm (pageId, title, ownerId, correspondentId,message,thetime,ownerRead,correspondentRead) VALUES (:pageId, :title, :ownerId, :correspondentId,:message,:thetime,:ownerRead,:correspondentRead)"


I use the 3.0.153 dev branch and my function is placed in a page Class (wonderful feature!)


Of course, nothing is saved in the database. What am I missing?

Share this post

Link to post
Share on other sites

My bad... everything is fine. A table name was not correctly written ...🥵

  • Like 1

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...