Serverside validation php help

freefall 28 May, 2010
Hello, Can you please help?
I am using chronoForm in a module to register for few events with the same form.
I have db connection (named jos_chronoforms_pre_register) and was able to pass fields to the table.

I found this and worked nicely in serverside validation tab.
 <?php
$db =& JFactory::getDBO();
$email = JRequest::getString('pr_email', '', 'post');
$query = "
  SELECT COUNT(*) FROM `jos_chronoforms_pre_register` WHERE `pr_email` = '$email';
";
$db->setQuery($query);
if ( $db->loadResult() ) {
  return "You are already registered with your email.";
}
?> 

But i realized user can register only once with the same email (even though there are other events that users are allowed to register)

Right now the code is saying "if email exists in the table, return error"

How do i say "if email exists in specific title, return error" in php format?

I tried SELECT COUNT(*) FROM `jos_chronoforms_pre_register` WHERE `pr_email`,'event_id' = '$email';
and many other combination, but failed.

Thank you. 😀
GreyHead 29 May, 2010
Hi freefall,

Please check the MySQL manual for correct MySQL syntax.

There isn't enough info here to give you the correct query but it will be something like
WHERE `pr_email` = '$email' AND 'event_id' = '$event_id';

Bob
freefall 29 May, 2010
Thank you Bob for helping me. It must be painful to answer newbie's question all the time.
Basically I am trying to prevent double registration for a specific event_id, and user should be able to register for other events with the same email.

My form is passing event_id&email to my table.

When i use code below, only one person can register.
Server side validation tab
<?php
$db =& JFactory::getDBO();
$event_id = JRequest::getString('event_id', '', 'post');
$query = "
  SELECT email FROM jos_chronoforms_pre_register WHERE event_id = $event_id;
";
$db->setQuery($query);
if ( $db->loadResult() ) {
  return "You are already registered for this program with your email.";
}
?>}
?>

Could you lead me to right direction? I've been working on this all day, but no luck.

thank you.
freefall 01 Jun, 2010
Can somebody help, please? ( i am a licensed owner :mrgreen: if this encourages others to get their own license.)
i search the forum, trying to find answers myself for many days, but failed.
Thank you!!!
GreyHead 02 Jun, 2010
Hi freefall,

Sorry I overlooked this.

You need to check both the event_id and the email (your code only checks the event_id so as long as one person is registered it shows up as an error).
<?php
$db =& JFactory::getDBO();
$event_id = JRequest::getString('event_id', '', 'post');
$email = JRequest::getString('email', '', 'post');
$query = "
  SELECT COUNT(`email`) 
    FROM `#__chronoforms_pre_register` 
    WHERE `event_id` = ".$db->Quote($event_id)."
      AND `email` = ".$db->Quote($email)." ;
";
$db->setQuery($query);
if ( $db->loadResult() ) {
  return "You are already registered for this program with your email.";
}
?>

Bob
freefall 02 Jun, 2010
Thank you.😀 I really appreciate your help.
freefall 13 Jun, 2010
i am pulling my hair out... please help. I'll be bald, before i can finish this project.

I thought it was working, but it is not.

Here is what i'm trying to to. Prevent double registration with the same email for one event, but should be able to register for different event with the same email.

I have event_id, email column and event_id '1' already has email [email]'test@test.com[/email]'
Now i am test - registering the same event with the same email.

 <?php
    $db =& JFactory::getDBO();
    $event_id = JRequest::getString('event_id', '', 'post');
    $email = JRequest::getString('email', '', 'post');
    $query = "
      SELECT COUNT(`email`)
        FROM `#__chronoforms_pre_register`
          WHERE `email` = ".$db->Quote($email)." AND `event_id` = ".$db->Quote($event_id).";
    ";
    $db->setQuery($query);
    if ( $db->loadResult() ) {
      return "You are already registered for this program with your email.";
    }
else if ( !$db->loadResult() ) {
  return "you can register.";
}
?>


Basically the same code you already helped me, but My form always returns, "you can register"
Can't figure out what is wrong. any help, please?
GreyHead 14 Jun, 2010
Hi freefall,

Add a line of code to display the query so that you can see what is actually being created. (Or you can turn site debug on to see all the queries.)

Your if else should just be } else { but otherwise the code looks OK.

Bob

Later:: see Fredrik's post after this. I missed that :-(
nml375 14 Jun, 2010
Hi,
Also keep in mind, that in case the validation is successful, you should not return anything from within the serverside validation, as any returned string is assumed to be an error stopping any further processing of the submitted data. Use the 'on submit' code for any 'success'-messages.

/Fredrik
freefall 14 Jun, 2010
thank you all for helping.
I wish i can think like you guys. 😲

I added echo $query; Then i see this. I can't figure out why this doesn't load any result.
Is this query saying: Count if there is [email]test@test.com[/email] exist in event_id 2?

When i use the code above without else statement , it doesn't catch double registration.

What am i doing wrong?(except i am trying to achieve more than i am capable of.. :mrgreen: )
freefall 15 Jun, 2010
Hello. I finally figure it out. It was because my query was event_id='2'
Correct query is event_id=2 (don't know why. they are all same data type, varchar)
$db->Quote was adding '' around 2.

i learned it very hard way.

when you get a chance, would you mind explaining why event_id should not have '' around it?

Thank you guys!
This topic is locked and no more replies can be posted.