繁体   English   中英

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

[英]Edit specific row of a table using id of that row

我使用php以表格的形式从数据库中显示了一些值。 该表的一列保留用于编辑。 我希望可以编辑表格的每一行。

编辑代码是

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

模态中使用的代码是

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

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

我的问题是当我到达edit.php页面时,我得到了这样的URL: http://example.com/xyz/edit.php?id=http://example.com/xyz/edit.php?id= edit.php

我无法携带id,因为我无法编辑表格的特定行。

你现在的方式,你需要改变这个..

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

将ID直接输出到表单中。

但是这样你就可以为每一行输出多种不同的模态形式?

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

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

使用一个模态窗口并通过jquery传递ID的示例...

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.


     });

如果您启用调用URL就好了。 很可能是因为UPDATE语句中的这一行:

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

二,正确回显行id:

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

并确保定义$name

为什么不使用预备语句:

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