1

I create a Drupal module which install database tables during module installation, table is created using Drupal way. The tables are created perfectly and now i have to insert data into those tables.

Example table structure where i want data to be inserted in sequence :

  1. Node (insert product name)
  2. myproduct_category (take the inserted node id and save the product category and nid at the same time in this table)
  3. myproduct_price (take the inserted node id and save the product price and nid at the same time in this table)

Once data are inserted into database, the site admin will retrieve those saved data and show it in front-end.

here's my current code to insert data into node, i just can't get the last inserted id and continue insert data into other tables :

            $node   = new stdClass();
            $node->title    = $val['ADDRESS_1'];
            $node->type     = 'basil_veco';
            node_object_prepare($node);
            $node->language = LANGUAGE_NONE;
            $node->uid      = $user->uid;
            $node->status   = 1;
            $node->promote  = 0;
            $node->comment  = 0;
            $node->created  = timestamp;

            $node = node_submit($node); // Prepare node for saving      
            node_save($node);

Please tell me the way to go about if my approach is not the right way.

Thank you.

1

Why you are using custom code to store the records in the node table. Are you calling any API service or you creating form yourself?

If you have created content type, then just go with drupal way and call custom submit function to store products and price values in a separate table by using db_write_record() or db_insert() function to insert the record in the database.

Just go through the below link

https://api.drupal.org/api/drupal/includes!common.inc/function/drupal_write_record/7 https://www.drupal.org/node/310079

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.