0

I am trying to convert post data into a format that would allow me to pass it right into my collection. For example: When I print_r on the $_POST I get this form data:

Array
(
    [Name] => Steve
    [Email] => [email protected]
    [submit] => Submit
)

I am wondering how i can convert this to an acceptable object to insert into mongodb collection using php similar to:

$Record = array(
    'Name' => 'Steve',
    'Email' => '[email protected]',
    'submit' => 'Submit'
);
$Collection->insert($Record);

I am thinking a loop of the above array with some additional formatting but I can't seem to figure it out. I have also tried json_encode but keep getting the same error "Call to a member function insert() on a non-object in..." saying that its not a proper object. Thank you for any help.

1 Answer 1

1

No need to encode anything, it's just PHP native and expects an array. Let the driver do the work for you:

$Collection->insert( $_POST );

As it is the two should be equivalant:

$rec = array(
  'Name' => 'Steve',
  'Email' => '[email protected]',
  'submit' => 'Submit'
);

print_r ($rec);
4
  • I have tried that but getting the error "Call to a member function insert() on a non-object in..."
    – user982853
    Commented Mar 10, 2014 at 4:38
  • @user982853 are you doing it exactly like above or are you calling json_encode?
    – Neil Lunn
    Commented Mar 10, 2014 at 4:43
  • Exactly as you posted.
    – user982853
    Commented Mar 10, 2014 at 4:45
  • It seems there was something wrong with my include. I had the mongo connection in an include/class and once i took it out of the class and just put it in the same file it all works fine. I will have to play with my include/class to see what i did wrong. Thanks.
    – user982853
    Commented Mar 10, 2014 at 4:53

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