How to update multiple records at once? PHP + MYSQL

0

TABLESIZE:

ID|NOME|PRECO1|06x08|0.002|08x10|0.003|09x12|0.004|11x14|0.005|13x18|0.006|18x24|0.00

Ineedtoupdateprices...howdoIdothisusingUPDATEwithPDOMySELECTisthustogetherwithaWHILE

<divclass="panel-body">
    <?php
        $pdo = db_connect();
        $listar = $pdo->prepare("SELECT * FROM tamanhos");
        $listar->execute();
        while ($dados = $listar->fetch(PDO::FETCH_OBJ)){            
    ?>
    <div class="form-group">
        <label class="col-sm-2 control-label"><?php echo $dados->nome; ?></label>
        <div class="col-sm-4">
            <input type="text" name="tamanho[]" id="tamanho" class="form-control porcelana" value="<?php echo $dados->preco; ?>"  />
        </div>
    </div>
    <? } ?>
</div>
    
asked by anonymous 22.08.2017 / 03:00

1 answer

1

Without knowing how you are trying to record, I believe the logic you need is this:
HTML Exchange:

<input type="text" name="tamanho[]" id="tamanho" class="form-control porcelana" value="<?php echo $dados->preco; ?>"  />

by:

<input type="text" name="tamanho<?= $dados->id ?>" id="tamanho" class="form-control porcelana" value="<?= $dados->preco ?>"  />

submit = > "controller.php"

 $pdo = db_connect();
 $listar = $pdo->prepare("SELECT * FROM tamanhos");
 $listar->execute();
 while ($dados = $listar->fetch(PDO::FETCH_OBJ)){ 
   $dados->preco = $_REQUEST['tamanho'.$dados->id] ;

$sql = "update tamanho set preco = ? where id = ?";

    $statement = $pdo->prepare($sql);
    $statement->bindParam(1, $dados->preco);
    $statement->bindParam(2, $dados->id);

    $statement->execute();

}
    
22.08.2017 / 04:43