benqus benqus - 2 months ago 9
PHP Question

What might be wrong with this MongoDB update $set query via PHP?

I am a little stuck with an updating on a collection.

So, I have this query:

$coll->update(
array("_id" => new MongoId($_POST["id"])),
array("$set" => array(
"contentType" => htmlspecialchars(strip_tags(trim($_POST["type"]))),
"contentTitle" => htmlspecialchars(strip_tags(trim($_POST["title"]))),
"contentBody" => htmlspecialchars(strip_tags(trim($_POST["content"])))
)),
true,
false
);


Please don't bother with the way of validating the $_POST, it's only a test, but it doesn't work. The collection is ok, I have checked that. It doesn't do anything, and when I insert
var_dump($db->lastError());

it gives me no errors at all.

Answer Source

Okay, I have got the solution for the moment.

$r = $coll->findOne(
            array(
                "_id" => new MongoId($_POST["id"])
            )
        );
        //
        $r["contentType"] = htmlspecialchars(strip_tags(trim($_POST["type"])));
        $r["contentTitle"] = htmlspecialchars(strip_tags(trim($_POST["title"])));
        $r["contentBody"] = htmlspecialchars(strip_tags(trim($_POST["content"])));
        //
        $coll->save($r);

So this one works fine. However I would very much like to find out the previous thingy... =P If anyone knows why it isn't working feel free to answer! =]