0

I made a function in class connectDB to update a document then try to call that function and always has error :

Notice: Undefined index: username in C:\xampp\htdocs\phpmongodb\TM\connectDB.php on line 34

Notice: Undefined index: name in C:\xampp\htdocs\phpmongodb\TM\connectDB.php on line 35

Notice: Undefined index: email in C:\xampp\htdocs\phpmongodb\TM\connectDB.php on line 36

public function editAdmin( $editInfo = [])
{
    $edited = $this->db->updateOne(
    ['_id'=>$editInfo['_id']],
    ['$set' =>['username' =>$editInfo['username'], 
               'name'=>$editInfo['name'], 
               'email'=>$editInfo['email'] ]],

    ['multi' => false, 'upsert' => false]

);
    return $edited;

}

and this is how I use the function in anothe php page will inputs:

$edited=$db->editAdmin(

                ['_id'=>new MongoDB\BSON\ObjectID($id)],
                ['$set' =>
                    ['username' =>$_POST['U'],
                     'name'=>$_POST['N'],
                     'email'=>$_POST['E']]
                ],
                ['multi' => false, 'upsert' => false]
            );

            print_r($edited->getModifiedCount());
            print_r($edited->getMatchedCount());

<input type='text' name='U' id='U'  class='form-control'  placeholder='Username'  >

  <input type='text' name='N'    id='N' class='form-control' placeholder='Name' >
    
  <input type='text' name='E' id='E' class='form-control'  placeholder='E_mail'>

all other CRUD operation works well except 'update' and I still don't know why it doesn't take what I write in $_POST.

Can anyone help please?

Yosra MH
  • 129
  • 3
  • 11
  • You basically wrote out your `->update()` arguments twice. Either pass through the arguments or actually translate the mappings. Your current code does neither, hence why `'username'` or `'name'` are undefined, because you nested the values under `'$set'` when passing into the function. – Neil Lunn Aug 25 '17 at 10:28
  • thank u for your reply^^ but I changed the names and still doesn't work! I don't think it's a matter of labels honestly – Yosra MH Aug 25 '17 at 11:02

0 Answers0