Server side validation, check if a value already exists

marcoboni 15 Apr, 2014
Hi all,😀
i put in the action Custom Server Side Validation these code
<?php
   // Acquisisco il numero di telaio
   $ntelaio=$form->data['ntelaio'];   

 $controllo = "SELECT * FROM name_table WHERE ntelaio = '$ntelaio'";
 $risultato = mysql_query($controllo);

      if (mysql_num_rows($risultato) <>$ntelaio){
	  	
	$form->validation_errors['ntelaio']= "Numero di telaio con garanzia attiva";
	return false;
		 }
   
?>


but don't work very weell, because when i test it , comeback these word

Warning: mysql_query(): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in /web/htdocs/www.redmoto.it/home/rivenditori/administrator/components/com_chronoforms/form_actions/custom_serverside_validation/custom_serverside_validation.php(19) : eval()'d code on line 15 Warning: mysql_query(): A link to the server could not be established in /web/htdocs/www.redmoto.it/home/rivenditori/administrator/components/com_chronoforms/form_actions/custom_serverside_validation/custom_serverside_validation.php(19) : eval()'d code on line 15 Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in /web/htdocs/www.redmoto.it/home/rivenditori/administrator/components/com_chronoforms/form_actions/custom_serverside_validation/custom_serverside_validation.php(19) : eval()'d code on line 17



the same situation on another site joomla 2.5 and chronoform V4 work very very well😲

Thanks in advance!
Best Regards
Max_admin 16 Apr, 2014
Hi Marco,

I suggest that you change your code to use the Joomla db class:

<?php
$ntelaio=$form->data['ntelaio'];   
$database = JFactory::getDBO();
$controllo = "SELECT * FROM name_table WHERE ntelaio = '$ntelaio'";
$database->setQuery($controllo);
$risultato = $database->loadAssocList();

if (mysql_num_rows($risultato) <>$ntelaio){
$form->validation_errors['ntelaio']= "Numero di telaio con garanzia attiva";
return false;
}
   
?>


it may need 1 more fix in your "if" statement!

Regards,
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
marcoboni 16 Apr, 2014
Hi Max, thank you for your anwser.
I change the db connect, but now comeback these error
Warning: mysql_num_rows() expects parameter 1 to be resource, array given in /web/htdocs/www.redmoto.it/home/rivenditori/administrator/components/com_chronoforms/form_actions/custom_serverside_validation/custom_serverside_validation.php(19) : eval()'d code on line 17 

Thanks
King Regards
Max_admin 16 Apr, 2014
Yes, this function should be removed, but I'm not sure what you are doing in the if statement, are you trying to check if there are any results returned ? or check the result itself ?

you may try
count($risultato)
instead, also whats "<>" ???

Regards,
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
marcoboni 16 Apr, 2014
I want to check if the value is already in the table

if any -> error message this value already exists

if there is not okay, and enter the value in the table

Thank
Regards
Marco
Max_admin 16 Apr, 2014
Answer
ok, then you can use:
if(count($risultato))
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
marcoboni 17 Apr, 2014
Hi Max, now work very well, thank you so much!
...but i did undestand why if(count($risultato)).....i go to study PHP;-)
This topic is locked and no more replies can be posted.