Problem with Event Switcher

How to check for duplicate database entries in ChronoForms.

Overview

The issue occurs when an Event Switcher fails to display messages due to incorrect database query logic.
Modify the Event Switcher code to use a COUNT query and ensure form data values are properly validated before the check.

Answered
TR TRIJ 08 Oct, 2014
Hello all,

i have started my first projekt with Chronoforms ... and as expected i have my first problem πŸ˜‰

I created a Form to save data to a database. That was easy. ( With this forrum ...πŸ™‚ ). Next step would be a check before saving if this data already exists in the database. I have two field with prename and surename. I want to check if there is already a combination of this in the database or not.
I found the EVENET SWITCHER. For first test i just have two Display Message event for both cases. One Returns "Data ok" the other " Data exists"

My problem is now, that there is never any Message displayed. I fear i have a error in my code so no case is occured ...
Can anybody help me ?

Thx and Greets from Germany

... my code for the event switcher is :
<?php
$db =JFactory::getDBO();
$query = "
    SELECT `id`
    FROM `#__Data_Player`
    WHERE `player_vn` = '{$form->data['player_vn']}'
    AND  `player_nn` = '{$form->data['player_nn']}'
";
$db->setQuery($query);
$num_rows = $db->getNumRows()

if ($num_rows === 0) {  
    return "fail";
}  else {
    return "success";
}
?>
ca calculus00 08 Oct, 2014
Hello TRIJ,
I'm not a Chrono professional, but after checking your new post, I think that the following links may help:
Event switcher
P.S: I'm just an automated serviceπŸ˜‰
Max_admin Max_admin 08 Oct, 2014
1 Likes
Please debug this line of code, using an "echo" statement in a "custom code" action:

$query = "
    SELECT `id`
    FROM `#__Data_Player`
    WHERE `player_vn` = '{$form->data['player_vn']}'
    AND  `player_nn` = '{$form->data['player_nn']}'
";

Does it work as expected ?

Regards,
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
TR TRIJ 08 Oct, 2014
Hi Max,

if i use an "echo" for the colplete expression i get nothing. if i insert an "echo ($query);" after the expression i get a message in form of :

SELECT `id` FROM `#__data_player` WHERE `player_vn` = 'Tim' AND `player_nn` = 'IJsselstein'

i tried a "echo ($num_rows); but i there is now message then😟

Regards
Tim
Gr GreyHead 08 Oct, 2014
Answer
1 Likes
Hi TRIJ,

Please try this version:
$db = JFactory::getDBO();
$query = "
  SELECT COUNT(*)
    FROM `#__Data_Player`
    WHERE `player_vn` = '{$form->data['player_vn']}'
      AND  `player_nn` = '{$form->data['player_nn']}' ;
";
$db->setQuery($query);
$count = $db->loadResult();
$result = 'fail';
if ( $count > 0 ) {
  $result = 'success';
}
return $result;
?>
You may also need to add some validation to check that the values in $form->data[''] exist and are set.

Bob
TR TRIJ 08 Oct, 2014
Many Thx to Max and Bob.

Best Regards from Germany 🀣
This topic is locked and no more replies can be posted.