Forums

How to check if an ID has DB-Record?

relaxo 31 Oct, 2014
[attachment=0]cv5-db.jpg[/attachment]Hi, i have a beginner question i think. And i'm not sure what are the exact search-terms for my issue.

I use the demo-fields-events. I have registered users who should fill the form. I created a db-table and can see the entries in php-my-admin. Now i want, that everyone has just one voice. When they click again on the menu-icon he/she should see a messege, which tells that they already gave their opinions.

I try it with db-reader, picked my table and put in the on found event the display messege and show stopper. In the on not found event i put the html render form.

My problem is now, that when just one record is in database-table everyone gets the stopper-message.
So i think i need something like a id-checker that chronoforms can check if an entry of that id exists and then choose show stopper or go with the html-render?

Thank you for help!
SPABO 01 Nov, 2014
Answer
Hi,
I used in the ON SUBMIT area a CUSTOM SERVER SIDE VALIDATION ans put the following code in this:
<?php
$name = JRequest::getString('name', '', 'post');
$last = JRequest::getString('lastname', '', 'post');
if ( $name && $lastname) {
  $db =& JFactory::getDBO();
  $query = "
    SELECT COUNT(*)
      FROM `YOUR DB NAME`
      WHERE `name` = '{$name}'
        AND `lastname` = '{$lastname}';
  ";
  $db->setQuery($query);
  $count = $db->loadResult();
  if ( $count > 0 ) {
    $form->validation_errors[''] = "Sorry, you etc etc!";
    return false;
  }}
?>


For the string(s), you could take e.g. the users-id of e-mail

In the "red" part of the CUSTOM SERVER SIDE VALIDATION, you should drag the "EVENT LOOP", to stop sending and showing the error message.

But pls bear in mind, this works when the user pushes the send button, and not on opening your form!
Max_admin 01 Nov, 2014
Hi SPABO,

You could use the "conditions" box in the "db read" to find the record matching the user's id or name and in the "record found" event you can add the "event loop"!🙂

Regards,
Max
Max
ChronoForms developer...
Did you try ChronoMyAdmin for managing your Joomla database tables ?
relaxo 06 Nov, 2014
Hi, thanks for your replays.

Unfortunately, i don´t get it running. I tried it the way Spabo describes and even just adding the code in the conditions box of db-read. If i put the event loop in the record found event, like admin describes i receive an 500 error.

I tried something like this
<?php
$uniq_id = JRequest::getString('uniq_id', '', 'post');
$user_id = JRequest::getString('user_id ', '', 'post');
if ( $uniq_id&& $user_id) {
  $db =& JFactory::getDBO();
  $query = "
    SELECT COUNT(*)
      FROM `jos_chronoengine_chronoforms_datatable_demo-fields-events`
      WHERE `uniq_id` = '{$uniq_id}'
        AND `user_id` = '{$user_id}';
  ";
  $db->setQuery($query);
  $count = $db->loadResult();
  if ( $count > 0 ) {
    $form->validation_errors[''] = "Sorry, you etc etc!";
    return false;
  }}
?>


Maybe someone can give me another hint?
Thanks, Relaxo
Max_admin 06 Nov, 2014
In the conditions box:

<?php
$user = JFactory::getUser();
return array( "user_id" => $user->id );

Then you can use an "Event loop" in the "on found" to return the user to the form!

That's all is needed to check if the user already has a record or not, the user must be logged in of course.

Regards,
Max

[[> Updated to fix a couple of small typos : Bob <]]
Max
ChronoForms developer...
Did you try ChronoMyAdmin for managing your Joomla database tables ?
This topic is locked and no more replies can be posted.