我已经使用 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">×</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=
我无法携带 ID,因此我无法编辑表格的特定行。
最佳答案
如果您能够调用该 URL 就好了。很可能是因为 UPDATE 语句中的这一行:
WHERE id='".$id."'");
^^^^^^^
其次,正确回显行id:
action="edit.php?id=<?php echo $row['id']; ?>"
并确保定义了 $name
。
为什么不使用准备好的语句:
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';
}
}
关于php - 使用该行的 id 编辑表格的特定行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26944813/