Php 数据库中的editin类别出错

Php 数据库中的editin类别出错,php,mysql,database,Php,Mysql,Database,我已经创建了一个包含表类别的数据库。我创建了添加、删除、读取和编辑类别的功能。但阅读添加和删除操作很好,但编辑显示错误 <div id="wrapper"> <?php include "includes/admin_navigation.php"?> <div id="page-wrapper"> <div class="container-fluid"> <!-- Page

我已经创建了一个包含表类别的数据库。我创建了添加、删除、读取和编辑类别的功能。但阅读添加和删除操作很好,但编辑显示错误

<div id="wrapper">

    <?php include "includes/admin_navigation.php"?>

    <div id="page-wrapper">

        <div class="container-fluid">

            <!-- Page Heading -->
            <div class="row">
                <div class="col-lg-12">
                    <h1 class="page-header">
                        Blank Page
                        <small>Subheading</small>
                    </h1>
                    <div class="col-lg-6">
                        <?php // add category

                        if (isset($_POST['submit'])) {
                            $cat_name = $_POST['cat_name'];

                            if ($cat_name == "" || empty($cat_name)) {
                                echo "this field should not be empty";
                            } else {
                                $query = "INSERT INTO categories(cat_name) VALUE('$cat_name')";

                                $create_category = mysqli_query($connection, $query);

                                if(!$create_category){
                                    die("QUERY FAILED" . mysqli_error($connection));
                                }
                            }
                        }

                        ?>
                        <form action="categories.php" method="post">
                            <div class="form-group">
                                <label for="cat_name">Enter category name</label>
                                <input class="form-control" type="text" name="cat_name">
                            </div>
                            <div class="form-group">
                                <input class="btn primary" type="submit" name="submit" value="Add category">
                            </div>
                        </form>
                        <form action="categories.php" method="post">

                            <?php // edit category


                            if (isset($_GET['edit'])){
                                $cat_id = $_GET['edit'];
                                $query = "SELECT * FROM categories WHERE id = {$cat_id}";

                                $select_categories_id = mysqli_query($connection, $query);

                            while ($row = mysqli_fetch_assoc($select_categories_id)){

                                $cat_title = $row['cat_name'];
                                $the_cat_id = $row['id']; ?>

                                <div class="form-group">
                                    <label for="cat_name">Edit category name</label>
                                    <input value="<?php if(isset($cat_title)) {echo $cat_title;} ?>" class="form-control" type="text" name="cat_name">
                                </div>
                                <div class="form-group">
                                    <input class="btn primary" type="submit" name="update_category" value="Edit category">
                                </div>
                            <?php }

                            }

                            if (isset($_POST['update_category'])) {
                                $update_cat_title = $_POST['cat_name'];
                                $query = "UPDATE categories SET cat_name = {$update_cat_title} WHERE id = {$cat_id}"; // the problem is with  $cat_id (on videolesson everything works fine but on my local pc becomes next error - Notice: Undefined variable: the_cat_id in /var/www/html/wordpress/cms/CMS_TEMPLATE/admin/categories.php on line 75)
                                $update_query = mysqli_query($connection, $query);

                                if(!$update_query){

                                    die("QUERY FAILED" . mysqli_error($connection));

                                }
                            }

                            ?>


                        </form>
                    </div>
                    <div class="col-lg-6">

                        <table class="table table-bordered table-hover">
                            <thead>
                            <tr>
                                <th>ID</th>
                                <th colspan="3">CATEGORY TITLE</th>
                            </tr>
                            </thead>
                            <tbody>

                            <?php  // delete category

                            $query = "SELECT * FROM categories"; // find categories query
                            $select_categories_sidebar = mysqli_query($connection, $query);

                            while ($row = mysqli_fetch_assoc($select_categories_sidebar)){

                                $cat_title = $row['cat_name'];
                                $cat_id = $row['id'];

                                echo "

                                <tr>
                                    <td>
                                        {$cat_id}
                                    </td>
                                    <td>
                                        {$cat_title}
                                    </td>
                                    <td>
                                        <a href='categories.php?edit={$cat_id}'>Edit</a>
                                    </td>
                                    <td>
                                        <a href='categories.php?delete={$cat_id}'>Delete</a>
                                    </td>
                                </tr>

                                ";
                            }

                            if (isset($_GET['delete'])) {
                                $del_cat_id = $_GET['delete'];

                                $query = "DELETE FROM categories WHERE id = {$del_cat_id} "; // delete category
                                $delete_query = mysqli_query($connection, $query);
                                header("location: categories.php");
                            }

                            ?>
                            </tbody>
                        </table>

                    </div>
                </div>
            </div>
            <!-- /.row -->

        </div>
        <!-- /.container-fluid -->

    </div>
    <!-- /#page-wrapper -->

</div>

空白页
副标题

您没有将
cat\u id
解析到
update\u类别
POST请求

将编辑表单代码更改为:

<form action="categories.php" method="post">

    <?php // edit category


    if (isset($_GET['edit'])){
        $cat_id = $_GET['edit'];
        $query = "SELECT * FROM categories WHERE id = {$cat_id}";

        $select_categories_id = mysqli_query($connection, $query);

    while ($row = mysqli_fetch_assoc($select_categories_id)){

        $cat_title = $row['cat_name'];
        $the_cat_id = $row['id']; ?>

        <div class="form-group">
            <label for="cat_name">Edit category name</label>
            <input value="<?php if(isset($cat_title)) {echo $cat_title;} ?>" class="form-control" type="text" name="cat_name">
            <input value="<?php if(isset($the_cat_id)) {echo $the_cat_id;} ?>" name="cat_id" type="text" />
        </div>
        <div class="form-group">
            <input class="btn primary" type="submit" name="update_category" value="Edit category">
        </div>
    <?php }

    }

    if (isset($_POST['update_category'])) {
        $update_cat_title = $_POST['cat_name'];
        $cat_id = $_POST['cat_id'];
       $query = "UPDATE categories SET `cat_name` = '{$update_cat_title}' WHERE `id` = '{$cat_id}' LIMIT 1"; // the problem is with  $cat_id (on videolesson everything works fine but on my local pc becomes next error - Notice: Undefined variable: the_cat_id in /var/www/html/wordpress/cms/CMS_TEMPLATE/admin/categories.php on line 75)

        $update_query = mysqli_query($connection, $query);

        if(!$update_query){

            die("QUERY FAILED" . mysqli_error($connection));

        }
    }

    ?>


</form>

编辑类别名称

你能告诉我们你收到的错误吗?谢谢,但它不起作用。我会从一开始就尝试重新制作。也许我丢了什么东西。在我这方面做得很好。你确定你合并了我添加的所有更改吗?它是有效的,但我得到的是$cat_id,不是从帖子中得到的。我有另一个按钮,它使用GET从类别表中获取id。我创建了一个if语句,检查是否单击了edit按钮,并加载表单以编辑特定类别。在视频中,老师也做了同样的事情,效果很好。但我不能那样做。。。php或mysql现在没有错误。我真的不明白为什么它不工作。表单是不希望使用get参数发布值。要发送值,我必须在表单操作中直接插入get参数,如下所示: