Buen día a todos,
Mi consulta es la siguiente:
Tengo un proceso en la cual el usuario selecciona un archivo a importar (.CSV,.TXT Y .BACKUP). Cada fila usa un delimitador "|" para diferencias el valor de un campo de otro. Mi problema es que no sé si estoy usando bien las transacciones (BEGIN, ROLLBACK Y COMMIT). Ya que si bien no se registran las operaciones que están mal,
el auto_increment de mi tabla sigue aumentando por sí solo, como si en realidad registrase algo, pero no es así

Mi código es extenso, pero igual dejo una gran parte de ello.
Código PHP:
Ver original$dblink=fConectar("estadisticas");
$sEstructuraDet="INSERT INTO movement_of_vessel_det(cod_movement_of_vessel,nitem,qtty,
cod_cargo,cod_port_orig,cod_port_dest,cod_shipper,cod_receiver,cod_tracing)VALUES";
$aInsertCamposDet=array(); if($nFilaCont>=$nFilaDefaultIni){//INICIO DE IMPORTACION
$aDatos=split("[".$cmbDelimitador_vImportAJAX."]",$linea); //ID|VESSEL|GRT|OPERATION|COUNTRY (ORIG)|PORT (ORIG)|COUNTRY (DEST)|PORT
//(DEST)|ARRIVED|BERTHED|COMPLETED|AGENT|OWNER|CHARTERER|CARGO_TYPE|CARGO|QTTY|SHIPPER|RECEIVER|REMARKS
if(count($aDatos)==20){//and strlen($aDatos[0])>0 $nIDOperacion=trim($aDatos[0]); $sPortOrig=trim($aDatos[5]);$sPortDest=trim($aDatos[7]); $sArrived=trim($aDatos[8]);$sBerthed=trim($aDatos[9]);$sCompleted=trim($aDatos[10]); $sAgent=trim($aDatos[11]);$sOwner=trim($aDatos[12]);$sCharterer=trim($aDatos[13]); $sShipper=trim($aDatos[17]);$sReceiver=trim($aDatos[18]);$sRemarks=trim($aDatos[19]);
if(count($aInsertCamposDet)==0) else if(count($aInsertCamposDet)>0){ $sQueryInsertDET=$sEstructuraDet.join(",",$aInsertCamposDet); if(!$respInsertDET){
}else{
$nTotRegValid++;
}
}
$aIDOperacion[]=$nIDOperacion;$nNumItem=1;$aInsertCamposDet=array(); $sQueryIDOpetation="SHOW TABLE STATUS LIKE 'movement_of_vessel'";
$aIDOperation=faResultQuery_vImport($sQueryIDOpetation,$dblink);
$cod_movement_of_vessel=($aIDOperation['error']==true?0:$aIDOperation['Auto_increment']);
$aIDTbl[$nIDOperacion]=$cod_movement_of_vessel;//GUARDA TEMPORALMENTE EL ID AUTO EN UN ARRAY
$cod_vessel="";
if($sVessel!="-" and
strlen($sVessel)>0){ $sQueryVessel="SELECT cod_vessel,dsc_vessel FROM vessel WHERE TRIM(dsc_vessel)='".trim($sVessel)."'"; $aVessel=faResultQuery_vImport($sQueryVessel,$dblink);
$cod_vessel=($aVessel['error']==true?0:$aVessel['cod_vessel']);//echo $sVessel."->".$cod_vessel."\n";
}
$cod_operation="";
if($sOperation!="-" and
strlen($sOperation)>0){ $sQueryOperation="SELECT cod_operation,dsc_operation FROM operation WHERE TRIM(dsc_operation)='".trim($sOperation)."'"; $aOperation=faResultQuery_vImport($sQueryOperation,$dblink);
$cod_operation=($aOperation['error']==true?0:$aOperation['cod_operation']);
}
$xsArrived=fsChangeFormartDate($sArrived,"d/m/y","y-m-d");
//strlen($sArrived)>0 and
if (!preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/",$xsArrived)){ $aErr[$nContErr]['id']=$nIDOperacion;
$aErr[$nContErr]['fila']=$nFilaCont;
$aErr[$nContErr]['columna']=9;
$aErr[$nContErr]['descripcion']="(ARRIVED) El dato ingresado no es de tipo fecha";
$nContErr++;
}
$cod_agent="";
if($sAgent!="-" and
strlen($sAgent)>0){ $sQueryAgent="SELECT cod_agent,dsc_agent FROM agent WHERE TRIM(dsc_agent)='".trim($sAgent)."'"; $aAgent=faResultQuery_vImport($sQueryAgent,$dblink);
$cod_agent=($aAgent['error']==true?0:$aAgent['cod_agent']);
}
$cod_owner="";
if($sOwner!="-" and
strlen($sOwner)>0){ $sQueryOwner="SELECT cod_owner,dsc_owner FROM owner WHERE TRIM(dsc_owner)='".trim($sOwner)."'"; $aOwner=faResultQuery_vImport($sQueryOwner,$dblink);
$cod_owner=($aOwner['error']==true?0:$aOwner['cod_owner']);
}
$cod_charterer="";
if($sCharterer!="-" and
strlen($sCharterer)>0){ $sQueryCharterer="SELECT cod_charterer,dsc_charterer FROM charterer WHERE TRIM(dsc_charterer)='".trim($sCharterer)."'"; $aCharterer=faResultQuery_vImport($sQueryCharterer,$dblink);
$cod_charterer=($aCharterer['error']==true?0:$aCharterer['cod_charterer']);
}
$cod_movement_of_vessel=(isset($aIDTbl[$nIDOperacion])?
$aIDTbl[$nIDOperacion]:0);
$sQueryInsertCAB="INSERT INTO movement_of_vessel(cod_movement_of_vessel,cod_vessel,cod_operation,arrived,
berthed,completed,cod_agent,cod_owner,cod_charterer,remarks,record_date,record_cod_user,
active)VALUES("
.(strlen($cod_movement_of_vessel)>0?
$cod_movement_of_vessel:"NULL")."," .(strlen($cod_vessel)>0?
$cod_vessel:$cod_vessel)."," .(strlen($cod_operation)>0?
$cod_operation:"NULL").",";
if (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/",$xsArrived)) $sQueryInsertCAB.=(strlen($sArrived)>0?
"'".$xsArrived."'":"'".$xsArrived."'").",";
if (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/",$xsBerthed)) $sQueryInsertCAB.=(strlen($sBerthed)>0?
"'".$xsBerthed."'":"NULL").","; else if($sBerthed=="-" or $sBerthed=="")
$sQueryInsertCAB.="0000-00-00,";
if (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/",$xsCompleted)) $sQueryInsertCAB.=(strlen($sCompleted)>0?
"'".$xsCompleted."'":"NULL").","; else if($sCompleted=="-" or $sCompleted=="")
$sQueryInsertCAB.="0000-00-00,";
$sQueryInsertCAB.=(strlen($cod_agent)>0?
$cod_agent:"NULL")."," .(strlen($cod_owner)>0?
$cod_owner:"NULL")."," .(strlen($cod_charterer)>0?
$cod_charterer:"NULL")."," .(strlen($sRemarks)>0?
"'".$sRemarks."'":"NULL")."," ."CURRENT_DATE(),".$arrayDatosUser[0].",1)";
if(!$respInsertCAB ){
//echo $sQueryInsertCAB."\n".mysql_error($dblink);
$aErr[$nContErr]['id']=$nIDOperacion;
$aErr[$nContErr]['fila']=$nFilaCont;
$aErr[$nContErr]['columna']="ALL";
$aErr[$nContErr]['descripcion']="Los datos de importación ya han sido registrados.<br/>
Ref: Los campos VESSEL, ARRIVED Y OPERATION no pueden duplicarse.";
$nContErr++;
}
}
}//FIN CAB OPERACIONES
$cod_cargo="";
if($sCargo!="-" and
strlen($sCargo)>0){ $sQueryCargo="SELECT cod_cargo,dsc_cargo FROM cargo WHERE TRIM(dsc_cargo)='".trim($sCargo)."'"; $aCargo=faResultQuery_vImport($sQueryCargo,$dblink);
$cod_cargo=($aCargo['error']==true?0:$aCargo['cod_cargo']);
}
$cod_port_orig="";
if($sPortOrig!="-" and
strlen($sPortOrig)>0){ $sQueryPort="SELECT cod_port,dsc_port FROM port WHERE TRIM(dsc_port)='".trim($sPortOrig)."'"; $aPortOrig=faResultQuery_vImport($sQueryPort,$dblink);
$cod_port_orig=($aPortOrig['error']==true?0:$aPortOrig['cod_port']);
}
$cod_port_dest="";
if($sPortDest!="-" and
strlen($sPortDest)>0){ $sQueryPort="SELECT cod_port,dsc_port FROM port WHERE TRIM(dsc_port)='".trim($sPortDest)."'"; $aPortDest=faResultQuery_vImport($sQueryPort,$dblink);
$cod_port_dest=($aPortDest['error']==true?0:$aPortDest['cod_port']);
}
$cod_shipper="";
if($sShipper!="-" and
strlen($sShipper)>0){ $sQueryShipper="SELECT cod_shipper,dsc_shipper FROM shipper WHERE TRIM(dsc_shipper)='".trim($sShipper)."'"; $aShipper=faResultQuery_vImport($sQueryShipper,$dblink);
$cod_shipper=($aShipper['error']==true?0:$aShipper['cod_shipper']);
}
$cod_receiver="";
if($sReceiver!="-" and
strlen($sReceiver)>0){ $sQueryReceiver="SELECT cod_receiver,dsc_receiver FROM receiver WHERE TRIM(dsc_receiver)='".trim($sReceiver)."'"; $aReceiver=faResultQuery_vImport($sQueryReceiver,$dblink);
$cod_receiver=($aReceiver['error']==true?0:$aReceiver['cod_receiver']);
}
$cod_movement_of_vessel=(isset($aIDTbl[$nIDOperacion])?
$aIDTbl[$nIDOperacion]:0); //INSERT DETALLE
$aInsertCamposDet[]="(
".(strlen($cod_movement_of_vessel)>0?
$cod_movement_of_vessel:"NULL")."," ."".$nNumItem.",".$fQtty.","
.(strlen($cod_cargo)>0?
$cod_cargo:"NULL")."," .(strlen($cod_port_orig)>0?
$cod_port_orig:"NULL")."," .(strlen($cod_port_dest)>0?
$cod_port_dest:"NULL")."," .(strlen($cod_shipper)>0?
$cod_shipper:"NULL")."," .(strlen($cod_receiver)>0?
$cod_receiver:"NULL").",NULL)"; $nNumItem++;
}else{
$aErr[$nContErr]['id']=$nIDOperacion;
$aErr[$nContErr]['fila']=$nFilaCont;
$aErr[$nContErr]['columna']="ALL";
$aErr[$nContErr]['descripcion']="La fila debe de contar con 20 Columnas para la importación<BR>REF:Asegúrese que en los
valores de las celdas no usen como primer carácter \" o '";
$nContErr++;
}
}
$nFilaCont++;
}
if(count($aInsertCamposDet)>0){ $sQueryInsertDET=$sEstructuraDet.join(",",$aInsertCamposDet); if(!$respInsertDET){
}else{
$nTotRegValid++;
}
}
De antemano gracias por darle una chequeadita.
IMPORTANTE: La importación se realiza en 2 tablas movement_of_vessel (CABECERA) y movement_of_vessel_det (DETALLE),si un registro del detalle esta mal debe de cancelarse sólo esa operación no de las demás.Con esta variable diferencio una operación de otra
$nIDOperacion=trim($aDatos[0]);
Saludos