Binding with PHP and Postgresql

Asked

Viewed 82 times

1

Good people, I’m having a problem making an insert in the postgresql database, this is my insert:

INSERT INTO dynfipemarca (codigomarca,marca,tipo, OID, fgenabled, fgsystem , nrversion) VALUES 
                               (:codigomarca,'" . pg_escape_string(utf8_encode($nomeMarca)) . "',:tipo, '" . $codigoMarca . "' , 1 , 0, 0)

I receive back end data from an API, and insert dynamically, here’s my Binding:

$cadastrarMarca     = conectar()->prepare($sql);
        $cadastrarMarca->bindValue(":codigomarca",$codigoMarca,PDO::PARAM_INT);
        $cadastrarMarca->bindValue(":marca", $nomeMarca);
        $cadastrarMarca->bindValue(":tipo",$tipo);
        $cadastrarMarca->execute();

and the mistake that’s coming to me is this

PHP Warning: Pdostatement::bindValue(): SQLSTATE[HY093]: Invalid Parameter number:tag

i identified where the error is, $register?

  • Where is the parameter :marca in SQL?

  • is that actually in Insert where I enter the value of the mark, I was doing with :mark, but started to error when inserting a mark with accent, then I changed and put the function pg_escape_string and utf8_encode, as it is there in the example. Then gives the error in this parameter, I can do the encoding directly in the parameter?

  • In this SQL: INSERT INTO dynfipemarca (codigomarca,marca,tipo, OID, fgenabled, fgsystem , nrversion) VALUES (:codigomarca,'" . pg_escape_string(utf8_encode($nomeMarca)) . "',:tipo, '" . $codigoMarca . "' , 1 , 0, 0) has the parameter :tipo and the parameter :codigomarca, why don’t you put the :marca also while doing the bind his?

  • As I explained, it started to make a mistake to insert accents, you know? then I removed the parameter, and displayed what comes from the back end within the encoding functions

  • So why do the bind of a parameter you removed? Remove the line: $cadastrarMarca->bindValue(":marca", $nomeMarca);

  • Hummm Beauty Laércio, in the case as I am inserting what comes from the back end and is already set the value does not need to bind, I understand. Removed and worked, thanks.

  • You tried to keep the parameter :marca and leave SQL as INSERT INTO dynfipemarca (codigomarca,marca,tipo, OID, fgenabled, fgsystem , nrversion) VALUES (:codigomarca,'" . pg_escape_string(utf8_encode(:marca)) . "',:tipo, '" . $codigoMarca . "' , 1 , 0, 0)? That way I could keep the bind.

  • yes I tried, but stopped inserting the marks, then I do another test and see better what happens

  • Arrow up in the answer?

Show 4 more comments

1 answer

1


The error is being shown because the parameter :marca is not in your SQL.

You can remove the line below that will solve the problem.

$cadastrarMarca->bindValue(":marca", $nomeMarca);

Browser other questions tagged

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