user982853
user982853

Reputation: 2488

PHP Mongodb - POST array insert

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.

Upvotes: 0

Views: 897

Answers (1)

Neil Lunn
Neil Lunn

Reputation: 151072

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);

Upvotes: 1

Related Questions