I am putting together a shopping cart but I am facing the following error which I mentioned in the title. The method receives 2 parameter $ listProducts is an array as all purchase information and the $ Total parameter receives $ _SESSION of the total purchase value. The first insert works fine, which is the creation of the request, then I make a select to retrieve the last id of the request to insert the items of the order. But when it enters the if variable the variable $ reg does not receive the value of the query and of the following error ...
sqlsrv_fetch_array (): 11 is not a valid ss_sqlsrv_stmt resource in
What I do not understand is because it causes this error, because I do not see an error in the code, if you can help me, thank you.
public function CriarVenda($listaProdutos, $Total){
// criando venda
$sql = "INSERT INTO CADPEDIDO(PED_DATA, VALORTOTAL)VALUES(?,?)";
$params = array(date('Y-m-d'), $Total);
$query = sqlsrv_query($this->con->Conecta(), $sql, $params);
//resgatar última venda
$sql = "SELECT MAX(CODIGO) FROM CADPEDIDO";
$query = sqlsrv_query($this->con->Conecta(), $sql) or die( print_r( sqlsrv_errors(), true));
$ultimavenda = 0;
if($reg = sqlsrv_fetch_array($query)){
$ultimavenda = $reg[0];
}
foreach($listaProdutos as $p){
$sql = "INSERT INTO CADITENSPEDIDO(ITEM_PEDIDO, ITEM_COD_PEDIDO,ITEM_QTD ITEM_PRECO)VALUES(?,?,?,?)";
$params = array($ultimavenda, $p->Codigo, $p->Qtd_comprado, $p->SubTotal);
$query = sqlsrv_query($this->con->Conecta(), $sql, $params);
}
}