-1
I intend to make a query to the database with the filter of several Ids, to return the corresponding lines. I am trying this way:
First send the ids through js:
function update_estado()
{
var pedpront = [];
$.each($("input[name='updpront']:checked"), function(){
pedpront.push($(this).val());
});
var dadosajax = {
'Id[]' : pedpront ,
'Fornecedor' : $("#Fornecedor3").val(),
'Estado' : $("#Estado1").val(),
'Observacao' : $("#Observacao").val()
}
$.ajax({
url: 'pedencom.php',
type: 'POST',
cache: false,
data: dadosajax,
error: function(){
$(".error_message").removeClass('hide');
swal("Erro!", "Tente novamente. Caso persista o erro, contatar Administrador!", "error");
},
success: function(result2)
{
$('.limp9')[0].reset();
}
});
}
Send separate Ids this way:
Id[]:23
Id[]:24
Then in php I’m trying to do the loop
in this way:
for($i=0;$i<count($Id);$i++)
{
if($Id[$i]!="")
{
$stmt1 = $conn->prepare("SELECT dados.Encomendas.Id_Enc, dados.Encomendas.Orcamento, NotaEnco, dados.Encomendas.Fornecedor AS IdForn,
dados.NovoForn.Fornecedor, Ref, Designacao, Quantidade, Observacao
FROM dados.Encomendas LEFT OUTER JOIN dados.Orcamento ON dados.Orcamento.Id = dados.Encomendas.Id_Enc
LEFT OUTER JOIN dados.NovoForn ON dados.NovoForn.Id = dados.Encomendas.Fornecedor
LEFT OUTER JOIN dados.EncEmail ON dados.EncEmail.IdEnc = dados.Encomendas.Id_Enc WHERE dados.Encomendas.Id_Enc IN (:Id)
ORDER BY dados.NovoForn.Fornecedor ASC");
$stmt1->bindValue(':Id', $Id[$i], PDO::PARAM_INT);
$stmt1->execute();
}
}
while($row1=$stmt1->fetch(PDO::FETCH_ASSOC)){
$Orcamento = $row1["Orcamento"];
$Ref1 = $row1["Ref"];
$Designacao1 = $row1["Designacao"];
$Quantidade1 = $row1["Quantidade"];
}
But this way it only returns one row, even if it sends more than one id through the url. Intended for example to send 3 ids, return the 3 lines corresponding to the 3 ids.
instead of making this loop pq you do not give an implode in the ids and passes them in the IN (ids)
– Lucas Miranda
@Lucas Miranda also tried to make the implode and it didn’t work. I did so
ids = implode(" ,", $Id)
and then I putWHERE dados.Encomendas.Id_Enc IN (:ids)
and it didn’t work. TheIds
was null, but I kept thefor($i=0;$i<count($Id);$i++){
. You can put an example?– Bruno