用php编辑表的行

时间:2014-09-30 05:22:56

标签: php mysql sql mysqli

我创建了一个表来显示员工的详细信息。以下是表格页面中使用的代码部分

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

执行此页面后收到的视图将是这样的

ID  Name  Edit
1   emp1  edit
2   emp2  edit

模态代码

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

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

我希望当模式弹出时,用户可以输入新值,并更新该特定行的值。问题是它只是运行代码而不显示任何结果。有人可以告诉我如何获得理想的结果。

2 个答案:

答案 0 :(得分:0)

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

替换为

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

谢谢
php-tutorial-php.blogspot.in

答案 1 :(得分:0)

在表单中添加此行

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