Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

439
Views
I want to select specific row when i click button using php/mysql

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

Picture for the table

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");

}
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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'] ."'"
over 4 years ago · Santiago Trujillo Report

0

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

  1. Name the session variable for every loop in such a way that it is different for every loop
  2. second is storing the id of current record in data attribute of the anchor tag, so you have to change the button tag to anchor tag, then follow the following:

<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 ;)

over 4 years ago · Santiago Trujillo Report

0

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");
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!