Solved - Preventing Duplicates Entries per User

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
Post Reply
khouse
Posts: 11
Joined: 2019-05-12 14:34

Solved - Preventing Duplicates Entries per User

Post by khouse » 2020-02-14 20:40

:idea: 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:

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)
	}
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!

pböttcher
AppGini Super Hero
AppGini Super Hero
Posts: 711
Joined: 2018-04-01 10:12

Re: Solved - Preventing Duplicates Entries per User

Post by pböttcher » 2020-02-14 22:41


khouse
Posts: 11
Joined: 2019-05-12 14:34

Re: Solved - Preventing Duplicates Entries per User

Post by khouse » 2020-02-14 23:34

Thanks! I forgot to note that this fix is used when you have a lookup table and want to use the unique ID to check for a duplicate instead of the value string.

Post Reply