I want to do a transaction with php and my sql with PDO, the thing is that when i use the commit() function, the code returns a "FALSE" echo, but insert the data anyway. Maybe im doing something wrong, my code is this:
In the Connection class
protected function getConexion()
{
try
{
$params = array(PDO::ATTR_PERSISTENT=>true,PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8");
$this->conexion = new PDO($this->motor.":host=".self::$servidor.";dbname=".$this->db_name,self::$usuario,self::$password,$params);
return $this->conexion;
}
catch (PDOException $ex)
{
echo "Error en la conexión : " . $ex->getMessage();
}
}
and into my register function
if($miembro == "S")
{
try
{
$this->getConexion()->beginTransaction();
$sqlInsertUsuario = "INSERT INTO Usuario (apodo, password, correo, nombre, apellido, "
. "fchNacimiento, sexo, fchCreacion, ultimaSesion, ip, llave, activo, "
. "imgPerfil, nivel)"
. " VALUES (:apodo, :password, :correo, :nombre, :apellido, "
. ":fchNacimiento, :sexo, NOW(), NOW(), 'Ip aqui', :llave, '1', "
. "'imagen aqui', 'I')";
$sqlInsertCandidato = "INSERT INTO Candidato (correo) VALUES (:correo)";
$sentencia = $this->getConexion()->prepare($sqlInsertUsuario);
$sentencia->execute(array(':apodo' => $apodo, ':password' => $clave, ':correo' => $correo,
':nombre' => $nombre, 'apellido' => $apellido, 'fchNacimiento' => date($fchNacimiento),
':sexo' => $sexo, ':llave' => $llave));
$sentencia = $this->getConexion()->prepare($sqlInsertCandidato);
$sentencia->execute(array(':correo' => $correo));
$this->getConexion()->commit();
echo 'TRUE';
}
catch (Exception $ex)
{
$this->getConexion()->rollBack();
echo 'FALSE';
}
}
the problem is that returns "FALSE" but the data is inserted in the table anyway.