简体   繁体   中英

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

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

Add this line your form

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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