简体   繁体   中英

How can I filter a MongoDB entry to update it using updateOne on PHP?

I am trying to update an entry on MongoDB using the code below. I want it so that it updates the 'name' field pertaining to the studentid posted from the html form. I keep getting a syntax error for unexpected ) even though I have changed it around a lot to no avail. I got this code straight from the MongoDB documentation too?

<?php
require 'vendor/autoload.php' ;
$client = new MongoDB\Client('mongodb://127.0.0.1:27017');
$db_name = 'studentsinfo';
$db = $client->$db_name;
$collection = $db->students;

if($_POST)
{
  $update = $collection->updateOne([
  'studentid'=> $_POST['studentid'],
   [ '$set' => [ 'name' => 'Brunos on Astoria' ]]
  
);
}

Missing square bracket ] in your updateOne method:

if($_POST){
     $update = $collection->updateOne(
     ['studentid'=> $_POST['studentid']],
     [ '$set' => [ 'name' => 'Brunos on Astoria' ]]
     );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM