Title: Insert records into pods using php
Last modified: August 22, 2016

---

# Insert records into pods using php

 *  Resolved [Shoptutor](https://wordpress.org/support/users/shoptutor/)
 * (@shoptutor)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/insert-records-into-pods-using-php/)
 * Hello Guys,
 * I have an plugin where I import some data, and then create an php array.
    This
   array is already on the same specs as one pod I created.
 * How do I insert into my pod these records?
 * [https://wordpress.org/plugins/pods/](https://wordpress.org/plugins/pods/)

Viewing 9 replies - 1 through 9 (of 9 total)

 *  Plugin Contributor [Josh Pollock](https://wordpress.org/support/users/shelob9/)
 * (@shelob9)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/insert-records-into-pods-using-php/#post-5797300)
 * [@shoptutor](https://wordpress.org/support/users/shoptutor/) – we resolved this
   issue in our Slack right?
 * For anyone googleing into this, the answer was Pods::save()
 * [http://pods.io/docs/code/pods/save/](http://pods.io/docs/code/pods/save/)
 *  Thread Starter [Shoptutor](https://wordpress.org/support/users/shoptutor/)
 * (@shoptutor)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/insert-records-into-pods-using-php/#post-5797307)
 * Yep, it worked here,
 * I’m just not sure how to make it work the check for existing records.
 * What I got so far is:
 *     ```
       $pod_data_array = array(
       			'name' => $record_title,
       			'productitemid' => $record_productitemid, //This is an unique field
       			'productitem_name' => $record_productitem_name,
       			'date_modified_text' => $record_date_modified,
       			'date_modified' => $record_date_modified,
       			'description' => $record_description,
       			'descriptionlong' => $record_descriptionlong,
       			'manufacturer' => $record_manufacturer,
       			'ean' => $record_ean //This is also an unique field
       			); 
   
       $mypod = pods( 'ofertas' );
       //Before Save I should check if there is an record inside my pod, with the same 'ean' as this new record I got here.
   
       $mypod_id = $mypod->save( $pod_data_array );
   
       echo 'Record nº' . $record_queue_number . 'saved!';
       ```
   
 *  Plugin Contributor [Josh Pollock](https://wordpress.org/support/users/shelob9/)
 * (@shelob9)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/insert-records-into-pods-using-php/#post-5797312)
 * Easier to explain with inline comments in code. This would need to have the `
   $pod_data_array` and all the variables making it up before it.
 *     ```
       //query by unique ID to see if a record with this unique ID exists
       $params = array(
       	'where' => 'productitemid.meta_value = "'.$record_productitemid.'"',
       );
   
       //start $id null
       $id = null;
       $mypod = pods( 'ofertas', $params );
       //check if we have results
       if ( 0 > $mypod->total() ) {
       	//if so, should only be one result since we are querying by a unique ID, so loop should run one
       	//set $id to the result we found
       	$id = $pods->id();
       }
   
       //reset iterator
       $mypod->reset();
   
       //save using $id to update existing, or if it's still null create new
       $mypod->save( $data, null, $id );
       ```
   
 *  Thread Starter [Shoptutor](https://wordpress.org/support/users/shoptutor/)
 * (@shoptutor)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/insert-records-into-pods-using-php/#post-5797332)
 * Awesomee!!!
 * Thanks.
 *  Thread Starter [Shoptutor](https://wordpress.org/support/users/shoptutor/)
 * (@shoptutor)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/insert-records-into-pods-using-php/#post-5797365)
 * Hello, I’m back on this…
 * Sometimes, after several loops saving data, It is crashing during inserts, so
   I had performed some tests and figured that it is crashing during the:
 * `$mypod->save( $data, null, $id );`
 * Reading the [pod’s save page](http://pods.io/docs/code/pods/save/) I read the
   following.
 * “**IMPORTANT: This example skips data sanitization and validation for the sake
   of simplicity to explain a concept. Do not use as is, always sanitize data from
   a form.**“
 * So I thought that may be something in the data.
    What kind of sanitization do
   you talk about there? where I can find more info about it?
 *  Plugin Contributor [Josh Pollock](https://wordpress.org/support/users/shelob9/)
 * (@shelob9)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/insert-records-into-pods-using-php/#post-5797366)
 * [http://codex.wordpress.org/Data_Validation](http://codex.wordpress.org/Data_Validation)
 *  Thread Starter [Shoptutor](https://wordpress.org/support/users/shoptutor/)
 * (@shoptutor)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/insert-records-into-pods-using-php/#post-5797368)
 * Another thing is that it never crashes on the fists saving loops. Always after
   inserting 200 and something…
 * Any ideas?
 *  Thread Starter [Shoptutor](https://wordpress.org/support/users/shoptutor/)
 * (@shoptutor)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/insert-records-into-pods-using-php/#post-5797370)
 * I sanitized everything, and had just compared the data from the last pod saved
   with the one that made the error, and there is no diference.
 * So I guess its not a data sanitization issue.
 * any other ideas?
 * Is it possible that it is crashing at save becouse it is getting to much data?
   
   It creashes always near the row 200, never before 150. The data I’m inserting
   is something like this:
 *     ```
       array(39) {
       ["name"]=> string(12) "Mini systems"
       ["description"]=> string(70) "Mini System LG - CM2730 160W, Bluetooth, USB, MP3, Cd e Rádio - Preto"
       ["comment_status"]=> string(4) "open"
       ["ping_status"]=> string(6) "closed"
       ["post_status"]=> string(7) "publish"
       ["post_content"]=> string(70) "Mini System LG - CM2730 160W, Bluetooth, USB, MP3, Cd e Rádio - Preto"
       ["post_excerpt"]=> string(12) "Mini systems"
       ["post_category"]=> string(2) "TV"
       ["category"]=> string(2) "TV"
       ["post_name"]=> string(12) "Mini systems"
       ["post_title"]=> string(12) "Mini systems"
       ["post_author"]=> int(2)
       ["post_date"]=> string(25) "2015-02-23T08:53:38+01:00"
       ["latest_update"]=> string(19) "2015-02-25 02:24:48"
       ["session"]=> string(2) "TV"
       ["saleavailability"]=> string(1) "1"
       ["featureavailability"]=> string(1) "0"
       ["sessioncheck"]=> string(1) "0"
       ["expected_margin"]=> int(49)
       ["affiliatesystem"]=> string(5) "Zanox"
       ["productitemid"]=> string(32) "cc36e3a5fe178746637124c5936639fb"
       ["productitem_name"]=> string(12) "Mini systems"
       ["date_modified_text"]=> string(25) "2015-02-23T08:53:38+01:00"
       ["date_modified"]=> string(25) "2015-02-23T08:53:38+01:00"
       ["idprogram"]=> int(12011)
       ["program_name"]=> string(7) "Walmart"
       ["price"]=> int(899)
       ["currency"]=> string(3) "BRL"
       ["trackinglink_adspaceid"]=> int(2034022)
       ["trackinglink_ppv"]=> string(218) "http://ad.zanox.com/ppv/?30105432C89279541&ULP=Eletronicos/Mini-systems/LG/415012-Mini-System-LG-CM2730-160W-Preto?utm_source=zanox&utm_medium=xml_Zanox&utm_campaign=Zanox&zpar9=4209ECD457683232DBD9"
       ["trackinglink_ppc"]=> string(218) "http://ad.zanox.com/ppc/?30105432C89279541&ULP=Eletronicos/Mini-systems/LG/415012-Mini-System-LG-CM2730-160W-Preto?utm_source=zanox&utm_medium=xml_Zanox&utm_campaign=Zanox&zpar9=4209ECD457683232DBD9"
       ["descriptionlong"]=> string(0) ""
       ["manufacturer"]=> string(2) "LG"
       ["ean"]=> int(7893299527609)
       ["image_url"]=> string(66) "http://static.wmobjects.com.br/imgres/arquivos/ids/2748054-250-250"
       ["merchantcategory"]=> string(12) "Eletrônicos"
       ["merchantproductid"]=> string(6) "415012"
       ["merchantlogo"]=> string(110) "https://upload.wikimedia.org/wikipedia/commons/thumb/7/76/New_Walmart_Logo.svg/1280px-New_Walmart_Logo.svg.png"
       ["manufacturer_model"]=> string(0) "" }
       ```
   
 *  Plugin Contributor [Josh Pollock](https://wordpress.org/support/users/shelob9/)
 * (@shelob9)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/insert-records-into-pods-using-php/#post-5797372)
 * > It creashes always near the row 200, never before 150.
 * Sounds like you’re running out of memory. Try saving less data at a time, or 
   using a more efficient process like WP CLI.

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘Insert records into pods using php’ is closed to new replies.

 * ![](https://ps.w.org/pods/assets/icon.svg?rev=3286397)
 * [Pods - Custom Content Types and Fields](https://wordpress.org/plugins/pods/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/pods/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/pods/)
 * [Active Topics](https://wordpress.org/support/plugin/pods/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/pods/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/pods/reviews/)

## Tags

 * [insert data](https://wordpress.org/support/topic-tag/insert-data/)
 * [php](https://wordpress.org/support/topic-tag/php/)

 * 9 replies
 * 2 participants
 * Last reply from: [Josh Pollock](https://wordpress.org/support/users/shelob9/)
 * Last activity: [11 years, 2 months ago](https://wordpress.org/support/topic/insert-records-into-pods-using-php/#post-5797372)
 * Status: resolved