Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8616315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:35:20+00:00 2026-06-12T05:35:20+00:00

I have a database with a parent child relationship. I need to first insert

  • 0

I have a database with a parent child relationship. I need to first insert into the parent table then get the ID, that I would use to then insert data into the child table.

The following is snippets of the code I am using to insert into the database. This is my first time using PDO class. The errors I am getting; Undefined property: and Call to a member function lastInsertId() on a non-object database::$db. I must admit am I fairly new to PHP. Thank you


{

    public function __construct($DB_TYPE, $DB_HOST, $DB_NAME, $DB_USER, $DB_PASS)
    {
        parent::__construct($DB_TYPE.':host='.$DB_HOST.';dbname='.$DB_NAME, $DB_USER, $DB_PASS);
}

$this->db = new database(DB_TYPE, DB_HOST, DB_NAME, DB_USER, DB_PASS);

    $this->db->insert('images', array(
        'image_userID' => $data['image_userID'],
        'image_date' => $data['image_date']
    ));

/**
 * insert
 * @param string $table A name of table to insert into
 * @param string $data An associative array
 */
public function insert($table, $data)
{
    $fieldNames = implode('`, `', array_keys($data));
    $fieldValues = ':' . implode(', :', array_keys($data));

    $sth = $this->prepare("INSERT INTO $table (`$fieldNames`) VALUES ($fieldValues)");

    foreach ($data as $key => $value) {
        $sth->bindValue(":$key", $value);
    }

    $sth->execute();

$result= $this->db->lastInsertId();
 return $result;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T05:35:21+00:00Added an answer on June 12, 2026 at 5:35 am

    The last insert ID comes from the active statement, and not from the database handle. So change the last line of your code to this:

    $return = $sth->lastInsertId();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm really upset with Hibernate! I have a database table (mysql) that holds parent-child
I'm building a database table representing a 'note' that has a parent-child relationship in
I have a parent-child relationship in an Oracle 9i database-table like: parent | child
I have a parent/child database relationship between the table ACCOUNT and the table USER.
I have a database table that holds parent and child records much like a
I have a database table with parent child relationships between different rows. 1 parent
I know that in elasticsearch, we can have child/parent relationships between documents. And then,
I have an identifying relationship between two tables in the database. The parent table
I have two HTML pages that work in a parent-child relationship in this way:
I have a parent/child relationship via our users table, with models as such: class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.