I need to successfully save msg only when COMMIT is run without.
The problem is with ajax return, do not donate.
In my HTML when an error occurs, it returns the catch error and ok commit
I need to start OK if the file is successfully added without any error, so I saw it exists but it was not executed because my return is like this.
"Error: SQLSTATE [HY093]: Invalid parameter number: number of bound variables does not match number of tokens "" ok "
This message does not even enter the ajax DONE. This is the problem, so no msg appears for the user, the message only stays in the console.log.
My ajax.
function sub(arquivo, acao) {
$("#load").show();
var Self = this;
if (Self.working) {
return;
}
Self.working = true;
var formData = new FormData($("#form")[0]);
formData.append("acao", acao);
formData.append("arquivo", arquivo);
jQuery.ajax({
type: 'POST',
mimeType: "multipart/form-data",
url: 'model/acao/controller.php',
dataType: 'json',
data: formData,
contentType: false,
processData: false
}).done(function (html) {
if (html == "ok") {
if (acao == 'Incluir') {
alert("Salvo com sucesso");
}
if (acao == 'Alterar') {
alert("Alterado com Sucesso");
}
} else {
alert(html);
}
}).complete(function () {
Self.working = false;
$("#load").hide();
});
}
My commit
public function commit() {
if (DB::getInstance()->commit()) {
print json_encode("ok");
}
}
My raw file is including it like this.
public function insert($table, array $dados, $bool = true) {
try {
if ($bool == true) {
$this->beginTransaction();
}
$sql = "INSERT INTO $table ({$this->colunas($table)->select}) VALUES ({$this->colunas($table)->indiceInsert})";
$stmt = DB::prepare($sql);
foreach ($dados as $key => $v) {
$stmt->bindValue(':' . $key, $v);
}
// print'<pre>';
// $stmt->debugDumpParams();
// print'</pre>';
if ($stmt->execute()) {
$cd = DB::getInstance()->lastInsertId();
return $cd;
} else {
echo json_encode("Erro ao Inserir!");
return false;
}
} catch (PDOException $e) {
echo json_encode('Error: ' . $e->getMessage());
}
}
My include
public function incluir() {
$dados = $this->dados();
$dados['dt_boletim'] = date("Y-m-d H:i:s");
if (self::insert($this->table, $dados)) {
self::commit();
return true;
}
}