当前位置: 动力学知识库 > 问答 > 编程问答 >

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

网友答案:

Add this line your form

<label for="exampleInputEmail1">ID</label>
<input type="text" class="form-control" id="exampleInputEmail1" placeholder="" name="id">
网友答案:
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

分享给朋友:
您可能感兴趣的文章:
随机阅读: