0
I started studying PDO these days, and I started doing a CRUD, but when I give the Insert it shows the error:
There is no active transaction
I tried to see in other related questions, but did not understand anything.. (The connection is right, because the login is working..).
Method of class Methodospdo:
static function inserirCliente($nome, $sobrenome) {
try {
$con = ConnectionFactory::getConnection();
$con->beginTransaction();
$stmt = $con->prepare("INSERT INTO pessoa(nome, sobrenome) VALUES(:nome, :sobrenome)");
$stmt->bindParam('nome', $nome);
$stmt->bindParam('sobrenome', $sobrenome);
if($stmt->execute()){
$result = 'Cadastrado com sucesso';
} else {
$result = 'Erro ao cadastrar!';
$con->rollBack();
}
$con->commit();
} catch (PDOException $e) {
echo $e->getMessage();
}
$con = null;
return $result;
}
Form:
if ($_POST) {
$nome = $_POST['nome'];
$sobrenome = $_POST['sobrenome'];
$result = MetodosPDO::inserirCliente($nome, $sobrenome);
echo $result;
}
?>
<form action="" method="post">
<input type="text" name="nome" value="" /><br>
<input type="text" name="sobrenome" value="" /><br>
<input type="submit" value="Cadastrar" name="btn" /><br>
</form>
Thank you, you’ve given me a lot of doubt about that.
– Leandro