Checking for PDO commit

Asked

Viewed 34 times

0

I need to send a successful save only when COMMIT runs without.

the problem is in return ajax, no done.

in my HTML when an error occurs, it returns the catch error and commit ok

I need to print the OK only if the file is successfully included without any error, so I saw it exists the commit 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 is only 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 crud file is with include so.

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 including

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;
        }
    }
  • Very calm at this time, your php always sends a json and in javascript vc always compares a string, need to turn it into a json and compare the values correctly.

  • @You can make an example?

  • It’s something like this, var res = JSON.parse(html); if(res.ok){ alert('insert ok'); } in the doubt of a console.log(res). Prefer to be consistent, for example always send in json an attribute called msg the value of it can be an error or success, pq in case of error o ok there won’t be there you have two problems ;)

  • When to the Invalid parameter number you know how to solve?

  • @rray I’m trying to solve but did not succeed.

  • Invalid parameter number happens when you have a value more than one column or placeholder more or less. Printing sql helps you detect where the failure is.

  • 1

    I already solved the sql vlw part.

  • @rray the problem is that it does not enter the done it falls straight into the log return fail ({"error":"Error: SQLSTATE[HY093]: Invalid Parameter number: number of bound variables does not match number of tokens"}{"Success":"ok"}) I am leaving the error only to try to show the msg on the user screen.

  • Has not .fail() in the question, you made the JSON.parse()?

Show 4 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.