I have the following code:
<?php
// Conexion a MySQL
$mysql_link = mysql_connect( 'localhost', 'root', '010101' );
if (!$mysql_link) {
die('No se pudo conectar a la DB: ' . mysql_error());
}
$mysql_db = mysql_select_db('test', $mysql_link);
if (!$mysql_db) {
die ('No se pudo seleccionar DB: ' . mysql_error());
}
$mysql_doc_query = "INSERT INTO documents (name, wfid, docid, archivo) VALUES ('{$CodDoc}: {$documentoNombre} de {$DNI}', '{$workflowNombre}', '{$documentoNombre}', '{$archivoNombre}' );
INSERT INTO keywords (document_id, keyword, value) VALUES (LAST_INSERT_ID(), 'DNI', '{$DNI}' ), (LAST_INSERT_ID(), 'Cuit Empleador',
'{$cuitEmpleador}' ), (LAST_INSERT_ID(), 'DigitalizadoPor', '{$usuario}' ),
(LAST_INSERT_ID(), 'Direccion IP', '{$IP}' ), (LAST_INSERT_ID(), 'Ubicacion', CONCAT('pdfs/',LAST_INSERT_ID(),'.pdf') );";
// Insert en mysql
$log = fopen('/dev/shm/log.txt', 'w');
if( $log ) {
fwrite( $log, $mysql_doc_query );
}
mysql_query("START TRANSACTION");
if (mysql_query($mysql_doc_query) == TRUE)
{
mysql_query("COMMIT");
echo "\nCOMMIT!";
}
else {
mysql_query("ROLLBACK");
echo "\nROLLBACK!";
}
mysql_close($mysql_link);
fclose ($log);
?>
It’s always giving me ROLLBACK but I don’t understand why.
Any clue on this? The code generated in the log.txt archive can be executed in PHP MY ADMIN without problems. (I know the variables aren’t referenced but this is part of a larger script).
Thanks a lot.
mysql_query()only supports one statement at a time. You are executing multipleINSERTstatements in one go:This could be debugged with
mysql_error()inside yourROLLBACKblock:If you need to do two
INSERTs, you will need two separate calls tomysql_query(), and check for errors after each one. On failure of either, do yourROLLBACK.