Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

240
Visualizações
trying to DELETE a row with PHP and MYSQL

I'm trying to add a delete button to a PHP and MYSQL to-do list. I test my query without placeholders in mysql workbench and the delete query works fine. I'm also pretty sure the variables I sent to the url work too, when I hover over them in my page I can see the values are correct. When I execute delete link, nothing happens and I don't get any error messages. Not sure what I'm doing wrong. Any help is appreciated

<?php
require_once 'app/init.php';

if($_GET['delete']){

   $id =  $_GET['id'];
   

    $deleteQuery = $db->prepare("
    DELETE 
    FROM todo_list
    WHERE id = :id
    AND user = :user LIMIT 1
    ");

    $deleteQuery->execute([
        'id' => $id,
        'user' => $_SESSION['user_id']
    ]);

}

header('Location: index.php');
?>

this is where the variables are coming from

<section>
        <div class="container">
            <div class="list">
                <h1 class="header">To Do</h1>

                <?php if(!empty($items)) : ?>
                <ul class="items">
                    <?php foreach($items as $item): ?>
                    <li>
                        <span class="item<?php echo $item['done'] ? ' done': '' ?> "><?php echo $item['name']; ?></span>
                        <?php if(!$item['done']): ?>
                        <a href="mark.php?as=done&item=<?php echo $item['id']; ?>" class="done-button">Mark as Done</a>
                        <a href="delete.php?delete=true&item=<?php echo $item['id']?>" class="item-delete">Delete <i class="fas fa-trash-alt"></i></a>
                        <?php endif; ?>
                    </li>
                    
                  
                    <?php endforeach; ?>
                </ul>
                <?php else:?>
                        <p>you haven't added anything yet</p>
                <?php endif; ?>
                <form class="item-add" action="add.php" method="post">
                    <input type="text" name="name" placeholder="type a new item" class="input" autocomplete="off" required>
                    <input type="submit" value="Add" class="submit">

                </form>
                
            </div>
        </div>
    </section>
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You called the parameter item and not id.

$id =  $_GET['id'];

should hence be

$id =  $_GET['item'];
over 4 years ago · Santiago Trujillo Relatório

0

You probably need to check if the get isset if(isset($_GET['item'])) { var_dump($_GET['item']); }

over 4 years ago · Santiago Trujillo Relatório

0

In your attribute tag you are using item as query string .

<a href="mark.php?as=done&item=<?php echo $item['id']; ?>

But you are getting data with wrong query string id. You should use below one.

if($_GET['delete']){

   $id =  $_GET['item'];
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda