• Resolved David Gard

    (@duck_boy)


    Hey all,

    I’m trying to add some data to a table whenever a users sends a form from my site. I’m struggling though with the form data as this is an array, which I understand needs to be serialized to be stored in the DB? I’ve tried the following method but that doesn’t seem to work –

    if(!is_serialized($form_data)) :
    	$data = serialize($form_data);
    endif;

    Here is the link to the full code in the Paste Bin.

    I’d be greatful for any help with this.

    Thanks.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    When I try this with your code:

    $form_data = array('array' => 'does it work');
    	if(!is_serialized($form_data)) :
    	$data = serialize($form_data);
    endif;
    echo $data;

    The output is a serialized array: a:1:{s:5:”array”;s:12:”does it work”;}

    Are you sure the function add_form_to_table() is called?

    http://codex.wordpress.org/Class_Reference/wpdb#INSERT_rows
    http://andrewnacin.com/2010/04/18/wordpress-serializing-data/

    Moderator keesiemeijer

    (@keesiemeijer)

    from the codex link above, $data needs to be a array:

    <?php $wpdb->insert( $table, $data, $format ); ?>
    data (array) Data to insert (in column => value pairs). Both $data columns and $data values should be “raw” (neither should be SQL escaped).

    Thread Starter David Gard

    (@duck_boy)

    Hi keesiemeijer, and thanks for your reply.

    I’ve got it sorted now, with the issue being that I was trying to searilaze the whole array that I was inserting, rather than just the arrays within that needed it! School boy error.

    Thanks for your time.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Adding array data to the database’ is closed to new replies.