Php 编辑查询在CMS系统中不起作用 编辑类别

Php 编辑查询在CMS系统中不起作用 编辑类别,php,Php,当您更新值时,请求将处于post状态,因此 <form action="categories.php" method="post"> <div class="form-group"> <label for="cat_title"> Edit Category </label> <?php if (isset($_GET['edit'])) { $c

当您更新值时,请求将处于post状态,因此

<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存储到隐藏字段中,然后更新值。更新查询中也有错误

按如下所示更改代码

if(isset($_GET['edit']))

编辑类别

只是我必须将其隐藏?是的,并删除条件
if(isset($\u GET['edit'])
if(isset($\u POST['upddate']))
Sir如果我删除编辑,它将在查询中称为未定义的cat id,类型隐藏将使其隐藏,然后我将如何编辑任何类别,我有一张您想要查看的表的快照,然后检查您有两次此情况
if(isset($\u GET['edit'])
。我告诉过你只从第二次删除这个。或者简单地复制粘贴我答案的代码,然后再试一次
<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>";
                                            }
                                        ?>