0

I am trying to add a delete button to my note taking website project I am doing at school. Almost like a forum, where you would delete replies to a topic... I want to be able to delete individual notes. Currently I can delete, however it will only delete the most recent note (bottom of the notes table). How would I get it so the delete button deletes the note from the corresponding row, not just the bottom one.

The main interface (Where I believe the problem is)

$sql="SELECT noteID, title, note, timestamp FROM $tbl_name WHERE user='$currentuser' AND subject='$currentsubject'";


$result=mysql_query($sql);
?>

<table align="center" id="notes">
<tr>
<td width="10%"><strong>Title</strong></td>
<td width="10%"><strong>ID</strong></td>
<td width="50%"><strong>Note</strong></td>
<td width="10%"><strong>Time</strong></td>
<td width="10%"><strong>Delete</strong></td>
</tr>

<?php

// Start looping table row
while($rows=mysql_fetch_array($result)){

echo'
<br> 
<tr>
<td>'.$rows['title'].'</td>
<td>'.$rows['noteID'].'</td>
<td>'.$rows['note'].'</td>
<td>'.$rows['timestamp'].'</td> ';
$todelete = $rows['noteID'];
$_SESSION['todelete'] = $todelete;
echo'
<td><form method="post" action="deletenote.php" ><input type="submit" name="submit" value="Delete" </td> 
</tr>

';
}
ob_end_flush()
?>
8
  • 1
    Do not use mysql_* functions anymore! It's old, deprecated, unsecure, removed in php7. Use mysqli or PDO! Commented Feb 7, 2018 at 11:32
  • Loop is Overwrite $todelete variable Commented Feb 7, 2018 at 11:32
  • I don't see a DELETE FROM ..... so how do we know if that isn't failing also? Commented Feb 7, 2018 at 11:33
  • You have a bunch of buttons that all submit the same form. Make sure you somehow pass the id of the note to delete. Also, note that your form is never closed, so you're nesting forms, possibly generating side effects. Lastly, mysql_query and other functions from the mysql_ API are depricated. If you're learning, please learn about an API that is actually in use, like PDO (my suggestion) or mysqli_*. Commented Feb 7, 2018 at 11:33
  • I also don't see a closing </form> tag. Plus, <form> cannot be made child of <table>. Commented Feb 7, 2018 at 11:34

2 Answers 2

2

Another way of doing it would be by just providing a delete link for each post:

// Start looping table row
while($rows=mysql_fetch_array($result)){
    echo '<br>';
    echo '<tr>';
    echo '<td>'.$rows['title'].'</td>';
    echo '<td>'.$rows['noteID'].'</td>';
    echo '<td>'.$rows['note'].'</td>';
    echo '<td>'.$rows['timestamp'].'</td>';
    echo '<td><a href="//example.com/deletenote.php?id='.$rows['noteID'].'">Delete</a></td>';
    echo '</tr>';
}

Keep in mind that deletenote.php would have to verify that the user is signed in and has permission to delete the post.

Sign up to request clarification or add additional context in comments.

1 Comment

Sorted! Legend <3
0

First of all: where is your corresponding code/query regarding the specific use case (delete note)?

You are trying to submit a form without any parameters, I am assuming you are trying to use the session variable. However, this variable will always be set to the last loop-iteration. Therefore, I would recommend you to use this code sample:

// Start looping table row
while($rows=mysql_fetch_array($result)){
    echo '<br>';
    echo '<tr>';
    echo '<td>'.$rows['title'].'</td>';
    echo '<td>'.$rows['noteID'].'</td>';
    echo '<td>'.$rows['note'].'</td>';
    echo '<td>'.$rows['timestamp'].'</td>';
    echo '<td><form method="post" action="deletenote.php" ><input type="hidden" name="noteID" value="'.$rows['noteID'].'"><input type="submit" name="submit" value="Delete"></form> </td>';
    echo '</tr>';
}

1 Comment

May be you're missing name in hidden field.

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.