<form action="categories.php" method="post">
<div class="form-group">
<label for="cat_title"> Edit Category </label>
<?php
if (isset($_GET['edit'])) {
$cat_id = $_GET['edit'];
$query = "SELECT * FROM categories WHERE cat_id = $cat_id ";
$select_cat_gories = mysqli_query($connection, $query);
if (!$select_cat_gories) {
die("query failed".mysqli_error($connection));
}
while ($rows = mysqli_fetch_assoc($select_cat_gories)) {
$id = $rows['cat_id'];
$title = $rows['cat_title'];
?>
<input value="<?php if(isset($title)){ echo $title;} ?>" type="text" class="form-control" name="cat_title">
<?php
}
} ?>
<?php
if (isset($_GET['edit'])) {
$cat_id = $_GET['edit'];
if (isset($_POST['upddate'])) {
$title = $_POST['cat_title'];
$query = "UPDATE categories SET cat_title = '{$title}' WHERE cat_id = {$cat_id} ";
$update_query = mysqli_connect($connection, $query);
if (!$update_query) {
die("query Failed".mysqli_error($connection));
}
}
}
?>
</div>
<div class="form-group">
<input class="btn btn-primary" type="submit" name="upddate" value="Update_category">
</div>
</form>
</div>
<div class="col-xs-6">
<table class="table table-bordered table-hover">
<thead>
<tr>
<th> Id</th>
<th> Category title </th>
<th> Delete </th>
</tr>
</thead>
<tbody>
<?php
$query = "SELECT * FROM categories";
$select_cat_gories = mysqli_query($connection,$query);
while($rows = mysqli_fetch_assoc($select_cat_gories)){
$id = $rows['cat_id'];
$title = $rows['cat_title'];
echo "
<tr>
";
echo "
<td>{$id}</td>";
echo "
<td>{$title}</td>";
echo "
<td><a href='categories.php?delete={$id}'> delete </a> </td>";
echo "
<td><a href='categories.php?edit={$id}'> Edit </a></td>";
echo "
</tr>";
}
?>我一直在尝试编辑表格中的内容,我已经创建了一个表格,我在其中显示了一个添加类别和一个更新类别,这两个都是网页中的文本字段,在同一页面上有列id,title,edit和delete的表格,除了编辑类别之外,所有其他的东西都工作得很好,当我点击编辑链接来编辑任何类别时,它在输入字段中显示它,但当我点击更新类别时,它不会编辑它。
发布于 2017-02-11 13:29:55
当您更新值时,请求将在post中,因此条件
if(isset($_GET['edit']))永远不会变成真的。你必须在编辑时将id存储到隐藏字段中,然后更新值。此外,update查询中还存在错误
按如下所示更改代码
<form action="categories.php" method="post">
<div class="form-group">
<label for="cat_title"> Edit Category </label>
<?php
if(isset($_GET['edit']))
{
$cat_id = $_GET['edit'];
$query = "SELECT * FROM categories WHERE cat_id = $cat_id ";
$select_cat_gories = mysqli_query($connection,$query);
if(!$select_cat_gories){
die ("query failed". mysqli_error($connection));
}
while($rows = mysqli_fetch_assoc($select_cat_gories))
{
$id = $rows['cat_id'];
$title = $rows['cat_title'];
?>
<input value="<?php if(isset($title)){ echo $title;} ?>" type="text" class="form-control" name="cat_title">
<input value="<?php echo $id; ?>" type="hidden" class="form-control" name="cat_id">
<?php }} ?>
<?php
if(isset($_POST['upddate']))
{
$title = $_POST['cat_title'];
$cat_id = $_POST['cat_id'];
$query = "UPDATE categories SET cat_title = '{$title}' WHERE cat_id = {$cat_id} ";
$update_query = mysqli_query($connection,$query);
if(!$update_query)
{
die("query Failed" . mysqli_error($connection));
}
}
?>
</div>
<div class="form-group">
<input class="btn btn-primary" type="submit" name="upddate" value="Update_category">
</div>
</form>
</div>
<div class="col-xs-6">
<table class="table table-bordered table-hover">
<thead>
<tr>
<th> Id</th>
<th> Category title </th>
<th> Delete </th>
</tr>
</thead>
<tbody>
<?php
$query = "SELECT * FROM categories";
$select_cat_gories = mysqli_query($connection,$query);
while($rows = mysqli_fetch_assoc($select_cat_gories))
{
$id = $rows['cat_id'];
$title = $rows['cat_title'];
echo "<tr>";
echo "<td>{$id}</td>";
echo "<td>{$title}</td>";
echo "<td><a href='categories.php?delete={$id}'> delete </a> </td>";
echo "<td><a href='categories.php?edit={$id}'> Edit </a></td>";
echo "</tr>";
}
?>https://stackoverflow.com/questions/42172469
复制相似问题