Error message if a pair of values exist in database

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
Post Reply
fgazza
Veteran Member
Posts: 205
Joined: 2019-04-30 17:37

Error message if a pair of values exist in database

Post by fgazza » 2019-10-03 21:29

Hello!
In my table I have a name field and a last _name field.
I need to create a code that, once these fields have been filled in and before saving the record, checks the database and if the name - surname pair is already present prevents from saving the new record, empty the name and surname fields and generate an error message like "this pair name - surname is already present in the database".
Thank you!
Fabiano.

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: Error message if a pair of values exist in database

Post by pbottcher » 2019-10-07 07:17

Hi,

you can either check in the hooks/TABLNAME.php -> TABLENAME_before_insert if a record alredy exisits in the database through an SQL query and return false, with a custom error message. (see also viewtopic.php?t=3190)

or you can hook onto the two fields (firstname and lastname) and check based on change of one of the fields via an ajax call if that name is already used in the database.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

fgazza
Veteran Member
Posts: 205
Joined: 2019-04-30 17:37

Re: Error message if a pair of values exist in database

Post by fgazza » 2019-10-07 12:21

Thanks!
I am very sorry but I have difficulty understanding the code in the post you suggested to me and I understand that we need to modify the header-extra.php file?

in my table I also have a "first_last_name" field in which the name and last_name fields are concatenated in real time.

can someone help me to define the exact code to insert in the hooks / tablename.php so that if I write the name-last_name combination in the form (and the "first_last_name" field is compiled by itself linking the two previous fields as per code under reposting) are already present in the database table an error message is shown and the fields are emptied (all before saving the form).?

Here is the code to concatenate my two fields (i create a file tablename-dv.js:

function updateName() {
var name= $j( '#name' ).val(),
last_name= $j( '#last_name' ).val();
$j( '#first_last_name' ).text(last_name+' '+name);
$j( '#first_last_name' ).val(last_name+' '+name);


}

$j('#last_name,#name').on('change', function() {
updateName();
});

Thanks to those who can suggest the code to use and excuse me: I'm trying to learn from all of you.

Fabiano

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: Error message if a pair of values exist in database

Post by pbottcher » 2019-10-07 15:22

Hi,

you can try

Code: Select all

$val = sqlvalue("SELECT count(*) from TABLE where first_last_name='{$data['first_last_name']}'");
if ($val != 0) return false;
This will create the default message, that you could not insert the record.

If you want to add some custom error message you will need to go through the post viewtopic.php?f=7&t=1740&p=10906#p4709. The solution is already there.

Also I think it is not possible, if you use this option to get back to the submitted page and clear ony your name/firstname fields, as you will be redirected to the TV page. A redirect to the DV page will only happen if a record was successfully stored, as it will require the priKey to identify the data for the record.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

fgazza
Veteran Member
Posts: 205
Joined: 2019-04-30 17:37

Re: Error message if a pair of values exist in database

Post by fgazza » 2019-10-07 19:08

Thanks!

The code you suggested should be inserted in the file tablename-dv.js?

If yes, I can inserti it at the end othis code?

function updateName() {
var name= $j( '#name' ).val(),
last_name= $j( '#last_name' ).val();
$j( '#first_last_name' ).text(last_name+' '+name);
$j( '#first_last_name' ).val(last_name+' '+name);


}

$j('#last_name,#name').on('change', function() {
updateName();
});

/* here is your code! */

$val = sqlvalue("SELECT count(*) from TABLE where first_last_name='{$data['first_last_name']}'");
if ($val != 0) return false;

Or Have I to insert it in the TABLNAME.php -> TABLENAME_before_insert ?

Thank you!

Fabiano

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: Error message if a pair of values exist in database

Post by pbottcher » 2019-10-07 19:16

TABLENAME_before_insert

but you need to replace TABLE with your tablename and TABLENAME is of course your tablename as well.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

Post Reply