How to perform UPDATE with PDO in PHP?

3

So folks, I wanted to redeem the data to edit in update.php , but how much lost here, how to give a force?

   <div class="container">
<div class="table-responsive">
    <table class="table">
        <tr>
            <th>Produtos</th>
            <th>Marca</th>
            <th>Modelo</th>
            <th>Quantidade</th>
            <th>Estado</th>
            <th>Opções</td>
        </tr>
            <?php foreach ($resultado as $key) :?>
        <tr>
            <td><?php echo $key->nome;?></td>
            <td><?php echo $key->marca;?></td>
            <td><?php echo $key->modelo;?></td>
            <td><?php echo $key->quantidade;?></td>
            <td><?php echo $key->estado;?></td>
            <td>
                <a href="mysql/update.php?id=<?php echo $key->id; ?>">Alterar</a>
                <a href="mysql/delete.php?id=<?php echo $key->id; ?>">/Deletar</a>
            </td>
        </tr>
        <?php endforeach;?>
    </table>
</div>

  • update.php

        <?php 
    try{
        $id = $_GET['id'];
        $sql = $pdo->prepare("UPDATE FROM estoqueprodutos . produtos WHERE id = $id");
        $sql->execute();
        var_dump($id);
    
        echo $sql->rowCount();
    
    }catch (PDOException $e){
        echo 'Error: '. $e->getMessage();
    
    }    ?>
    

    EDITED

I have a problem, I created this code to be leaner but it is not running and it is not giving me any errors could you help me to solve this problem?         

if(!empty($_POST['id'])){
    try {
        $sql = "update produtos set nome = ?, marca = ?, modelo = ?, quantidade = ?, estado = ? where id = ?";

        $statement = $pdo->prepare($sql);

        $statement->bindParam(1, $_POST['nome']);
        $statement->bindParam(2, $_POST['marca']);
        $statement->bindParam(3, $_POST['modelo']);
        $statement->bindParam(4, $_POST['quant']);
        $statement->bindParam(5, $_POST['estado']);
        $statement->bindParam(6, $_POST['id']);

        $statement->execute();


        var_dump($statement);
    } catch (PDOException $e) {
        print $e->getMessage();
    }
} else {
    echo "Desculpe-nos, mas não foi possível alterar este produto. Entre em contato com o suporte!";
}

? >

    
asked by anonymous 10.12.2015 / 17:35

1 answer

4

The logic to perform the update is as follows, in its file update.php it will not execute a UPDATE tabela SET campo = valor .

update.php in you need to query the database through the id received by get, with the information in hand create the html form and fill in the value attribute with the database value something like:

<?php
//linhas omitidas ...
$sql->execute();
$registro = $sql->fetch(PDO::FETCH_ASSOC);
?>

<form method="post" action="gravar.php">
  <input type="hidden" name="id" value="<php echo $registro['id']; ?>" />
  <input type="text" name="nome" value="<php echo $registro['nome']; ?>" />

In the grava.php file, a new or updated record will be saved. But how do you know which operation to perform?

This can be done by checking the value of id (see form fields).

gravar.php

<?php
  if(!empty($_POST['id']){
      //realizar um update
  }else{
      //Não existe um id, logo é um registro novo, insert nele!
  }
    
10.12.2015 / 17:54