Logo Hardware.com.br
JCF.ALFABR
JCF.ALFABR Novo Membro Registrado
1 Mensagem 0 Curtidas

Update vindos de arrays diferentes (PhP com MySQL).

#1 Por JCF.ALFABR 08/04/2022 - 15:09
Quero fazer atualização de uma tabela de produtos, mas somente nas quantidades e valores.
Já tentei várias opções mas só atualiza todas as linhas da tabela com base no último valor informado.

Segue os códigos, tudo em estrutura mvc:

Formulário:
<div class="table-responsive" style="border: none;margin-top: 10px;">
<table class="table table-bordered table-hover" id="tabela">
<thead class="thead-dark">
<tr>
<th style="text-align:center; width: 20px;"><h4>ID</h4></th>
<th style="text-align:center; width: 60px;"><h4>Data</h4></th>
<th style="text-align:center"><h4>Alterar Produto/Serviço</h4></th>

<th style="text-align:center"><h4>Alterar Quant</h4></th>
<th style="text-align:center"><h4>R$ Valor</h4></th>
<th width="60" style="text-align:center"><h4>Ações</h4></th>

</tr>
</thead>
<?php foreach ($orc_prod as $orc): ?>
<tr>
<td> <input type="text" name="id_orcamento[]" value="<?php echo $orc['id_orcamento']; ?>" style="text-align:center;" /> </td>
<td><input type="text" name="data_orcamento" value="<?php echo date('d/m/Y', strtotime($orc['data_orcamento'])); ?>" /></td>



<td width="250"><input type="text" name="id_produto[]" value="<?php echo $orc['nome']; ?>" /></td>



<td width="100"><input type="text" name="quant[]" value="<?php echo number_format($orc['quant'], 2, ',', '.'); ?>" /></td>
<td width="100"><input type="text" name="preco_venda[]" value="<?php echo number_format($orc['preco_venda'], 2, ',', '.'); ?>" /></td>
<td style="text-align: center;">
<?php if ($edit_permissao): ?>
<a href="<?php echo BASE_URL; ?>orcamentos/edicao_orc_prod_item/<?php echo $orc['id']; ?>"><img src="<?php BASE_URL; ?>../../assets/images/editar.png" width="20px;" title="EDITAR""/></a>
<a href="<?php echo BASE_URL; ?>orcamentos/excluir_orc_prod/<?php echo $orc['id']; ?>" onclick="return confirm('Tem certeza que deseja excluir?')"><img src="<?php BASE_URL; ?>../../assets/images/delete.png" width="20px;" title="EXCLUIR" /></a>
<?php else: ?>
<a href=""><img src="<?php BASE_URL; ?>../assets/images/search.png" width="20px;" title="VISUALIZANDO" /></a>
<?php endif; ?>
</td>

</tr>
<?php endforeach; ?>

</table>
</div>

Model: (Esta primeira parte funciona perfeito)
public function editOrcamentoProduto($produto, $data, $tipo, $pgtos, $status, $vt, $orcamento, $cliente, $quant, $preco, $id, $id_empresa) {
$sql = $this->db->prepare("UPDATE orcamentos SET id_usuario =:id_usuario, id_cliente =:id_cliente, "
. "id_tipo_venda =:id_tipo_venda, id_tipo_pgto =:id_tipo_pgto, id_status_orcamento =:id_status_orcamento, valor_orcamento =:valor_orcamento "
. "WHERE id =:id AND id_empresa =:id_empresa&quot;
$sql->bindValue(":id_usuario", $_SESSION['cLogin']);
$sql->bindValue(":id_cliente", $cliente);
// $sql->bindValue(":nr_orcamento", $nr);
$sql->bindValue(":id_tipo_venda", $tipo);
$sql->bindValue(":id_tipo_pgto", $pgtos);
$sql->bindValue(":id_status_orcamento", $status);
$sql->bindValue(":valor_orcamento", $vt);
$sql->bindValue(":id", $id);
$sql->bindValue(":id_empresa", $id_empresa);
$sql->execute();


Essa outra é que esta dando problema, só atualiza todos os itens com o "for", mas aí assume somente o último valor da linha para todos os update.
/*
foreach ([$produto, $orcamento, $quant, $preco] as list($pd, $oc, $qt, $pv)) {
$sqlp = $this->db->prepare("SELECT id_produto,id_orcamento, quant, preco_venda FROM orcamento_produtos&quot;

$sqlp->bindValue(":id_produto", $pd);
$sqlp->bindValue(":id_orcamento", $oc);
$sqlp->bindValue(":quant", $qt);
$sqlp->bindValue("reco_venda", $pv);


if ($sqlp->rowCount() > 0) {
$row = $sqlp->fetch();

$pd = $row['id_produto'];
$oc = $row['id_orcamento'];
$qt = $row['quant'];
$pv = $row['preco_venda'];
*/

for ($i = 0; $i < count($produto); $i++){
$sqlp = $this->db->prepare("UPDATE orcamento_produtos SET id_usuario =:id_usuario,"
. "quant =:quant, preco_venda =reco_venda&quot;
$sqlp->bindValue(":id_usuario", $_SESSION['cLogin']);

$sqlp->bindValue(":quant", $quant[$i]);
$sqlp->bindValue("reco_venda", $preco[$i]);
//$sqlp->bindValue(":id_produto", $pd);
// $sqlp->bindValue(":id_orcamento", $id);

$sqlp->execute();
}
}
© 1999-2024 Hardware.com.br. Todos os direitos reservados.
Imagem do Modal