2014-09-22 75 views
0

我正在尝试更新mongoDB中的数组,代码执行时没有发生错误,但文档未被更新。mongoDB中的PHP更新查询不起作用

这里是我的代码:

<?php 
include 'connection.php'; 
session_start(); 
$username = $_SESSION ['username']; 
$collection = $database->selectCollection ($username); 

$books = $collection->find (array (
     '_id' => new MongoId ($_POST ['id']) 
)); 
$book = $books->getNext(); 
$imgid = $book ['imgid']; 
$id = $book['_id']; 

if (isset ($_POST ['formsubmit'])) { 

    $gridFS = $database->getGridFS(); 

    // if (isset ($_FILES ['pic'])) 
    // $imgid = $gridFS->storeUpload ('pic', array (
    // "username" => $username 
    //)); 

    $collection->update (array (
      "_id" => new mongoId ($id) 
    ), array (
      '$set' => array (
        'bookname' => $_POST ['bookname'], 
        'authname' => $_POST ['authname'], 
        'pubname' => $_POST ['pubname'], 
        'imgid' => $imgid, 
        'cost' => $_POST ['cost'] 
      ) 
    )); 

    echo '<h3>File Updated Successfully</h3>'; 

} 
$mongo->close(); 
?> 
<html> 
<head> 
<title>Online Book Exchange::Exchange old/Used books</title> 
<link rel="icon" href="images/favicon.ico"> 
<link href="style/own.css" rel="stylesheet" type="text/css"> 
</head> 
<body> 
    <div class="container_box"> 

     <div class="navbar"> 
      <p> 
       <a href="session.php">Dash Board</a> &nbsp;|&nbsp; <a 
        href="logout.php" 
        onClick="return confirm('Are you sure you want to Logout?')">Logout</a> 
      </p> 
     </div> 
     <br> <br> 
     <div class="motofont"> 
      <h3> 
       <img src="images/LogoShortMedium.png" alt="Online book store" 
        width="162" height="39" />&nbsp;&nbsp;&nbsp; Edit Book 
      </h3> 
      <hr> 
     </div> 
     <br> <br> 
     <div id="errorBox"></div> 
     <form name="form" method="POST" enctype="multipart/form-data" 
      action="editbook.php"> 
      <table width="60%" cellspacing="2" cellpadding="5" align="center"> 
       <tr> 
        <td> 
         <div class="reg_font">Book Name :</div> 
        </td> 
        <td><input type="text" name="bookname" id="bookname" class="txt" 
         value="<?php echo $book['bookname']; ?>" /></td> 
       </tr> 
       <tr> 
        <td> 
         <div class="reg_font">Author Name:</div> 
        </td> 
        <td><input type="text" name="authname" id="authname" class="txt" 
         value="<?php echo $book['authname']; ?>" /></td> 
       </tr> 
       <tr> 
        <td> 
         <div class="reg_font">Publication Name:</div> 
        </td> 
        <td><input type="text" name="pubname" id="pubname" class="txt" 
         value="<?php echo $book['pubname']; ?>" /></td> 
       </tr> 
       <tr> 
        <td> 
         <div class="reg_font">Cost</div> 
        </td> 
        <td><input type="text" name="cost" id="cost" class="txt" 
         value="<?php echo $book['cost']; ?>" /> <input type="hidden" 
         name="id" id="id" value="<?php $id?>" /></td> 
       </tr> 
       <tr> 
        <td><div class="reg_font">Upload an Image:</div></td> 
        <td><input type="file" name="pic" id="pic" class="btn_2" /></td> 
       </tr> 
       <tr> 
        <td colspan="2" align="center" valign="middle"><button 
          type="submit" value="Register" name="formsubmit" id="formsubmit" class="btn">Edit</button></td> 
       </tr> 
      </table> 
     </form> 
     <hr> 
    </div> 
</body> 
</html> 

我已经采取了“身份证”由POST方法不同的文件和搜索在此文件中需要的文件。然后我引用更新查询。但它不能正常工作

并且我该如何检查图像是否在表单中被选中?如果新图片上传,我想替换现有的图片。

在此先感谢

回答

2

我有POST方法采取“身份证”,从一个不同的文件和搜索 对该文件所需的文件。然后我引用更新查询。但 它不工作

如果你从集合中选择一个文件,你如果没有文件被发现应该考虑使用MongoCollection::findOne()方便返回第一个匹配的文件(如关联数组)或null。这比使用find()创建光标效率更高,并且调用getNext()

一眼就可以看出,表单不起作用,因为您没有正确回显标识符值。引用你的例子:

<input type="hidden" name="id" id="id" value="<?php $id?>" /> 

<?php $id?>本质上是NOP。您可能的意思是将$id打印为字符串。

作为一个侧面观察,我没有理由$setimgid在您的更新查询字段。在find()查询和更新之间,此值永远不会更改。此外,$id先前分配给$book['_id'],它应该已经是MongoId对象。在这种情况下,没有理由在更新条件中构造一个新的MongoId对象。您应该可以原样使用$id(因为它是MongoId)。

以及如何检查图像是否在表格中选择?如果新图片上传,我想 替换现有图片。

除了将用户名与GridFS文件存储在一起之外,我还会建议添加书籍标识符,以便您可以根据需要轻松地从文件文档中确定相关书籍。您所评论的代码看起来是正确的。可能缺少的是删除原始文件,这将是$book['imgid'](如果有的话)。我建议推迟这个清理步骤,直到新的GridFS插入和更新成功。


单独提示:您不应该在脚本末尾调用MongoClient::close()。事实上,文档强烈反对它,因为它挫败了驱动程序使用持久连接的能力。

此外,由于您似乎在使用用户名作为集合名称,因此我建议您查看MongoDB的collection naming restrictions以确保您不会无意中尝试使用无效名称。这看起来像是对多租户的尝试,通常是在数据库级而不是单独的集合上完成的。除非你真的需要分离数据,否则我建议将它存储在一个集合中以开始。这将要求您在每个书籍文档中存储用户名,或者更好的是用户标识符;但是,单个集合可以更轻松地跟踪索引,并避免使用用户名作为集合名称时可能发生的冲突。