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

时间:2017-02-11 05:10:12

标签: php

<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,标题,编辑和删除,所有其他的东西工作正常除了编辑类别当我点击编辑链接编辑任何类别它确实在输入字段中显示它但在我点击更新类别时不编辑它。

1 个答案:

答案 0 :(得分:0)

当你更新价值请求时会在帖子中显示条件

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

永远不会成真。您必须在编辑时将id存储到隐藏字段,然后更新值。您的更新查询中也有错误

按以下方式更改您的代码

<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>";
                                            }
                                        ?>