# Unique result from two fields not working

**URL:** <https://www.phphelp.com/t/unique-result-from-two-fields-not-working/32104>\
**Category:** Code Snippets\
**Created:** [June 19, 2020, 8:53am UTC](https://www.phphelp.com/t/unique-result-from-two-fields-not-working/32104 "2020-06-19T08:53:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sailingmike49](https://www.phphelp.com/user_avatar/www.phphelp.com/sailingmike49/32/3210_2.png) [@Sailingmike49](https://www.phphelp.com/u/Sailingmike49)\
**Post date:** [June 19, 2020, 8:53am UTC](https://www.phphelp.com/t/unique-result-from-two-fields-not-working/32104/1 "2020-06-19T08:53:24Z")

</div>

I have a location field [539] and a date field [663] - latter receives combined result of two date fields and I need a unique result of the location field and combined date field. I’m using the code below but it doesnt appear to be working as testing results in positive eveytime when I’ve tried a negative test on a date I know is filled, I still get an appointment on that date?

```auto
add_filter('frm_validate_field_entry', 'two_fields_unique', 10, 2);
function two_fields_unique( $errors, $posted_field ) {
  $first_field_id = 539; // change 125 to the id of the first field
  $second_field_id = 663; // change 126 to the id of the second field
  if ( $posted_field->id == $first_field_id ) {
    $entry_id = isset( $_POST['id'] ) ? absint( $_POST['id'] ) : 0;
    $values_used = FrmDb::get_col( 'frm_item_metas',
		array( 'item_id !' => $entry_id,
			array( 'or' => 1,
				array( 'field_id' => $first_field_id, 'meta_value' => $_POST['item_meta'][$first_field_id] ),
				array( 'field_id' => $second_field_id, 'meta_value' => $_POST['item_meta'][$second_field_id] ),
			)
		), 'item_id', array( 'group_by' => 'item_id', 'having' => 'COUNT(*) > 1' )
	);
	if ( ! empty( $values_used ) ) {
		$errors['field'. $first_field_id] = 'You have already selected that option';
		$errors['field'. $second_field_id] = 'You have already selected that option';
	}
  }

  return $errors;
}
```

---

<div class="post-metadata">

**Author:** ![ErnieAlex](https://www.phphelp.com/letter_avatar_proxy/v4/letter/e/bc8723/32.png) [@ErnieAlex](https://www.phphelp.com/u/ErnieAlex)\
**Post date:** [June 19, 2020, 1:24pm UTC](https://www.phphelp.com/t/unique-result-from-two-fields-not-working/32104/2 "2020-06-19T13:24:16Z")

</div>

> [@Sailingmike49](#):
>
> array( ‘field\_id’ =\> $first\_field\_id, ‘meta\_value’ =\> $\_POST[‘item\_meta’][$first\_field\_id] ),  
> array( ‘field\_id’ =\> $second\_field\_id, ‘meta\_value’ =\> $\_POST[‘item\_meta’][$second\_field\_id] ),

You are pulling the same field from the $\_POST data for each item. $\_POST[‘item\_meta’] …  
So, you would always get the same two for each. Not sure what this code is supposed to do, but  
that is probably the problem…

---

<div class="post-metadata">

**Author:** ![benanamen](https://www.phphelp.com/user_avatar/www.phphelp.com/benanamen/32/287_2.png) [@benanamen](https://www.phphelp.com/u/benanamen)\
**Post date:** [June 19, 2020, 4:30pm UTC](https://www.phphelp.com/t/unique-result-from-two-fields-not-working/32104/3 "2020-06-19T16:30:27Z")

</div>

Tell us what the real problem is that you are trying to solve by doing this. What is the high level overview of what you have going on?

---

<div class="post-metadata">

**Author:** ![Sailingmike49](https://www.phphelp.com/user_avatar/www.phphelp.com/sailingmike49/32/3210_2.png) [@Sailingmike49](https://www.phphelp.com/u/Sailingmike49)\
**Post date:** [July 3, 2020, 5:42pm UTC](https://www.phphelp.com/t/unique-result-from-two-fields-not-working/32104/4 "2020-07-03T17:42:22Z")

</div>

Hi benanamen,  
I’ve got further and got a new request at this link if you are able to have a look?

> [@Need help to change booking system php](https://www.phphelp.com/t/need-help-to-change-booking-system-php/32179):
>
> So I have the php code which works well BUT: Users now want to make a booking IF a current user has left the location. The booking system is for boats using facilities and predicated on a booking being unique based on the date and location being unique. So now if the new required date clashes with the desired date and location BUT the existing booking Leave Time is past compared to the current system time then the booking system should allow the new booking? Current PHP below so I would wel…
