Insert with lastInsertId() error

Asked

Viewed 52 times

0

The code below only inserts in the requested table, already in the requested product has_does not insert. What error ?

 public function criarPedido(){

        $consulta = 'INSERT into pedido(data, valorTotal, cliente_id) values(:data,:valorTotal,:clienteId)';

        $stmt = $this->PDO->prepare($consulta);


        $stmt->bindParam(':data', $this->data);
        $stmt->bindParam(':valorTotal', $this->valorTotal);
        $stmt->bindParam(':clienteId', $this->clienteId);

        $stmt->execute();

        echo "Produto cadastrado.";


        /////////////////////////////////////// produto_has_pedido /////////////////////////////////////////////////

        $consulta2 = 'INSERT into produto_has_pedido(produto_id, pedido_id) values('.$this->produto_id.', '.$this->PDO->lastInsertId().')';

        $stmt2 = $this->PDO->prepare($consulta2);




        $stmt2->execute();


}
  • Also publish which error msg displayed and which line! So it is easier to help you!

  • The problem is this, no error appears, it simply does not insert in the product table_has_request

  • Try to use print_r($this->PDO->errorInfo()); to assess the error!

  • what are the table fields, pk, fk? what values you are trying to insert? wouldn’t that be a duplicate pk problem? vc confiriu esses valores '.$this->produto_id. ', '.$this->PDO->lastInsertId(). ' pq pode ser null...

  • Foreign key is requested

  • an echo at these values '.$this->producto_id. ', '.$this->PDO->lastInsertId(). ' to see if they are not empty

  • It correctly returns the product id and last order id

Show 2 more comments
No answers

Browser other questions tagged

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