Good day,
So I’ve been using eForm2db in some of my MODx forms. However, when I work with form fields with the same name (duplicated fields), is confusing to get it’s values and insert them in DB table.
Here’s an example:
&
<?php function insertPensum( $fields ) {
global $modx, $table_prefix;
$tbl_carrera = $table_prefix.'carreras';
$tbl_catedra = $table_prefix.'catedras';
define('IDSTAT',1);
// Asignacion de fields para carreras
$dbTable = array();
$dbTable[id_carrer] = NULL;
$dbTable[id_stat] = IDSTAT;
$dbTable[id_uni] = $fields[id_uni];
$dbTable[carrer_descp] = $fields[carrer_descp];
// Insercion de datos en tabla de carreras
$dbQuery = $modx->db->insert($dbTable, $tbl_carrera); //This query works perfectly.
//Here I search the recent inserted ID.
$allFields = $modx->db->select('id_carrer', $tbl_carrera);
$rowSize = $modx->db->getRecordCount($allFields);
if($rowSize > 1) {
$i=1;
while($row = $modx->db->getRow($allFields)) {
$i++;
if($i == $rowSize) $lastID = $row['id_carrer'];
}
} elseif($rowSize == 1) {
$row = $modx->db->getRow($allFields);
$lastID = $row['id_carrer'];
} else echo 'No hay carreras insertadas. Por favor verifique el insert de carreras en el snippet';
//These fields are the duplicated ones. Is basicatly a form to insert
$idMat = array(implode(',', $fields[id_mat]));
$matDesc = array(implode(',', $fields[mat_desc]));
$matSem = array(implode(',', $fields[mat_sem]));
$matCredit = array(implode(',', $fields[mat_credit]));
$matBasico = array(implode(',', $fields["mat_basico"]));
$fldSize = count($idMat);//I don't know if this is right: Here I get the length of array.
for($i=0; $i <= $fldSize; $i++) {
if($i == ($fldSize-1)) {
$fldOuput .= '('.$idMat[$i].', '.IDSTAT.', '.$lastID.', `'.$matDesc[$i].'`, '.$matSem[$i].', '.$matCredit[$i].', '.$matBasico[$i].')';
} elseif(($i == $fldSize) || (!$idMat[$i])) {
$fldOuput .= '';
} else $fldOuput .= '('.$idMat[$i].', '.IDSTAT.', '.$lastID.', `'.$matDesc[$i].'`, '.$matSem[$i].', '.$matCredit[$i].', '.$matBasico[$i].'), ';
}
//Inserte the data in the table
$dbQuery = $modx->db->query('INSERT INTO '.$tbl_catedra.' (id_mat, id_stat, id_carrer, mat_desc, mat_sem, mat_credit, mat_basico) VALUES '.$fldOuput);
return true;
}
?>
The following is the error MODx gives back, when I tried to submit 2 subjects:
« MODx Parse Error »
MODx encountered the following error while attempting to parse the requested resource:
« Execution of a query to the database failed - Column count doesn’t match value count at row 1 »
SQL: INSERT INTO ugmodx_catedras (id_mat, id_stat, id_carrer, mat_desc, mat_sem, mat_credit, mat_basico) VALUES (12001114,12001213, 1, 2, `Matemática I,Computación Básica I`, 1,1, 4,3, 1)
The form chunk. I duplicate the fields by the jQuery append function:
<h2>Nuevo pensum</h2>
<form method="post" action="[~[*id*]~]" id="newPensum" name="newPensum">
<fieldset>
<h4>Sede y carrera</h4>
<input name="newPensum" type="hidden" value="NewPensum" />
<div id="Labels"><label for="id_uni">Sede:</label></div>
<select name="id_uni" id="id_uni" tabindex="0" eform="Sede::1:Eliga una sede de la carrera:#SELECT id_uni FROM {PREFIX}sedes WHERE id_stat=1">
[[getUni2Options]]
</select><br />
<div id="Labels"><label for="carrer_descp">Carrera:</label></div>
<input type="text" name="carrer_descp" id="carrer_descp" size="40" tabindex="1" eform="Carrera:string:1:Los numeros y simbolos no son aceptados:" /><br />
<hr />
<h4>Catedras</h4>
<table cellpadding="0" cellspacing="0" id="Catedras">
<tr>
<th><label for="id_mat[]">Código</label></th>
<th><label for="mat_basico[]">Básica</label></th>
<th><label for="mat_desc[]">Catedra</label></th>
<th><label for="mat_sem[]">Semestre</label></th>
<th><label for="mat_credit[]">Créditos</label></th>
</tr>
<tr id=\"Catedra\">
<td><input type="text" name="id_mat[]" id="id_mat" size="10" maxlength="10" tabindex="2" eform="Cod. Catedra:string:1:Solo numeros aceptados:" /></td>
<td><input type="checkbox" name="mat_basico[]" id="mat_basico" value="1" tabindex="3" /></td>
<td>
<input type="text" name="mat_desc[]" id="mat_desc" size="25" tabindex="4" eform="Catedra:string:1:Simbolos especiales no aceptados:" />
</td>
<td><input type="text" name="mat_sem[]" id="mat_sem" size="5" maxlength="2" tabindex="5" eform="Semestre:string:1:Letras y simbolos no son aceptados:" /></td>
<td><input type="text" name="mat_credit[]" id="mat_credit" size="5" maxlength="2" tabindex="6" eform="Creditos:string:1:Letras y simbolos no son aceptados:" /></td>
</tr>
</table>
</fieldset>
<span><a href="#" onclick="return false;" id="otraCatedra">Añadir Catedra</a></span>
<input name="Reset" type="reset" value="Cancelar" />
<input name="Submit" type="submit" value="Aceptar" />
</form>