Forums

Validate form field in db

chuanse 08 Jul, 2015
Hi,

I am following the guideline from this thread: http://www.chronoengine.com/forums/posts/f5/t96748.html

I use a custom server side validation with this code
<?php
$db = JFactory::getDBO();
$query = "
SELECT * FROM `jos_chronoforms_data_aanvraag_parking`
(also tried: SELECT `cf_id` FROM `jos_chronoforms_data_aanvraag_parking`và
WHERE `stamnr` = '{$form->data['stamnr']}' ;
";
$db->setQuery($query);
$options = $db->loadAssocList();
if ($options) {
  return 'fail';
}else{
  return 'success';
}
?>
the db table is empty so it shouldn't find anything an perform the OnFail actions, how ever the validation keeps going to the OnSuccess actions. Any suggestions? screenshot of events attached.
chuanse 08 Jul, 2015
using the code from this thread doesn't work either :/

http://www.chronoengine.com/forums/posts/t66154/p265998/custom-server-side-validation-chronoforms-v4.html#p265998
GreyHead 08 Jul, 2015
Hi chaunse,

What are you actually trying to check here? Is it just if a record exists then the code from the other thread is better (I'm not sure what if ($options) { does in the example here):
<?php
$db =& JFactory::getDBO();
$query = "
  SELECT COUNT(*)  
    FROM `#__chronoforms_data_aanvraag_parking`
    WHERE `stamnr` = '{$form->data['stamnr']}' ;
";
$db->setQuery($query);
$count = $db->loadResult();
if ( empty($count) || $count < 1 ) {
  return 'fail';
} else {
  return 'success';
}
?>

Bob
chuanse 08 Jul, 2015
Hi Bob,

i am trying to check if there is a table row with the "stamnr" that is submitted in the new form, thus preventing a duplicate record.

More precise, the form is made to request a badge giving access to the parking lot.
The form asks for employeeid, license_plate1, license_plate2 and licenseplate3 amongst other field.

I would like to prevent duplicate entries in the db for those 4 form fields. So if the script finds a duplicate entry (OnSuccess) I have a (welcome)message saying the user already made a request for a badge followed by a show stopper. If none of the field data is found (OnFail) the form gets submitted to the db.
chuanse 08 Jul, 2015
I am getting a bit frustrated now ...😟
Even plain php is not working, I have no clue what to do...


<?php
if (!$link = mysql_connect('localhost', 'nottherealdb', 'nottherealpw')) {
    echo 'Could not connect to mysql';
    exit;
}

if (!mysql_select_db('nottherealdb', $link)) {
    echo 'Could not select database';
    exit;
}

$sql    = "SELECT COUNT(*) FROM `jos_chronoforms_data_aanvraag_parking_PVUB` WHERE `stamnr` = $_POST['stamnr']";

$result = mysql_query($sql, $link);

if (!$result) {
    echo "DB Error, could not query the database\n";
    echo 'MySQL Error: ' . mysql_error();
    exit;
}

if ($result == 0) {
  return 'success';
} else {
  return 'fail';
}
?>
chuanse 08 Jul, 2015
So message to everyone trying the above (check db for duplicate before submit)

DO NOT USE SERVER SIDE VALIDATION, USE EVENT SWITCHER!
This topic is locked and no more replies can be posted.