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.