Personalize SQL error for Unique field

trimarina 08 Jun, 2010
Hi Bob and Chrono Forms Community!

I've made a Form and I need it to be submited only once per joomla user.

So, I made the userid field UNIQUE in my new table (that stores the information of the chronoform).

My question is, if I can personalize the SQL error that appears when I try to submit the form more than once.

Now it looks like: "(...)store failed - Duplicate entry '69' for key 2 SQL=INSERT INTO `jos_chronoforms_(...)"

Thanks a lot!

Marina
GreyHead 08 Jun, 2010
Hi Marina,

You need to put the check at the beginning of the Form HTML to see if the user has already responded. I posted some very similar code in the last few days.

Your check is working but it's really too late by the time the form is submitted.

Bob
srthomas 15 Feb, 2011
Sorry to dig this old post up, but it same up in the search I ran and I couldn't find the code you referred to in your post.

Could you advise on how is best to prevent unique violoations occuring resulting in the same error?
GreyHead 15 Feb, 2011
Hi srthomas,

What exactly do you want to check for?

I think that the question in this thread was about editing existing data - in that case you need to make sure that the record primary key is included in the form as a hidden input. It will be something like:
<input type='hidden' name='cf_id' id='cf_id' value='<?php echo $cf_id; ?>' /> 

Bob
srthomas 15 Feb, 2011
I have a field that I have marked as UNIQUE in the table definition called "Category". Previous to changing the table definition the form allowed you to enter the same category time after time. I stopped this by setting the UNIQUE property on the column.

Now I get an error on the page after submit "store failed - Duplicate entry ..."

What do you suggest?
GreyHead 15 Feb, 2011
Hi srthomas,

I'm confused. I'd guess that you need to validate the form results to make sure that the Category is indeed unique, otherwise you'll get the error.

Bob
srthomas 15 Feb, 2011
Sorry about the confusion.

That's the bit I was hoping for some help with:

You need to put the check at the beginning of the Form HTML to see if the user has already responded. I posted some very similar code in the last few days.



Coudl you share that snippet of code?
GreyHead 15 Feb, 2011
Hi srthomas,

<?php
$user =& JFactory::getUser();
$db =& JFactory::getDBO();
$query = "
    SELECT COUNT(*)
        FROM `#__some_table`
        WHERE `cf_user_id` = '".$user->id."' ;
";
$db->setQuery($query);
$count = $db->loadResult();
if ( $count ) {
  echo "You've already completed this";
  return;
} 
?>

Bob
angie_iu 03 Mar, 2011
Hi greyhead,

I've the same problem, but the field's name I need to make unique is "codice_fiscale"
I want that this field is unique and I want to avoid double inserts, so where should I put that code you posted? and how can I persionalize that code to my needs?
Thank u so much!
GreyHead 04 Mar, 2011
Hi angie_iu,

I think that the code you need is:
<?php
$codice_fiscale = JRequest::getString('codice_fiscale', '', 'post');
$db =& JFactory::getDBO();
$query = "
    SELECT COUNT(*)
        FROM `#__some_table`
        WHERE `codice_fiscale` = '$codice_fiscale' ;
";
$db->setQuery($query);
$count = $db->loadResult();
if ( $count ) {
  echo "You've already completed this";
  return;
}
?>
angie_iu 04 Mar, 2011
thank u...but where should i insert this code? on submit code (before sending), in extra code? where?

FROM `#__some_table`



should I insert my table here?
sorry but I really don't know php code!

thanx
GreyHead 04 Mar, 2011
Hi angie_iu,

You should probalby use this version (a small change in the message display) in the Serverside validation box on the form Validation tab:
<?php
$codice_fiscale = JRequest::getString('codice_fiscale', '', 'post');
$db =& JFactory::getDBO();
$query = "
    SELECT COUNT(*)
        FROM `#__some_table`
        WHERE `codice_fiscale` = '$codice_fiscale' ;
";
$db->setQuery($query);
$count = $db->loadResult();
if ( $count ) {
  return "Questo codice fiscale è già stato inserito";
}
?>
angie_iu 04 Mar, 2011
It works!! It works!! Thank you so much...as ever!
This topic is locked and no more replies can be posted.