I want to select specific row when i click the delete button or edit button in my table because every row in my table have 2 buttons one for delete and other for edit but it delete the row has the last id not the row i want it . I want when i click the button in the same row the row deleted . look for the below image please to understand what i mean
I'm using this code to create the table
while($row = mysqli_fetch_assoc($result))
{
echo '
<tr class="w3-hover-orange">
<td>
<a class="links" href="topic.php?id=' . $row['topic_id'] . '">' . $row['topic_subject'] . '</a>
</td>
<td>
' . date('d-m-Y', strtotime($row['topic_date'])) . '
</td>
<td>
</td>
<td>
<a href="edit_topic.php" name="topicEdit" id = "topicEdit" class = "w3-button tableButtons" >Edit</a>
<button href="#" name="topicDelete" type="button" id="topicDelete" data-toggle="modal" data-target="#myModal" class ="w3-button tableButtons">Delete</button>
</td>
</tr>';
$_SESSION['topic_id_number'] = $row['topic_id'];
$_SESSION['topciSubject'] = $row['topic_subject'];
}
echo'</table></form></div>';
And below the code for the delete topic
if(isset($_POST['topicDelete'])){
$delete = $_SESSION['topic_id_number'];
$query = "DELETE FROM topics WHERE topic_id =".$delete;
$result = mysqli_query($con,$query);
header ("location: awareness-projects.php");
}
I think $row['topic_id'] id the id against which delete is to be performed.
Make following changes:
<button href="#" name="topicDelete" type="button" id="topicDelete" data-toggle="modal" data-target="#myModal" class ="w3-button tableButtons">Delete</button>
change it to an anchor and in its href pass this $row['topic_id'] and get its value on the deletion page like:
<a href="xyz.php?id='".$row['topic_id'] ."'"
As the table entries are being generated dynamically through the loop it will always take the last id when the loop is finished because the session name you use is the same for every while loop entry. so it will be overwrite every time and the last id will be in the session variable. There are several options you can try
<a href="#" name="topicDelete" type="button" id="topicDelete" data-toggle="modal" data-target="#myModal" class ="w3-button tableButtons" data-cid="$row['topic_id']">Delete</a>
then when you click the link get the value of the current link using the following code of jquery and store it into the hidden field of the modal
$(".tableButtons").click(function(){
var id=$(this).data("cid");
alert(id);//check the id if you are getting current id or not
$("#temp").attr("value",id);//this is the hidden field in modal which id is temp and name it to whatever name you use to get the value of it in your delete code
});
whenever you will submit the form this hidden field's value will be set with the value of the id and will be submitted if you are using the form action method. then you can retrieve the value of it as a normal textbox as you have already done many times I hope so. All The Best. I am personally using this code for my work and it works completely fine ;)
Send topic_id with query-string as follows
<a href="xyz.php?id='".$row['topic_id']."'" name="topicDelete" type="button" id="topicDelete" data-toggle="modal" data-target="#myModal" class ="w3-button tableButtons">Delete</a>
Delete code -
if(isset($_POST['topicDelete'])){
$delete = $_GET['id'];
$query = "DELETE FROM topics WHERE topic_id =".$delete;
$result = mysqli_query($con,$query);
header ("location: awareness-projects.php");
}