Sam
Sam

Reputation: 1381

Edit specific row of a table using id of that row

I have displayed some values from database in form of a table using php. One column of the table is reserved for edit. I wish that each row of the table can be edited.

Code for edit is

echo"<td class='success'><button class='btn blue' data-toggle='modal' data-target='#myeditModal'>Edit </button></td>";

Code used in modal is

<div class="modal fade" id="myeditModal" 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"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title" id="myModalLabel">Edut Treatment</h4>
            </div>

            <div class="modal-body">
                <form class="form-horizontal" role="form" action="edit.php?id="$row['id']"" enctype="multipart/form-data" method="post">
                    <div class="form-group">
                        <label class="col-lg-4 control-label"> Name</label>
                            <div class="col-lg-6">
                                <input class="form-control" value="" type="text" name="name" >
                            </div>
                    </div>

                    <div class="form-group">
                    <label class="col-md-3 control-label"></label>
                        <div class="col-md-8">
                            <input class="btn btn-primary" value="Save Changes" type="submit" name="submit">

                            <span></span>
                        </div>  
                    </div>
                </form>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

Code for edit.php page is

<?php
include('admin_session.php');
$con=mysqli_connect("localhost","root","","db");

// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$id = $_GET['id'];

mysqli_query($con,"UPDATE treatment_type SET name='".$name.'" WHERE id='".$id."'");
mysqli_close($con);
header("Location: abc.php");
?> 

My problem is that when I reach the edit.php page I get the url as this: http://example.com/xyz/edit.php?id=

I am not able to carry the id due to which I am not able to edit a specific row of the table.

Upvotes: 4

Views: 6034

Answers (2)

Kevin
Kevin

Reputation: 41885

If you are enable to call the URL just fine. Most likely because of this line on the UPDATE statement:

WHERE id='".$id."'");
          ^^^^^^^

Second, properly echo the row id:

action="edit.php?id=<?php echo $row['id']; ?>"

And make sure $name is defined.

And why not use prepared statements instead:

if(isset($_GET['id'], $_GET['name'])) {
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    $con = mysqli_connect("localhost","root","","db");

    $id = $_GET['id'];
    $name = $_GET['name'];

    $sql = 'UPDATE treatment_type SET name = ? WHERE id = ?';
    $update = $con->prepare($sql);
    $update->bind_param('si', $name, $id);
    $update->execute();

    if($update->affected_rows > 0) {
        header("Location: abc.php");
    } else {
        echo 'did not update';
    }   
}

Upvotes: 3

Kylie
Kylie

Reputation: 11749

The way you have it now, you need to change to this..

 <form class="form-horizontal" role="form" action="edit.php?id='<?php echo $row['id'];?>'" enctype="multipart/form-data" method="post">

To output the ID corerectly into the form.

But this way you are ouputting multiple different modal forms for each row??

You should really just display the modal once, then pass the ID via jquery/javascript to the modal, do an ajax request-display the data, and then edit it.

If its just one row, it should be fine, But if you want multiple table rows, with the ability to edit any value in the row, then you definitely need to use ajax/jquery/javascript

Example using one modal window, and passing ID via jquery...

PHP

echo"<td class='success'><button class='btn blue editButton' data-id='<?php echo $row['id'];?>' >Edit </button></td>";

Jquery

$(document).ready(function(){
     //Click button, apply id, and open modal.
    $('.editButton').click(function(){
      var id = $(this).data('id');
      //Apply action with id to form
      $('#myForm').attr('action','edit.php?id='+id);
      //Open modal 
      $('#myModal').modal({background:'static'});
     }):

    //If you want values to come into the modal you need to do a sepereate ajax call to get the one particular element from database.


     });

Upvotes: 2

Related Questions