douyazi1129 2018-08-14 15:55
浏览 33
已采纳

在DB中编辑类别时出错

I have created a database with table categories. I create a functionality to add delete read and edit categories. But reading adding and deleting works fine but editing shows an error.

<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>
  • 写回答

1条回答 默认 最新

  • doubo6658 2018-08-14 16:48
    关注

    You are not parsing the cat_id through to the update_category POST request.

    Change your edit FORM code to this:

    <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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效