[SOLVED] PHP validation: how to get raw data from other field in the form and use it for sql query

kaito

Member
Hi to everybody:

I am new at Fabrik (and also to proggramming, that´s why I love Fabrik). I need help with an error that has me stucked in the mud.

I have a form with 2 fields (see form.jpg attached):
- afab_cursosalumnos_id_curso , is a databasejoin element
- afab_cursosalumnos_clave_usuario , is a field element

I want to make a php validation for afab_cursosalumnos_clave_usuario element. Php validation is to compare afab_cursosalumnos_clave_usuario value versus afab_cursos_clave_curso value (that is in another table), if they are the same, validation return true and is ok.

To obtain afab_cursos_clave_curso I need to perform a sql query inside the php code (see php_validation.jpg attached) , but I need to embed in the sql consult the raw value of the first field of this form (afab_cursosalumnos_id_curso).

Form gives me validation error when I place $value into where clause, but if I comment firts sentence and replace $value with real raw data (1) from sql table (see php_validation_2.jpg attached) it works !!! So, there is something I am making wrong in the syntax but I don´t know what it is...

Please, any help will be welcome.

Ricardo
 

Attachments

  • form.JPG
    form.JPG
    47.2 KB · Views: 95
  • php_validation.JPG
    php_validation.JPG
    57.8 KB · Views: 96
  • php_validation_2.JPG
    php_validation_2.JPG
    35.2 KB · Views: 83
Hi startpoint !! Tested your suggestion.
It gives a syntax error when run: 0 syntax error, unexpected '$myDb' (T_VARIABLE), expecting ')'

These are the different trial-error options that I have tested to try to find out the correct where clause:

CASE 1: $value with single quotes
PHP:
$value = JRequest::getVar('afab_cursosalumnos___id_curso_raw');
$myDb = JFactory::getDbo();
$myQuery = $myDb->getQuery(true);
$myQuery
    ->select('clave_curso')
    ->from('afab_cursos')
    ->where('id = ' .$myDb->quote('$value'));
$myDb->setQuery($myQuery);
$clave_curso = $myDb->loadResult();
return $data == $clave_curso;
php returns false in the comparison... I think where clause is looking into id for literal "$value"


CASE 2: $value without single quotes
PHP:
$value = JRequest::getVar('afab_cursosalumnos___id_curso_raw');
$myDb = JFactory::getDbo();
$myQuery = $myDb->getQuery(true);
$myQuery
    ->select('clave_curso')
    ->from('afab_cursos')
    ->where('id = ' .$myDb->quote($value));
$myDb->setQuery($myQuery);
$clave_curso = $myDb->loadResult();
return $data == $clave_curso;
It fails and give me following error: 1054 Unknown column 'Array' in 'where clause'


CASE3: $value with single quotes + no using _raw in getVar()
PHP:
$value = JRequest::getVar('afab_cursosalumnos___id_curso');
$myDb = JFactory::getDbo();
$myQuery = $myDb->getQuery(true);
$myQuery
    ->select('clave_curso')
    ->from('afab_cursos')
    ->where('id = ' .$myDb->quote('$value'));
$myDb->setQuery($myQuery);
$clave_curso = $myDb->loadResult();
return $data == $clave_curso;
php returns false in the comparison... I think where clause is looking into id for literal "$value"


CASE 4: $value with single quotes + using curly brackets and _raw into getVar()
PHP:
$value = JRequest::getVar('{afab_cursosalumnos___id_curso_raw}');
$myDb = JFactory::getDbo();
$myQuery = $myDb->getQuery(true);
$myQuery
    ->select('clave_curso')
    ->from('afab_cursos')
    ->where('id = ' .$myDb->quote('$value'));
$myDb->setQuery($myQuery);
$clave_curso = $myDb->loadResult();
return $data == $clave_curso;
php returns false in the comparison... I think where clause is looking into id for literal "$value"


I think that the problem can be in firts sentence $value definition or in the where clause definition sentence.
I am getting mad with this nigthmare :eek::eek:
Please help !!!
 
Another little step beyond...

If I comment first $value declaration and try with a well Known value (from database=1) for $value it works , as startpoint suggested without singles quotes for $value in teh where clause declaration. See code:
PHP:
#$value = JRequest::getVar('afab_cursosalumnos___id_curso_raw');
$value=1;

$myDb = JFactory::getDbo();
$myQuery = $myDb->getQuery(true);
$myQuery
    ->select('clave_curso')
    ->from('afab_cursos')
    ->where('id = ' .$myDb->quote($value));
$myDb->setQuery($myQuery);
$clave_curso = $myDb->loadResult();
return $data == $clave_curso;

So, the problem is focused in the declaration of $value... Something in $value = JRequest::getVar('afab_cursosalumnos___id_curso_raw'); is wrong.

What is the correct syntax to retrieve raw data from other field of the form???
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top