Forums

How to disable double DB-inserts

mznbg 18 Jul, 2015
Hi,
I`m new to use chronoforms, it seemd to be a very usefull component, but I have some problems to use it right. I saw the tutorials so I could understand a litte bit.
But now I don`t know how I can do that there are no double DB-inserts:
My form is very simple - 2 text-fields (Place and Name) and one submit button.
My setup is:
"On load" -> Html render form

"On submit" -> DB read ->
"On found" -> "Display message" (no insert, Name in database)
"On not found" -> "DB save" & "Display message" (in database saved)

I don`t know how i must config the "DB read"
My table have follow structur: id, uniq_id, place, name

My config in the moment is:
Label for this action : is empty
Enabled: Yes
Table name: x111x_placename
Multi read: Yes
Enable Model ID: Yes
Model ID: is empty
Fields: name
Order: is empty
Group: is empty
Conditions: is empty


I will say: when the name in the form is in the database than there is no saving, output only the message.
But when the name is not in the database than is should insert to the database.

(Is in the ChronoForms Book such cases exactly explained an is there are a german language version, because englisch is hard for me)

Thanks in advance
GreyHead 21 Jul, 2015
Answer
Hi mznbg,

I would use custom PHP in an Event Switcher action to check if the value exists. You would set the Event Switcher up with a single event called 'save' then add code like this:
<?php
$db = \JFactory::getDBO();
$query = "
    SELECT COUNT(*)
        FROM `#__placename`
        WHERE `name` = '{$form->data['name']}' ;
";
$db->setQuery($query);
$count = $db->loadResult();
if ( $count < 1 ) {
  return 'save';
} 
?>
and add your DB Save action to the Save event of the Event Switcher.

I'm afraid that the book only exists in English and is now fairly out of date as it was written for ChronoForms v3.

Bob
mznbg 21 Jul, 2015
Hi Bob,

thanks, It works perfect.😀

mznbg
This topic is locked and no more replies can be posted.