How to perform UPDATE with PDO in PHP?

Asked

Viewed 10,445 times

3

So guys, I wanted to rescue the data to edit on update.php, but I’m a little lost here, it’s got to give?

   <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

Guys I’m having a problem I created this code to get leaner but it’s not running and it’s not giving me any error you could 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!";
}

?>

  • What’s the doubt? From PHP and PDO I don’t know what you’re asking, but it would be nice to at least read the SQL manual to see the UPDATE syntax.

  • That’s almost it, just need to create the inputs with the values of the bank and use Prepared statements correctly.

  • i want to rescue table data by id and edit in my update.php but I’m lost on that.

  • @felipemarques would be nice if you [Dit] the question and put this information, and all possible details of the difficulty you are having, otherwise it becomes a guessing game. Suggested reading: [Ask]

  • Make a select by specifying the id, and executes the update when the form is submitted.

  • @felipemarques if it is a different question, ask a new question separately from this one. If there is no error, it may be that the ID is going empty, then it doesn’t even try to update; You need to check the FORM that sends this data.

Show 1 more comment

1 answer

4


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

update.php in you need to make a query in the database through the id received by get, with the information in hand create the html form and fill in the attribute value with 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 archive grava.php will be saved a new record or updated. But how to know which operation to perform?

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

write php.

<?php
  if(!empty($_POST['id']){
      //realizar um update
  }else{
      //Não existe um id, logo é um registro novo, insert nele!
  }
  • face excuse the question but I could not understand what would be in the lines omitted in update.php

  • @felipemarques means I didn’t put all the code, I just put the lines that change.

  • I understand, I’m sorry about the question, I’m feeling useless here.

  • @felipemarques, don’t worry about it, the best thing is not to be in doubt.

  • 1

    Felipemarques is always good to ask, and not get in doubt as @rray said. But always try to elaborate the questions so that you have as many relevant details as possible, as it increases the chance of the answers being complete and focused on your real problem. And remember that you can ask several questions as you experience difficulties in other parts of the process. Understand that our comments are always tips to help improve, and it doesn’t mean we don’t like the question.

  • Got it, thanks for the tip.

Show 1 more comment

Browser other questions tagged

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