Check Duplicate with two field

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.

Check Duplicate with two field

Postby nadimsia » Thu Jan 11, 2018 6:13 pm

Hi...

In PHPMAKER there have a "check duplicate" on Edit Tag but it only for individual field. How about, If I want to set when two field already exist, its cannot be duplicate/copy.

Example.
Data already exits.
Field A = Toyota
Field B = Altis

> When insert new data, field A = Toyota, field B = Vois. So data can be added.
> But when insert new data, field A = Toyota, field B = Altis, So data cannot be added.

Its mean when data field A and field B are present, then it cannot be add again.
Only one of the field already exists, then it can be added again.
nadimsia
 
Posts: 28
Joined: Sun Mar 24, 2013 7:02 pm

Re: Check Duplicate with two field

Postby kirondedshem » Thu Jan 11, 2018 6:28 pm

Phpmaker has only support for checking duplicate on one, so if you want a composite key kind of check then.
There is no such a thing but you can easily do it in row_inserting and row_updating events like this.


// Row Inserting event
function Row_Inserting($rsold, &$rsnew) {
// Enter your code here
// To cancel, set return value to FALSE


$if_exists = ew_ExecuteScalar("SELECT count(*) FROM my_table where field_a = '".$rsnew["field_a"]."' and field_b = '".$rsnew["field_b"]."' ");

if($target_exists > 0)
{
$this->CancelMessage = "This Combination of Field A and Field B already exists";
return FALSE;
}
else
{

return TRUE;

}

}


NOTE: If you allow editing and a user can change eithier of the fields then you need to add an axtra check to ensure that when editing a record which already had a given combination, it does not report itself as already exists, like this.

// Row Updating event
function Row_Updating($rsold, &$rsnew) {
// Enter your code here
// To cancel, set return value to FALSE

$target_exists = ew_ExecuteScalar("SELECT count(*) FROM field_a where field_a = '".$rsnew["field_a"]."' and field_b = '".$rsnew["field_b"]."' and id != '".$rsold["id"]."'");


if($target_exists > 0)
{
$this->CancelMessage = "This Region Monthly Target Already Exists";
return FALSE;
}
else
{

return TRUE;

}

}
kirondedshem
 
Posts: 325
Joined: Sat Jan 28, 2017 7:13 pm

Re: Check Duplicate with two field

Postby sangnandar » Thu Jan 11, 2018 9:23 pm

If you do composite key within db, phpmaker will acknowledge it.
sangnandar
 
Posts: 283
Joined: Mon Sep 19, 2016 2:04 am

Re: Check Duplicate with two field

Postby nadimsia » Fri Jan 12, 2018 5:36 pm

Thank you kirondedshem,

I tried using your scripts, but I can still enter data even though the data on both already exists.
nadimsia
 
Posts: 28
Joined: Sun Mar 24, 2013 7:02 pm

Re: Check Duplicate with two field

Postby kirondedshem » Fri Jan 12, 2018 7:06 pm

All you have to do is to make an sql check if there exists a record with had some value for both fields A and B.

paste how you implimented your code with an example of your table schema any maybe some sample data, am sure your missing something small
kirondedshem
 
Posts: 325
Joined: Sat Jan 28, 2017 7:13 pm


Return to General Discussion (PHPMaker)