我在php中显示了一个html表,while循环从数据库中提取一些数据,并在表的行中包含一个编辑按钮来编辑该行中的条目(最后是数据库)。但是,无论我点击哪个编辑按钮,它都只编辑第一行元素。我该如何解决?
<div class="col-md-8">
<?php
$test=1;
if(!empty($_SESSION['uid']))
{
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "Hostel";
$test++;
$flag=FALSE;
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
// $id=$_POST['complid'];
$c_name=$_SESSION['name'];
$c_usn=$_SESSION['uid'];
?>
<div class="container col-md-12" >
<h1 style="text-color:white;">The posts are listed below :</h1>
<hr style="box-shadow:1px 1px 1px black;">
<style>
h1
{
color: white;
}
</style>
<div class="table-responsive" style="border-radius:10px;border:2px solid gray;box-shadow:10px 10px 10px black;background-color:#d2d2d2;">
<table class="table table-bordered table-hover">
<tbody>
<tr>
<th>Post.No.</th>
<th>Date</th>
<th>Title</th>
<th>Operations</th>
</tr>
<?php
$query = "SELECT * FROM posts";
$result = mysqli_query($conn,$query);
while ($row=mysqli_fetch_array($result,MYSQLI_ASSOC))
{
?>
<tr>
<td><?php echo $row{'id'};?></td>
<td><?php echo $row{'date'};?></td>
<td><?php echo $row{'title'};?></td>
<td>
<b><button type="button" id="editbut" style="color:yellow;background-color:black;border:1px solid gray;border-radius:3px;" data-toggle="modal" data-target="#exampleModal">Edit</button> </b>
<b><button type="button" style="color:white;background-color:red;border:1px solid gray;border-radius:3px;" data-toggle="modal" data-target="#exampleModa2">Delete</button> </b>
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="exampleModalLabel">Edit your post here</h4>
</div>
<div class="modal-body">
<form>
<div class="form-group">
<label for="message-text" class="control-label">Title :</label>
<textarea class="form-control" id="message-text"><?php echo $row['title']; ?></textarea>
<label for="message-text2" class="control-label">Matter :</label>
<textarea class="form-control" id="message-text"><?php echo $row['matter']; ?></textarea>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-primary">Edit Post</button>
</div>
</div>
</div>
</div>
<script type="text/javascript">
posttitle = "<?php echo $row{'title'}; ?>";
postid = "<?php echo $row{'id'}; ?>";
document.getElementById("deletebut").onclick = function () {
alert(postid);
//location.href = "deletepost.php?id="+postid;
};
</script>
<div class="modal fade" id="exampleModa2" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="exampleModalLabel">Confirm your delete request:</h4>
</div>
<div class="modal-body">
<label for="message-text" class="control-label">Are you sure, you want to delete this post?</label>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" id="deletebut" class="btn btn-danger">Delete post</button>
</div>
</div>
</div>
</div>
</td>
</tr>
<?php
}
}
else
{
echo "<script type='text/javascript'alert(\"Please Login.\");>window.location.href = 'sign.php';</script>";
exit();
}
?>
</tbody>
</table>
</div>
</div>
答案 0 :(得分:1)
我可以继续,但这已经非常糟糕了。