duanjie2940 2019-04-25 03:57
浏览 44

单击“提交”按钮时单个条目的PHP Mongo更新特定字段

I have an HTML table that displays data from MongoDB. However in the final column of each row there is an input form from user that can update the entries of that row. On clicking the submit, and using update() those fields for all the rows are getting edited.

My initial DB entry looks like this :

{"group_size" : "3", "form" : "tour_request_quote_form", "phone" : "9999999999", "url" : "#", "referrer" : "#", "base_url" : "#", "headers" : { "User-Agent" : "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36" }, "tour_date" : "04-24-2019", "email" : "a@gmail.com", "company_list" : [ "A", "B", "C" ], "person_list" : [ "A", "B", "C" ], "lead_status" : [ "Follow Up", "New Lead", "Booked" ], "lead_assigned" :"NIL", "person_assigned": "NIL", "status":"New Lead"}

The final 3 entries "lead_assigned", "person_assigned", "status" are editable.

This is the code for my table, the table is displayed perfectly as it should.

db=$m->selectDB('fullstack');
$collection = $db->clients;
$cursor = $collection->find();
foreach($cursor as $document){
  if(!(isNull($document["group_size"]) || isNull($document["phone"])|| isNull($document["tour_date"]) || isNull($document["email"])))
  {?>
<tr>
  <td><?php echo $document["group_size"]?></td>
  <td><?php echo $document["form"]?></td>
  <td><?php echo $document["phone"]?></td>
  <td><?php echo $document["tour_date"]?></td>
  <td><?php echo $document["email"]?></td>
  <td><?php echo $document["url"]?></td>
  <td><?php echo $document["referrer"]?></td>
  <td><?php echo $document["base_url"]?></td>
  <td><?php echo $document["lead_assigned"]?></td>
  <td><?php echo $document["person_assigned"]?></td>
  <td><?php echo $document["status"]?></td>
  <td>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
      <h4>Lead Assignment</h4>
      <select class="lead" name="lead">
        <?php foreach($document["company_list"] as $val) {
          ?>
          <option value='<?php echo $val;?>'><?php echo $val; ?></option>
        <?php } ?>
      </select><br><h4>Person Assignment</h4>
      <select class="person" name="person">
        <?php foreach($document["person_list"] as $val) {
          ?>
          <option value='<?php echo $val;?>'><?php echo $val; ?></option>
        <?php } ?>
      </select><br>
      <h4>Lead Status</h4>
      <select class="status" name="status">
        <?php foreach($document["lead_status"] as $val) {
          ?>
          <option value='<?php echo $val;?>'><?php echo $val; ?></option>
        <?php } ?>
      </select><br><br>
      <input type="submit" name="submit" value="Update Details">
    </form>
    <?php
    if(isset($_POST['submit']))
    {
      $document['lead_assigned']=$_POST['lead'];
      $document['person_assigned']=$_POST['person'];
      $document['status']=$_POST['status'];

      updateDB($collection,$document,$document['_id']);
    }

This is the updateDB function:

function updateDB($collection,$new_data,$id)
{
  $filter=array('_id' => $id,'phone' => $new_data['phone'] );
  $collection->update($filter, array('$set' => $new_data),array('upsert' => true));
}

What I want is that the entry with id x,should only get edited with the new values. However finally this is what's happening

{ "_id" : ObjectId("5cc0dab44b24f233684908fa"), "group_size" : "3", "form" : "tour_request_quote_form", "phone" : "9999999999", "url" : "#", "referrer" : "#", "base_url" : "#", "headers" : { "User-Agent" : "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36" }, "tour_date" : "04-24-2019", "email" : "a@gmail.com", "company_list" : [ "A", "B", "C" ], "person_list" : [ "A", "B", "C" ], "lead_status" : [ "Follow Up", "New Lead", "Booked" ], "lead_assigned" : "A", "person_assigned" : "A", "status" : "Booked" }

{ "_id" : ObjectId("5cc0dac94b24f233684908fb"), "group_size" : "4", "form" : "tour_request_quote_form", "phone" : "9099090999", "url" : "#", "referrer" : "#", "base_url" : "#", "headers" : { "User-Agent" : "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36" }, "tour_date" : "05-26-2019", "email" : "ab@gmail.com", "company_list" : [ "A", "B", "C" ], "person_list" : [ "A", "B", "C" ], "lead_status" : [ "Follow Up", "New Lead", "Booked" ], "lead_assigned" : "A", "person_assigned" : "A", "status" : "Booked" }

Both the dummy entries got edited on clicking one submit in the first row, last column.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
    • ¥15 关于#hadoop#的问题
    • ¥15 (标签-Python|关键词-socket)
    • ¥15 keil里为什么main.c定义的函数在it.c调用不了
    • ¥50 切换TabTip键盘的输入法
    • ¥15 可否在不同线程中调用封装数据库操作的类