Page 1 of 1

duplicated database insert

Posted: Sun Mar 19, 2006 8:04 am
by ademus
hi, i'm having some trouble with a script that recieves some info through a form and inserts it into the db

Code: Select all

case'tipo_aviso':
		$cant_tipos = ( mssql_fetch_row( mssql_query( 'SELECT count(tipoid) FROM md_tipo_aviso' ) ) );
		if ( ( $cant_tipos[0] ) < 10 ) {
			$cant = '10' . $cant_tipos[0];
		} else {
			$cant = '1' . $cant_tipos[0];
		}
		$tipo = $_POST['tipo'];
		$tipoid =  $cant . strtoupper( 'ta' . substr( $tipo, 0, 1 ) );
		$descripcion = $_POST['descripcion'];
		$query = ('BEGIN TRANSACTION
				   INSERT INTO md_tipo_aviso VALUES (\'' . $tipoid . '\',\'' . $tipo . '\',\'' . $descripcion . '\')
				   COMMIT TRANSACTION');
		$result = mssql_query($query,$db->dblink);
		mssql_close($db->dblink);
		ob_clean();
		header("Location: http://127.0.0.1/docek/tipo_aviso_back. ... escripcion");
		break;
after i run the script, i check my db and there are two entries with different keys ($tipoid) just as if the script had run twice.

tipoid | tipo | descripcion
***** | **** | **********
100TA1 | 1/4 pag BN | 1/4 de pagina Blanco y Negro
101TA1 | 1/4 pag BN | 1/4 de pagina Blanco y Negro

that's the result i get when i query the table after inserting what was supposed to be a single record...
thanks !!

Posted: Sun Mar 19, 2006 9:26 am
by feyd
it would appear your code does run twice in some fashion.

Do some logging with debug_backtrace()

Posted: Sun Mar 19, 2006 10:16 am
by ademus
i found out that when the form variable 'tipo', from where the $tipo and $tipoid are extracted, contains a '/' like in "1/4 de pag" the code duplicates the entry in the db. But when the form variable 'tipo' contains plain text (no slashes) the code inserts only one registry in the db.
Tried echoing $query when 'tipo' contains a slash

Code: Select all

BEGIN TRANSACTION INSERT INTO md_tipo_aviso VALUES ('108TA1','1/2 pagina','media pagina a color') COMMIT TRANSACTION
and it didn't show any strange code or stuff... :roll:
Is this a usable hint/clue or not ?

Thanks !!

Tried using debug_backtrace() but just couldn't figure out how to use the results it gives, any hint is extremely welcome

Posted: Tue Mar 21, 2006 1:30 pm
by ademus
problem still persists, and is killing me !! anybody who might be able to share a similar experience !
thanks in advance !!

:cry: