简体   繁体   English

使用该行的id编辑表的特定行

[英]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. 我使用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 edit.php页面的代码是

<?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= 我的问题是当我到达edit.php页面时,我得到了这样的URL: http://example.com/xyz/edit.php?id=http://example.com/xyz/edit.php?id= edit.php

I am not able to carry the id due to which I am not able to edit a specific row of the table. 我无法携带id,因为我无法编辑表格的特定行。

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. 将ID直接输出到表单中。

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. 你应该只显示一次模态,然后通过jquery / javascript将ID传递给模态,执行ajax请求 - 显示数据,然后编辑它。

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 如果它只是一行,它应该没问题,但是如果你想要多个表行,能够编辑行中的任何值,那么你肯定需要使用ajax / jquery / javascript

Example using one modal window, and passing ID via jquery... 使用一个模态窗口并通过jquery传递ID的示例...

PHP PHP

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

Jquery 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.


     });

If you are enable to call the URL just fine. 如果您启用调用URL就好了。 Most likely because of this line on the UPDATE statement: 很可能是因为UPDATE语句中的这一行:

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

Second, properly echo the row id: 二,正确回显行id:

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

And make sure $name is defined. 并确保定义$name

And why not use prepared statements instead: 为什么不使用预备语句:

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM