1

I'm trying the store multiple rows into a database. The number of fields will change. Below is the way that I use to store just one row.

  <?php

  if(isset($_POST['save'])){
      $wpdb->insert('wp_table_name',
          array(
              'field_a' => $_POST['field_a'],
              'field_b' => $_POST['field_b'],
              'field_c' => $_POST['field_c'],
              'field_d' => $_POST['field_d']             
              ),
          array(
              '%s',
              '%s',
              '%s',
              '%s'
              )
          );
  }
  ?>

  <form>
      <input type="text" name="field_a[]"/>
      <input type="text" name="field_b[]"/>
      <input type="text" name="field_c[]"/>
      <input type="text" name="field_d[]"/>

      <input type="text" name="field_a[]"/>
      <input type="text" name="field_b[]"/>
      <input type="text" name="field_c[]"/>
      <input type="text" name="field_d[]"/>

      <input type="text" name="field_a[]"/>
      <input type="text" name="field_b[]"/>
      <input type="text" name="field_c[]"/>
      <input type="text" name="field_d[]"/>

      <button type="submit" name="save">Save</button>
  </form>

3 Answers 3

5

What WP does for arrays (and objects) on some contexts (such as post fields) is using maybe_serialize()/maybe_unserialize() to turn such types (and just them) to and from serialized (string-typed) representation.

While this simplifies workflow it comes with penalties, such as being unable to properly query through such data and common issues with migration (seriazlization-unaware tools or actions easily ruin serialized strings).

If you have control over design of custom table and it serves specific dedicated function — you should probably design it in a way that you don't store whole arrays in first place.

4
  • Not sure what this has to do with my question?
    – user759235
    Aug 24, 2014 at 20:04
  • 2
    @user759235 ...it explains how WordPress stores arrays into database when needed? :)
    – Rarst
    Aug 24, 2014 at 20:07
  • good explanation. Or maybe we can create our own migration tool. But the problem with querying filter for data still intact.
    – Adi Kusuma
    Jul 13, 2020 at 8:49
  • Great answer btw. What if we're storing arrays of data associated with a specific native object? i.e. a list of favorite foods per user, or a list of URLs associated with a particular post
    – John Dee
    Sep 29, 2020 at 21:36
0
<?php
if(isset($_POST['save'])){
      $wpdb->insert('wp_table_name',
          array(
              'field_a' => maybe_serialize( $_POST['field_a'] ),
              'field_b' => maybe_serialize( $_POST['field_b'] ),
              'field_c' => maybe_serialize( $_POST['field_c'] ),
              'field_d' => maybe_serialize( $_POST['field_d'] )             
              ),
          array(
              '%s',
              '%s',
              '%s',
              '%s'
              )
          );
}
?>

Use the maybe_unserialize function to get the value as array.

1
  • Doesn't this saves everything in 1 row? I need to store every value in seperate rows.
    – user759235
    Aug 25, 2014 at 9:07
-1

You can save post meta with the same meta key, just set the $unique parameter to false.

if(isset($_POST['save'])){
    foreach ($_POST as $post_key => $post_value){
        if ( 'save' === $post_key )
            continue;
        if ( is_array($post_value) ) {
            foreach ( $post_value as $key => value ) {
                add_post_meta($post_id, $post_key . '[' . $key . ']', $post_value, false);
            }
        }
        else{
            add_post_meta($post_id, $post_key, $post_value, false);
        }
    }
}

Then when you can call get_post_meta ( $post_id, $post_key, false ) to retrieve all of the meta values with that particular meta key.

That of course if this was a post, and you want to save something related to that post. For example if your post was the form and you want to sve the submissions as meta-data for that specific form(post).

else i would just do 1 insert to the table per field on a foreach like the example I gave above but using the $wpdb->insert() method you used.

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.