简体   繁体   English

用php编辑表格的行

[英]Edit row of a table with php

I have created a table to display details of employee. 我创建了一个表来显示员工的详细信息。 Here is the part of code used in the table page 这是表格页中使用的部分代码

echo "<td> <a href='#edit' data-toggle='modal'> edit </a> </td>";

View received after this page is executed would be something like this 执行此页面后收到的视图将是这样的

ID  Name  Edit
1   emp1  edit
2   emp2  edit

Code for Modal 模态代码

<div class = "modal fade" id="edit" role="dialog">
<div class = "modal-dialog">
<div class = "modal-content">
<div class = "modal-header">
<h4> Edit </h4>
</div>

<div class="modal-body">
<form role="form" action="edit_emp.php" method="post">
<div class="form-group">
<label for="exampleInputEmail1">Name</label>
<input type="text" class="form-control" id="exampleInputEmail1" placeholder="" name="empname">
</div>

<input name="submit" type="submit" value=" Edit ">              
</form> 
</div>  

<div class="modal-footer">
<a class="btn btn-primary" data-dismiss="modal"> Close </a>

</div>
</div>
</div>  
</div> 

Code used in the edit_emp.php page is 在edit_emp.php页面中使用的代码是

<?php
$con=mysqli_connect("localhost","root","","employee");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$id = $_POST['id']; 
$empname=$_POST['empname'];

mysqli_query($con,"UPDATE employee SET empname='$empname' WHERE id='".$id."'");
mysqli_close($con);
header("Location: index.php");
?> 

I wish that when the modal pops up the user can enter the new value and it updates the value of that specific row. 我希望在弹出模式对话框时,用户可以输入新值,并更新该特定行的值。 The problem is that it simply runs the code without showing any result. 问题在于它只是运行代码而没有显示任何结果。 Can someone please tell me how to get the desired result. 有人可以告诉我如何获得所需的结果。

mysqli_query($con,"UPDATE employee SET empname='$empname' WHERE id='".$id."'");

replace with 用。。。来代替

$result= mysqli_query($con,"UPDATE  employee SET empname=$empname WHERE id='$id'");
if ($result===false ) {
  printf("error: %s\n", mysqli_error($con));
} 

Thanks 谢谢
php-tutorial-php.blogspot.in php-tutorial-php.blogspot.in

Add this line your form 将此行添加到表格

<label for="exampleInputEmail1">ID</label>
<input type="text" class="form-control" id="exampleInputEmail1" placeholder="" name="id">

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

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