How to update multiple records at once? PHP+MYSQL

Asked

Viewed 623 times

0

inserir a descrição da imagem aqui

TABLE SIZE:

ID | NOME  |  PRECO 
1  | 06x08 |  0.00
2  | 08x10 |  0.00
3  | 09x12 |  0.00
4  | 11x14 |  0.00
5  | 13x18 |  0.00
6  | 18x24 |  0.00

I need to update the prices... how do I do it using UPDATE with PDO Man SELECT is thus along with a WHILE

<div class="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>

1 answer

1


without knowing how you’re trying to record, I believe the logic you need is this :
HTML Trocaria:

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

for:

<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();

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.