1

Can someone help me. My delete code below works, but it's deleting the most recent Favorited file and not the specific file chosen. Here's the code:

while($row=$query->fetch())
{

$id=$row['id'];
$vid=$row['thread_id'];
$preview=$row['preview'];
$tt=$row['thread_title'];
$fav=$row['fav'];

$List.='<form action="" method="POST" id="postForm">
<div class="LISTT"><a href="VP2.php?id='.$vid.'">'.$preview.'</a><br/><label     id="pwords">'.$tt.'</label><br/>
<input type="submit" name="submit" value="Remove" id="DeleteButton"/>
</div></form>'; 

if(isset($_POST['submit']))
{
$query=$db->prepare("DELETE FROM favorite WHERE thread_id=:thread");
$query->execute(array(':thread'=>$vid));
}

}
7
  • 1
    you're spitting out a bunch of rows of data, but never check WHICH of those many rows' submit buttons was clicked. Commented Jul 16, 2014 at 19:28
  • How do I check? I've been trying to figure this out for the pass 2 days. Commented Jul 16, 2014 at 19:36
  • 1
    your code makes little sense. spit out a bunch of database results, IF a submit was done, then delete the db record of the row you just spit out, leaving an invalid ID in your form. Commented Jul 16, 2014 at 19:39
  • This comes down to one underlying fact. If your query is deleting the most recent item, it is because $vid IS the ID of the most recent item. Have you output the variable to confirm it is the correct value? Commented Jul 16, 2014 at 19:41
  • 1
    @NidMoass - a couple of things, then. You only need one form tag, for the whole page, so you can put that outside your loop. Each row you print from the database needs to pass a unique ID number - the row's primary key will work; you can do this by adding an input button for each row and giving them a unique name. Once the form has been submitted, look and see which button was pressed, and delete the appropriate row - you can put the logic for that before the existing code, so deleted rows aren't shown in the rest of the page. Commented Jul 17, 2014 at 0:27

2 Answers 2

1

You need to add a hidden form field that contains the Thread ID into your form, then read that back in your form handler, something like this:

while($row=$query->fetch())
{
$id=$row['id'];
$vid=$row['thread_id'];
$preview=$row['preview'];
$tt=$row['thread_title'];
$fav=$row['fav'];

$List.='<form action="" method="POST" id="postForm">
<div class="LISTT"><a href="VP2.php?id='.$vid.'">'.$preview.'</a><br/><label id="pwords">'.$tt.'</label><br/>
<input type="hidden" name="thread" value="' . $vid . '" />
<input type="submit" name="submit" value="Remove" id="DeleteButton"/>
</div></form>'; 

if(isset($_POST['submit']))
{
$id = $_POST["thread"];
$query=$db->prepare("DELETE FROM favorite WHERE thread_id=:thread");
$query->execute(array(':thread'=>$id));
}
}
Sign up to request clarification or add additional context in comments.

1 Comment

One more thing I forgot - you might want to put the form handler on the top, since if you do this, your page will still display the deleted record until it is refreshed
1

The reason for this is because you have the If statement in your while loop. The logic in the code you have given is to delete records when $_POST['submit'] is set. So it will follow the loop to delete the records and not a specific record.

You need to pass the id you want to delete to the user, as you are using form to do this, have a hidden field with the id.

if(isset($_POST['submit']))
{
    $query=$db->prepare("DELETE FROM favorite WHERE thread_id=:thread");
    $query->execute(array(':thread'=>$_POST['id']));
}

while($row=$query->fetch())
{

    $id=$row['id'];
    $vid=$row['thread_id'];
    $preview=$row['preview'];
    $tt=$row['thread_title'];
    $fav=$row['fav'];

    $List.='<form action="" method="POST" id="postForm">
<div class="LISTT"><a href="VP2.php?id='.$vid.'">'.$preview.'</a><br/><label     id="pwords">'.$tt.'</label><br/>
<input type="submit" name="submit" value="Remove" id="DeleteButton"/>
<input type="hidden" name="id" id="id" value="'.$id.'" />
</div></form>'; 

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.