INSERT with prepare does not work

Asked

Viewed 175 times

2

I am trying to insert data into the database using mysqli prepare more are not inserting the data, and shows no error whatsoever

Questions:::

  • What am I doing wrong in this code below?
  • That’s the best way to do it?
  • Could they set an example?

    $conecta = new mysqli($hostname, $username, $password, $dbdatabase);
    if($conecta->connect_error){
    echo "Conexao:<span class=\"ls-tag-danger\">Erro!</span>";
    }else{ 
    echo "Conexao:<span class=\"ls-tag-success\">OK!</span>";
    }
    
    
    $sql = $conecta->prepare("INSERT INTO cotacao (chave,id_transfer,id_empresa,nome) 
    VALUES (?,?,?,?)");  
    $sql->bind_param('s', $chave,$id_transfer,$idc,$nome); //    
    $sql->execute();
    $sql->close();
    $conecta->close();
    

Thank you

3 answers

1


The error happens in your method bind_param, the method tells SQL what is the type of data being sent to the query and it is mandatory to have one for each parameter you send in the bind. The s means that the data is of the string type, i integer, d double and the b blob.

 $sql = $conecta->prepare("INSERT INTO cotacao (chave,id_transfer,id_empresa,nome) 
    VALUES (?,?,?,?)");  
    $sql->bind_param('iiis', $chave,$id_transfer,$idc,$nome); //Supondo que a chave é um integer, se não ela também recebe um "s" de string    
    $sql->execute();
    $sql->close();
    $conecta->close();
  • 1

    OK it worked, I also checked if variable $name is empty the Insert does not work. knows the reason?

  • 1

    @Fabiohenrique It may be because in the bank she is marked as Not null.

  • 1

    That even worked thanks

1

The bind_param who is wrong/or missing the other parameters, because, for each item has to have its type set, example in your code:

$sql = $conecta->prepare("INSERT INTO cotacao (chave,id_transfer,id_empresa,nome) 
VALUES (?,?,?,?)");  
$sql->bind_param('iiis', $chave,$id_transfer,$idc,$nome);

that is, the declaration of the other types in that are 4.

The guys are:

+-----+-------------------------------------------------------------------------------+
|  i  | corresponde a uma variável de tipo inteiro                                    |
+-----+-------------------------------------------------------------------------------+
|  d  | corresponde a uma variável de tipo double                                     |
+-----+-------------------------------------------------------------------------------+
|  s  | corresponde a uma variável de tipo string                                     |
+-----+-------------------------------------------------------------------------------+
|  b  | corresponde a uma variável que contém dados para um blob e enviará em pacotes |
+-----+-------------------------------------------------------------------------------+
  • That’s right... but I also checked if variable $name ,$etc.. is empty the Insert does not work. has to let pass. in the old Insert passed

  • 1

    @Fabiohenrique will depend on how it is configured in your table, if this field is mandatory then SQL has to have value, if it does not break your rule imposed in this field, in my opinion if you think that field should not be mandatory, goes in the field in this table and lets accept values null !!! that will solve this issue.

  • 1

    That even worked thanks

0

$sql = "INSERT INTO cotacao SET chave = ?, id_transfer = ?, id_empresa = ?, nome = ?";
$query = $mysqli->query($sql);

Try this, don’t forget to pass the array parameters, or use normal even.

  • da esse erro Fatal error: Call to a Member Function bind_param() on string in

Browser other questions tagged

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