Forums

Control the presence of a value from form in a DB before save

Vmad 16 Feb, 2014
Hi,
I would to save data from a form field to a Database., but only if already don't exist a table with the same data. The form field should collect data of patient of a medical research... like this:

Patient code
Age
Gender
Instruction
and others

the control should be on the "Patient code" form field value... How can I do the "setup"?
Thank you
GreyHead 16 Feb, 2014
Answer
Hi Vmad,

It depends a bit on what 'the same data' means. I think that you'd need to use a Custom Code action to check if the data already exists and then use an Event Switcher action (in the Validation group) to either save or not.

Bob
Vmad 16 Feb, 2014
Hi Bob, Thank you for the advice. I already solve the problem, as you say ,using Event Switcher.

Vmad
pierpari 21 Feb, 2014
I have the same problem, but i don't understand how to use event switcher.
I have loaded data with DB reard and then into code tab of Event switch i write this:
<?php
$verifica=$form->data['EMAIL'];
foreach ($verifica as $ver){
  if($ver['email'] == $form->data['email']):
    return 'success';
    break;
  else:
    return 'fail';
  endif;
}
?>

where $form->data['EMAIL'] is the array of database data and $form->data['email'] is the value of form field.

Into Events field of Event Switch I have this of default:
success,fail


In On Success i have a message that the value exist in database.
In On fail i have DB SAVE.

it's SAVE every time😟
Why????
GreyHead 23 Feb, 2014
Hi pierpari,

Your code looks OK. The next step is for you to add some debugger code so that you can see exactly what is happening.

Bob
pierpari 23 Feb, 2014
I have solved deleting "success,fail" into Events field of Event Switch.
thanks Bob
This topic is locked and no more replies can be posted.