Jake Burkey
Jake Burkey

Reputation: 11

Deleting Specific rows using PHP

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()
?>

Upvotes: 0

Views: 84

Answers (2)

Fell
Fell

Reputation: 56

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.

Upvotes: 2

fab
fab

Reputation: 455

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>';
}

Upvotes: 0

Related Questions