Check if a Database Record Already Exists Before Inserting a New Entry

Thanks for your help !

I tried what you described.
The logic didn’t work in the beginning because I had checked the “debug” option in the query.

But now everything works fine.
Both with {{transactionUnicity}} or {{transactionUnicity.length}} as a condition.

2019-10-04_1803

1 Like

can this be made to work when we are to insert bulk records not a single record?

Just drop the condition section into a repeat based on the initial query, should work fine.

1 Like

Yeah, this is possible. But of we have, say, a 10,000 records. This would be considerably slow given the looping that’ll have to be done.

Then I would suggest you use a custom query using “Insert on Duplicate key update” like this:

INSERT INTO mytable ( id, field_name)
VALUES
(Value1, Value2)
ON DUPLICATE KEY UPDATE field_name =Value2;

3 Likes

On step 14, in the message field, is there a way to put a key (instead of a text string for the error) that will be evaluated dynamically on the front end ?

I’m trying to localize completely my applications.
When I need to display error messages on the front end directly, it works just fine.

For instance, to validate a required field, I would add on my input :
dmx-bind:required=“true” dmx-bind:data-msg-required="{{translation.value.main_login__missing_login_label}}"

But I’m stuck on this one, to pass the error key from server connect to the front end and then make the front end load the key.
All variants of key, {{key}} or dmx.parse(key) I could try in step 14 failed so far.

I’m not even sure if this is supported ? :slight_smile:

Sidenote : In case someone else runs into the “error not displaying on frontend” problem after following this tutorial, head here for the solution :

Hello! Happy new year! How do I show the validation message on a notification? I used the “if invalid” but when i put, “show last error message” the notification says [Object object]

Just to try and clear up some confusion with this, for people that are not seeing the validation error message after performing the steps in this tutorial, it is more than likely because you have not added any type of client side form validation.

In other words if my client side form was built with no validation set to any field at all, and i performed the steps above, no error message would show at all, because the head of your document needs the dmxValidator.css and dmxValidator.js otherwise it will not work.

Try adding a client side validation to any field, such as required, and try again, and the error should display as expected.

1 Like

Just some security/privacy good practice.

Depending on how sensitive your app is you may not want to inform the browser user if an email exists in the database. This information can be exploited in several ways(spam, brute force attacks, etc).

You may however want to do something yourself with that information like sending a message to the email entered(if it exists in database) informing that there has been an attempt of registering their email again and send them a password reset link(in case it’s the legitimate user) and/or adding information about the person that tried to do register (ip address, browser, location, etc).

So for other fields and other use cases this might a very good approach. Just be careful what information you provide to strangers :slight_smile:

2 Likes

JonL, good suggestion.

Then, how can I capture the server side validation output? As far as I understand it only has a message option as output.

Even if I activate its output, I cannot see how to capture it in a variable to be used in a conditional step (i.e. if true send email - else … ).

Thanks.

Mag

Th validation component is for validating input only. If you want to perform extended operations then I suggest you use a query instead and query the parameter. You can then perform conditional actions or return information back to app connect.

3 Likes

Can a link be used in the message. If so, what is the syntax.

It just shows the code:
This email is already registered. If you have forgotten your login info, <a href="forgot_password.php">Click Here</a>.
instead of:
This email is already registered. If you have forgotten your login info, Click Here.

Thanks,
TOm

Hi @George,

I have followed the steps in your post above to set up a validation to check and avoid duplicate records in the database. However, this is not working correctly for me. I can insert duplicate records in the database table with the validation rule set as ‘Does not Exist in Database’.

The POST request is returning Status Code: 200 in the dev tools for the duplicate records as well. The input form field has been set up with Required validation.

The setup for inserting records and validation looks like this:


I’m not sure if I’m missing a step in the setting up of validation rule. Any help or ideas of where to look would be greatly appreciated.

Hi @George,

The validation is working correctly now. The error messages are also showing when duplicate values are found in the database.

I resolved this issue by renaming the file extensions from .html to .apsx (suggested by @psweb in reference to another issue I had when setting up the login system, based on his video Full Login Procedure).

I don’t know whether there’s a way to resolve this issue by keeping the .html extensions. But if there’s any way, then it would be really helpful to know that as well.

Thank you for your continued support :slight_smile:

I doubt there will ever really be a way around this, anything that requires a server action script to run, is going to perform checks on the server side, and the only way to pass values from server side to client side is by using .aspx or .php depending on your server environment. If the validator was checking only with client side validation then you could keep the .html but it can not then look at all the database records to ensure no duplicate exists.
I really do not think there is anything that can be done, as it’s not a Wappler related problem as such, but more of a current web server technologies order of operation.

i have same issues, i did validator name both same, and have the right field name and database field. also have required in form, but still this check is not working. anyone help?

i’m a validating the email not exist check, i have email field same as the database field. also i have required and email validation on the form field. my validator name is “validateUserEmail” on both.

Note: im using nodejs.

please see the screenshot below:

Please show some screenshots like guptast did in his post, otherwise it’s very difficult to follow properly.

1 Like

updated with screenshot please check psweb. thanks for trying to help me out

No problem, can you show me the code for your email input of your form please, I want to check the input name and ID

1 Like

please see above… thanks