如何使用PHP更新MongoDB中的整个文档?

时间:2015-08-17 10:28:27

标签: php mongodb nosql

我想通过MongoDB中的PHP界面更新我的旧条目。 首先,我从文本字段中获取数据,然后将其存储到变量中,然后使用这些变量更新Mongodb中的数据,这是我的代码,请帮助我,我尝试各种方式,但每次都失望。

<?php
                    if(isset($_REQUEST['btn']))
                    {
                        $a=$_REQUEST['textfield'];
                        $b=$_REQUEST['textfield2'];
                        $c=$_REQUEST['textfield3'];
                        $d=$_REQUEST['textfield4'];
                        $e=$_REQUEST['textfield5'];
                        $f=$_REQUEST['textfield6'];
                        $g=$_REQUEST['textfield7'];
                        $h=$_REQUEST['textfield8'];

                         $m = new MongoClient();         // connect to mongodb
                         $db = $m->app;             // select a database named app  
                         $collection = $db->lafaz;  

$db->lafaz->update(array("_id"=> new MongoID($a)),$doct, array('multiple' => true));

                         header('Location:page.php');
                    }?>

<!doctype html>
<html>
<head>
<style type="text/css">
    header a {
    font-weight: bold;
    font-family: Sarina;
    font-size: 45pt;
    font-style: oblique;
    text-decoration: none;
    text-shadow: 2px 2px   #33CCFF;
    color: #369;
}
</style>
<link href="form.css" rel="stylesheet" type="text/css" />

<meta charset="utf-8">
<title>Edit Word</title>
</head>
<body>
<div>
      <header align="center"><a href="page.php">Tarmeem -e- Haraf</a> </header>      
     <nav align="center">

       </nav>
  <div align="right" >
                        <?php
                         $m = new MongoClient();
                         $db = $m->app; 
                         $collection = $db->lafaz; 
                         $id=$_REQUEST['_id'];
                         $cursor = $collection->find(array("_id"=> new MongoID($id)));
                         foreach ($cursor as $obj)
                         {
                        ?>
    <form action="#" method="post">
      <table width="100%" class="top-table" >
        <tr>
          <td width="40%" align="right"><p>ID:</p></td>
          <td width="3%">&nbsp;</td>
          <td width="57%" align="left">
          <input class="tf" type="text" name="textfield" id="textfield" readonly value=" <?php echo $obj["_id"]; ?> " ></td>
        </tr>

        <tr>
          <td align="right"><p>Encoding:</p></td>
          <td>&nbsp;</td>
          <td align="left">
          <input class="tf" type="text" name="textfield2" id="textfield2" required value=" <?php echo $obj["Encoding"]; ?> " ></td>
        </tr>

        <tr>
          <td align="right"><p>Pos:</p> </td>
          <td>&nbsp;</td>
          <td align="left">
          <input class="tf" type="text" name="textfield3" id="textfield3" required value=" <?php echo $obj["Pos"]; ?> " ></td>
        </tr>

        <tr>
          <td align="right"><p>Roman: </p></td>
          <td>&nbsp;</td>
          <td align="left">
          <input class="tf" type="text" name="textfield4" id="textfield4" required value=" <?php echo $obj["Roman"]; ?> " ></td>
        </tr>

        <tr>
          <td align="right"><p>Important: </p></td>
          <td>&nbsp;</td>
          <td align="left">
          <input class="tf" type="text" name="textfield5" id="textfield5" required value=" <?php echo $obj["Important"]; ?> " ></td>
        </tr>

        <tr>
          <td align="right"><p>Hindi: </p></td>
          <td>&nbsp;</td>
          <td align="left">
          <input class="tf" type="text" name="textfield6" id="textfield6" required value=" <?php echo $obj["Hindi"];; ?> " ></td>
        </tr>

        <tr>
          <td align="right"><p>English: </p></td>
          <td>&nbsp;</td>
          <td align="left">
          <input class="tf" type="text" name="textfield7" id="textfield7" required value=" <?php echo $obj["English"]; ?> " ></td>
        </tr>

        <tr>
          <td align="right"><p>Type: </p></td>
          <td>&nbsp;</td>
          <td align="left">
          <input class="tf" type="text" name="textfield8" id="textfield8" required value=" <?php echo $obj["Type"]; ?> " ></td>
        </tr>

      </table>
      <input name="btn" id="btn" class="button" type="submit" value="Save"></td>

<?php } ?>
    </form>
  </div>

</div>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

这是一个完整工作的页面,已注释,以便您可以关注我。你需要通过它的声音将PHP运行时更新到更新的版本,但这应该已经完成​​,特别是因为PHP目前总是向后兼容。

$m = new \MongoClient();
$db = $m->app;
$collection = $db->lafaz;

// Better to sometimes actually check the right array, especially if someone wants to attack
// you by tricking PHP into combining the REQUEST from the GET
if(isset($_POST['btn'])){

    // We take the $_POST piece by piece, do a trim to strip white space
    // and then we add it to the doc array ready for use in MongoDB
    // Of course this is not good practice for production programs but
    // it is somewhere to start
    $doc = [];
    foreach($_POST as $k => $v){
        $doc[$k] = trim($v);
    }

    // This will detect if we are updating or not, if no a is set then no _id was passed
    if(isset($doc['_id'])){
        // Some validation to ensure that we have a valid MongoID, consider it free knowledge
        try{
            $_id = new \MongoId($doc['_id']);
        }catch(\Exception $e){
            throw new \Exception('The _id inputted was not valid: ' . var_export($doc['_id'], true));
        }
        // Unset the _id as to not raise an error
        unset($doc['_id']);

        // Could do an upsert here but, meh, I prefer the logic displayed here
        $collection->update(["_id"=> $_id], ['$set' => $doc], ['multiple' => true]);
    }else{
        $collection->insert($doc);
    }

    // We redirect to somewhere
    header('Location:page.php');
    // stop further processing and just send the damn headers
    exit();
}

