Babidi
Babidi

Reputation: 74

Edit a selected record from a list with records

I have a list of database records on my page. Each record have there own edit button. When i click on the edit button a modal shows up and i can do my process there in a form. But i can't figure out how i can get the selected record to my update query.

For now i have 3 records with an project name. Lets say i want to edit the second record in the list, I always get the values from the first record but i want the second record. So i would like to know how i can get the values from a selected record.

<!-- Get all projects in the database -->
<?php   $result = mysqli_query($connection, "SELECT project_name, project_completion FROM project");

        while ($project = mysqli_fetch_assoc($result)){ ?>

            <tr>
                <td><a href="#" class="btn-sm btn btn-warning"><span class="glyphicon glyphicon-ok"></span></a></td>
                <td><a href="milestones.php?project=<?php echo $project["project_name"]; ?>"><?php echo $project["project_name"]; ?></a></td>                                           
                  <td>
                    <div class="progress">
                        <div class="progress-bar" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width: 60%;"></div>
                    </div>                                                                                              
                </td>   
                <td><?php echo $project["project_completion"]?>%</td>
                            <!-- edit button on click to modal -->                                                                      
                <td><a  data-toggle="modal" data-target="#myModalEdit" class="btn-sm btn btn-warning"><span class="glyphicon glyphicon-pencil"></span></a> 
                            <!-- remove button -->  
                    <a href="#" class="btn-sm btn btn-danger"><span class="glyphicon glyphicon-remove"></span></a></td>



                    <!-- Modal -->
                     <div class="modal fade" id="myModalEdit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                                    <h4 class="modal-title" id="myModalLabel"> Edit project</h4>
                                </div>
                                <div class="modal-body">

                                    <!-- do process with selected record -->

                                </div>
                                </div>
                                <div class="modal-footer">
                                  <input type="submit" name="saveProject" value="Add" class="btn btn-success"/>
                                </div>
                            </form>
                          </div><!-- /.modal-content -->
                        </div><!-- /.modal-dialog -->
                      </div><!-- /.modal -->                                                                                    


           </tr>    


                                        <?php } ?><!-- end while -->  

Upvotes: 0

Views: 1083

Answers (4)

harsh4u
harsh4u

Reputation: 2600

Assign different id to every popup.

<?php $i++; ?>
<a  data-toggle="modal" data-target="#myModalEdit-<?php echo $i;?>" class="btn-sm btn btn-warning"><span class="glyphicon glyphicon-pencil"></span></a> 

Assign $i=0; before while loop start.

Then also change modal popup id like below:

<div class="modal fade" id="myModalEdit-<?php echo $i;?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">

Update above edit and modal popup line. Then try.

Upvotes: 1

Ajitha Ms
Ajitha Ms

Reputation: 555

Try this:

Step 1: In your select query, get auto increment value also (for example - any id)

step 2: Then set one hidden field which contains your increment id

for example:

<input type="hidden" name="test" value="<?php echo your id value?>">

when submit your form you will get which row will be updated

Some changes in your code:

 <?php $result = mysqli_query($connection, "SELECT project_id,project_name,  project_completion FROM project"); ?>
   ..........................
   ..........................
 <div class="modal-footer">
 <form>
 <input type="hidden" name="projectid" value="<?php $project['project_id'] ?>">
 <input type="submit" name="saveProject" value="Add" class="btn btn-success"/>
 </form>
 </div>

Upvotes: 0

Purushottam zende
Purushottam zende

Reputation: 552

There are different ways for this to do. like :-

  1. When creating the list of records in your php loop apply a "onclick" event to your edit button which will have records primary key as parameter. When ever edit button is clicked this function will be called and through ajax you can fetch data of this record and display. And at the time of saving the data again call ajax and save the data. In this you have to call two ajax method i.e for saving and for fetching the data.

  2. Or you can use same page with different Post variables. Like when edit button is cliked reload the same page with id appended to it url. Check the url if its appended with any records id variable, if yes then show form to edit, make changes and submit or else show only records.

Upvotes: 0

Hurricane Development
Hurricane Development

Reputation: 2464

You have to use some type of identification. For example lets say you has the following code for a button

<button type="button" class="btn btn-primary editor" data-target=<?php echo $project['id']; ?>>Edit Something</button>
<div id="returnbuffer" style="display: none;"></div>

Then in javascript later on you have the following code

<script>
    $(document).ready(function() {
        $('editor').click(function() {
            $('#returnbuffer').load('editRecord.php?id=' + $(this).attr('data-target'));
        });
    });
</script>

Then you just have a php file in the same directory that usses get protocol and changes the database.

Just to explain what is actually happening here, when the button is clicked javascript runs a php file and sends information on which record (with what ID) should be changed.

Upvotes: 0

Related Questions