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);
Sign up to request clarification or add additional context in comments.

4 Comments

I have tried that but getting the error "Call to a member function insert() on a non-object in..."
@user982853 are you doing it exactly like above or are you calling json_encode?
Exactly as you posted.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.