// If there is a _id in the REQUEST array
if(isset($_REQUEST['_id'])){
    // Some validation to ensure that we have a valid MongoID, consider it free knowledge
    try{
        $_id = new \MongoId($_REQUEST['_id']);
    }catch(\Exception $e){
        throw new \Exception('The _id inputted was not valid: ' . var_export($_REQUEST['_id'], true));
    }

    // We find the one we just did, btw your code atm means this never happens...
    $cursor = $collection->find(["_id"=> new \MongoID($_id)]);
}else{
    // We just find all
    $cursor = $collection->find();
}


?>
<!doctype html>
<html>
<head>
    <style type="text/css">
        header a {
            font-weight: bold;
            font-family: Sarina;
            font-size: 45pt;
            font-style: oblique;
            text-decoration: none;
            text-shadow: 2px 2px   #33CCFF;
            color: #369;
        }
    </style>
    <link href="form.css" rel="stylesheet" type="text/css" />
    <meta charset="utf-8">
    <title>Edit Word</title>
</head>
<body>
<div>
<header align="center"><a href="page.php">Tarmeem -e- Haraf</a> </header>
<nav align="center">
</nav>
<div align="right">

<!-- Add a new one form. There are better ways to do this but this is just to get it working -->
<form action="#" method="post">
<table width="100%" class="top-table" >
<tr>
<td align="right"><p>Encoding:</p></td>
<td>&nbsp;</td>
<td align="left">
<input class="tf" type="text" name="Encoding" id="textfield2" required></td>
</tr>
<tr>
<td align="right"><p>Pos:</p> </td>
<td>&nbsp;</td>
<td align="left">
<input class="tf" type="text" name="Pos" id="textfield3" required></td>
</tr>
<tr>
<td align="right"><p>Roman: </p></td>
<td>&nbsp;</td>
<td align="left">
<input class="tf" type="text" name="Roman" id="textfield4" required></td>
</tr>
<tr>
<td align="right"><p>Important: </p></td>
<td>&nbsp;</td>
<td align="left">
<input class="tf" type="text" name="Important" id="textfield5" required></td>
</tr>
<tr>
<td align="right"><p>Hindi: </p></td>
<td>&nbsp;</td>
<td align="left">
<input class="tf" type="text" name="Hindi" id="textfield6" required></td>
</tr>
<tr>
<td align="right"><p>English: </p></td>
<td>&nbsp;</td>
<td align="left">
<input class="tf" type="text" name="English" id="textfield7" required></td>
</tr>
<tr>
<td align="right"><p>Type: </p></td>
<td>&nbsp;</td>
<td align="left">
<input class="tf" type="text" name="Type" id="textfield8" required></td>
</tr>
</table>
<input name="btn" id="btn" class="button" type="submit" value="Create">
</form>
<?php foreach ($cursor as $obj){ ?>
<form action="#" method="post">
<table width="100%" class="top-table" >
<tr>
<td width="40%" align="right"><p>ID:</p></td>
<td width="3%">&nbsp;</td>
<td width="57%" align="left">
<input class="tf" type="text" name="_id" id="textfield" readonly value="<?= $obj["_id"] ?>"/></td>
</tr>
<tr>
<td align="right"><p>Encoding:</p></td>
<td>&nbsp;</td>
<td align="left">
<input class="tf" type="text" name="Encoding" id="textfield2" required value="<?= $obj["Encoding"] ?> "/></td>
</tr>
<tr>
<td align="right"><p>Pos:</p> </td>
<td>&nbsp;</td>
<td align="left">
<input class="tf" type="text" name="Pos" id="textfield3" required value="<?= $obj["Pos"] ?>"/></td>
</tr>
<tr>
<td align="right"><p>Roman: </p></td>
<td>&nbsp;</td>
<td align="left">
<input class="tf" type="text" name="Roman" id="textfield4" required value="<?= $obj["Roman"] ?>"/></td>
</tr>
<tr>
<td align="right"><p>Important: </p></td>
<td>&nbsp;</td>
<td align="left">
<input class="tf" type="text" name="Important" id="textfield5" required value="<?= $obj["Important"] ?>"/></td>
</tr>
<tr>
<td align="right"><p>Hindi: </p></td>
<td>&nbsp;</td>
<td align="left">
<input class="tf" type="text" name="Hindi" id="textfield6" required value="<?= $obj["Hindi"] ?>"/></td>
</tr>
<tr>
<td align="right"><p>English: </p></td>
<td>&nbsp;</td>
<td align="left">
<input class="tf" type="text" name="English" id="textfield7" required value="<?= $obj["English"] ?>"/></td>
</tr>
<tr>
<td align="right"><p>Type: </p></td>
<td>&nbsp;</td>
<td align="left">
<input class="tf" type="text" name="Type" id="textfield8" required value="<?= $obj["Type"] ?>"/></td>
</tr>
</table>
<input name="btn" id="btn" class="button" type="submit" value="Save">
</form>
<?php } ?>
</div>
</div>
</body>
</html>
<?php