Once I edit the record, all the ones that already had are replaced by the edited one. Where did I go wrong?

Asked

Viewed 36 times

-3

<script>
var apagar = {
    sim:function(id){
        if(confirm('Tem certeza que deseja apagar ?')){
            $.ajax({
                type:'GET',
                url:'pagina/<?php echo $url; ?>/apagar/'+id,
                data:{'id':id},
                success:function(html){
                    alert('Apagado com sucesso!');
                    location.reload();
                }
            });
        }
    }
}
</script>
<?php
if(isset($_GET['tipo']) && $_GET['tipo'] == 'editar'){
    $id = $_GET['id'];
    $item_ver = $pdo->query("SELECT * FROM loja WHERE id='".$id."'")->fetch(PDO::FETCH_ASSOC);
    if($_POST){
        $item = $_POST['item'];
        $valor = $_POST['valor'];
        $vendedor = $_POST['vendedor'];
        $vendido = $_POST['vendido'];
        $status = $_POST['status'];
        if(empty($item) || empty($valor) || empty($vendedor)){
            echo Site::Alerta('Preencha todos campos!',false);
        }else{
            $inserir = $pdo->prepare("UPDATE loja SET item=:item, valor=:valor, vendedor=:vendedor, vendido=:vendido, status=:status");
            $inserir->bindParam(':item',$item);
            $inserir->bindParam(':valor',$valor);
            $inserir->bindParam(':vendedor',$vendedor);
            $inserir->bindParam(':vendido',$vendido);
            $inserir->bindParam(':status',$status);
            $inserir->execute();
            echo Site::Alerta('Editado com sucesso!','pagina/'.$url);
       }
    }
?>
<form method="POST" autocomplete="off">
    Item:<br>
    <input type="text" class="text" name="item" value="<?php echo $item_ver['item']; ?>"><br>
    Valor:<br>
    <input type="number" min="1" class="text" name="valor" value="<?php echo $item_ver['valor']; ?>"><br>
    Vendedor:<br>
    <input type="text" class="text" name="vendedor" value="<?php echo $item_ver['vendedor']; ?>"><br>
    Vendido:<br>
    <select name="vendido" class="select">
        <option value="true" <?php if($item_ver['vendido'] == 'true'){ echo 'selected'; } ?>>Sim</option>
        <option value="false" <?php if($item_ver['vendido'] == 'false'){ echo 'selected'; } ?>>N達o</option>
    </select><br>
    Status:<br>
    <select name="status" class="select">
        <option value="true" <?php if($item_ver['status'] == 'true'){ echo 'selected'; } ?>>Ativo</option>
        <option value="false" <?php if($item_ver['status'] == 'false'){ echo 'selected'; } ?>>Inativo</option>
    </select><br>
    <input type="submit" class="button" value="Editar">
</form>
<?php
}else if(isset($_GET['tipo']) && $_GET['tipo'] == 'apagar'){
        $id = (int) $_GET['id'];
        $delete = $pdo->query("DELETE FROM loja WHERE id='$id'");
        echo 1;
}else{
    if($_POST){
        $item = $_POST['item'];
        $valor = $_POST['valor'];
        $vendedor = $_POST['vendedor'];
        if(empty($item) || empty($valor) || empty($vendedor)){
            echo Site::Alerta('Preencha todos campos!',false);
        }else{
            $inserir = $pdo->prepare("INSERT INTO loja(item, valor, vendedor, vendido, time, status) VALUES(:item, :valor, :vendedor, 'false', '".$time."', 'true')");
            $inserir->bindParam(':item',$item);
            $inserir->bindParam(':valor',$valor);
            $inserir->bindParam(':vendedor',$vendedor);
            $inserir->execute();
            echo Site::Alerta('Inserido com sucesso!','pagina/'.$url);
       }
    }
?>
<form method="POST" autocomplete="off">
    Item:<br>
    <input type="text" class="text" name="item"><br>
    Valor:<br>
    <input type="number" min="1" class="text" name="valor"><br>
    Vendedor:<br>
    <input type="text" class="text" name="vendedor"><br>
    <input type="submit" class="button" value="Criar">
</form>
<table width="100%" style="margin: 10px 0 0 0; float:left">
    <tr style="height: 40px;">
        <th><img src="assets/img/x.png"></th>
        <th><img src="assets/img/editar.png"></th>
        <th>Item</th>
        <th>Valor</th>
        <th>Vendedor</th>
        <th>Vendido</th>
        <th>Status</th>
    </tr>
    <?php
    $i = 1;
    $sql = $pdo->query("SELECT * FROM loja ORDER BY id DESC");
    while($ver = $sql->fetch(PDO::FETCH_ASSOC)){
        $css = $i%2==0 ? '' : 'background: #EEE;';
    ?>
    <tr style="height: 40px; <?php echo $css;?>">
        <th><a style="cursor: pointer" onclick="apagar.sim('<?php echo $ver['id']; ?>')"><img src="assets/img/x.png"></a></th>
        <th><a href="pagina/<?php echo $url; ?>/editar/<?php echo $ver['id']; ?>"><img src="assets/img/editar.png"></a></th>
        <th><?php echo $ver['item']; ?></th>
        <th><?php echo $ver['valor']; ?></th>
        <th><?php echo $ver['vendedor']; ?></th>
        <th><?php if($ver['vendido'] == 'true'){ echo 'Sim'; }else{ echo 'N達o'; }; ?></th>
        <th><?php if($ver['status'] == 'true'){ echo 'Ativo'; }else{ echo 'Inativo'; }; ?></th>
    </tr>
    <?php $i++;} ?>
</table>
<?php } ?>
  • The absence of WHERE is a very common mistake, and it causes a lot of damage. Try not to forget.

1 answer

1

Hello! Your error is in your sql:

UPDATE loja SET item=:item, valor=:valor, vendedor=:vendedor, vendido=:vendido, status=:status

The right thing would be:

 UPDATE loja SET item=:item, valor=:valor, vendedor=:vendedor, vendido=:vendido, status=:status **WHERE id=:id**

With this you can change only one record in question, if you don’t, the whole table SHOP will be changed to the new value passed in question.

Browser other questions tagged

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