Solved - Preventing Duplicates Entries per User
Posted: 2020-02-14 20:40
I needed to limit user records to 10 and prevent duplicate entries for that specific user. Here's how I modified the hooks file for the table in question:
I still haven't figured out how to present custom error messages but at least it's working. If anybody else is stuck with this issue I hope this helps!
Code: Select all
function tableMain_before_insert(&$data, $memberInfo, &$args) {
$mi = getMemberInfo();
$recordCount=sqlvalue("SELECT count(1) FROM `membership_userrecords` where memberID='".$mi['username']."' and tableName='tableMain'");
$lookupIDCompare = $data['lookupID'];
$maintablename = 'tableMain';
$countReturn = TRUE;
// check the current value submitted and compare vs query results, or if query results > 0 then it's a duplicate.
$duplicateCheck = sqlvalue("SELECT count(1) from lookupTable WHERE ID = $lookupIDCompare AND ID in (SELECT lookupID FROM membership_userrecords JOIN tableMain ON membership_userrecords.pkValue = tableMain.ID WHERE tableMain.ID IN (SELECT pkValue from membership_userrecords WHERE memberID='".$mi['username']."' AND tableName = '$maintablename' ))");
if ($duplicateCheck > 0) {
$countReturn = FALSE; }
// limit entries to 10
if ($recordCount > 9) {
$countReturn = FALSE; }
return $countReturn;
// return TRUE; (default value)
}