How to update several records in Mysql from PHP?

2

My question is how can I update several records of a Mysql table from php?

In the variable $ conver2 the values that I want to insert are saved

I put the update inside the foreach so that the sentence would be repeated according to the amount of data found in $ conver2.

If I update the records but it gives me the last value of the $ conver2 For example if in the $ conver2 there were these data:

'6,500.00' // '8,100.00', // '11,100.00'.

I would be updating the records but putting in all the last value of bone '11, 100.00 '.

 <?php
$conexion = mysqli_connect('localhost', 'root', '', 'scabc');

$query    = mysqli_query($conexion, "SELECT valStockI FROM movimientos where idMovimiento >= " . $idMovimiento . " and idStockI= " . $idStockI . "");
            
$stockvalorplus = 0;
$stockvalorplus = $valornuevo - $valorantiguo;

foreach ($query as $dato) {
$a       = $dato['valStockI'];
$conver  = str_replace(",", "", $a);
$total   = $conver + $stockvalorplus;
$conver2 = number_format($total, 2, '.', ',');
echo $conver2;
echo '</br>';

mysqli_query($conexion, 'UPDATE movimientos set valStockI="  ' . $conver2 . ' "  where idMovimiento >=' . $idMovimiento . ' and idStockI= ' . $idStockI . ' ');

  }
?>
    
asked by Luis 07.05.2018 в 22:26
source

1 answer

2

Your first select query selects 3 records. Your update query is exactly the same, then you will select the same 3 records. You can be more specific by selecting like this:

<?php
$conexion = mysqli_connect('localhost', 'root', '', 'scabc');

$query    = mysqli_query($conexion, "SELECT valStockI FROM movimientos where idMovimiento >= " . $idMovimiento . " and idStockI= " . $idStockI . "");

$stockvalorplus = 0;
$stockvalorplus = $valornuevo - $valorantiguo;

foreach ($query as $dato) {
$a       = $dato['valStockI'];
$conver  = str_replace(",", "", $a);
$total   = $conver + $stockvalorplus;
$conver2 = number_format($total, 2, '.', ',');
echo $conver2;
echo '</br>';

mysqli_query($conexion, 'UPDATE movimientos set valStockI="  ' . $conver2 . ' "  where idMovimiento >=' . $idMovimiento . ' and idStockI= ' . $idStockI . ' and movimientos ="' . $dato['movimiento'] . '" ');

  }
?>
    
answered by 07.05.2018 / 22:47
source