简体   繁体   中英

Warning: Cannot modify header information, what is wrong in my code

enter image description here

what is wrong in my code, when every i try to delete a record its giving warning message ..... please help me

Warning: Cannot modify header information - headers already sent by (output started at C:\\xampp\\htdocs\\CMS\\admin\\Categories.php:10) in C:\\xampp\\htdocs\\CMS\\admin\\Categories.php on line 181

<div id="wrapper">

    <!-- Navigation -->

<?php include "includes/Navigation.php";?>


        <!-- Top Menu Items -->
        <ul class="nav navbar-right top-nav">
            <li><a href="../index.php"> Home Page </a></li>

                   <li class="dropdown">
                <a href="#" class="dropdown-toggle" data-toggle="dropdown"><i class="fa fa-user"></i> John Smith <b class="caret"></b></a>
                <ul class="dropdown-menu">
                    <li>
                        <a href="#"><i class="fa fa-fw fa-user"></i> Profile</a>
                    </li>

                    <li class="divider"></li>
                    <li>
                        <a href="#"><i class="fa fa-fw fa-power-off"></i> Log Out</a>
                    </li>
                </ul>
            </li>
        </ul>
        <!-- Sidebar Menu Items - These collapse to the responsive navigation menu on small screens -->
        <div class="collapse navbar-collapse navbar-ex1-collapse">
            <ul class="nav navbar-nav side-nav">
                <li>
                    <a href="index.html"><i class="fa fa-fw fa-dashboard"></i> Dashboard</a>
                </li>

                <li>
                    <a href="javascript:;" data-toggle="collapse" data-target="#posts_dropdown"><i class="fa fa-fw fa-arrows-v"></i> posts <i class="fa fa-fw fa-caret-down"></i></a>
                    <ul id="posts_dropdown" class="collapse">
                        <li>
                            <a href="#">view all pasts</a>
                        </li>
                        <li>
                            <a href="#">add posts</a>
                        </li>
                    </ul>
                </li>

                <li>
                    <a href="bootstrap-grid.html"><i class="fa fa-fw fa-wrench"></i>  Categories</a>
                </li>


                <li>
                    <a href="blank-page.html"><i class="fa fa-fw fa-file"></i> Comments </a>
                </li>


                 <li>
                    <a href="javascript:;" data-toggle="collapse" data-target="#demo"><i class="fa fa-fw fa-arrows-v"></i> Users <i class="fa fa-fw fa-caret-down"></i></a>
                    <ul id="demo" class="collapse">
                        <li>
                            <a href="#">Dropdown Item</a>
                        </li>
                        <li>
                            <a href="#">Dropdown Item</a>
                        </li>
                    </ul>
                </li>


                <li>
                    <a href="index-rtl.html"><i class="fa fa-fw fa-dashboard"></i> Profile</a>
                </li>
            </ul>
        </div>



        <!-- /.navbar-collapse -->
    </nav>

    <div id="page-wrapper">

        <div class="container-fluid">




            <!-- Page Heading -->
            <div class="row">
                <div class="col-lg-12">
                    <h1 class="page-header">
                        Welcome to Admin
                        <small> Rajsekhar</small>
                    </h1>

                    <div class="col-xs-6">
                        <?php 

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

                                if ($cat_title == "" || empty($cat_title)) {
                                    # code...

                                    echo "this field should not be empty ";
                                } else {

                                    $query = "INSERT INTO category(cat_title) VALUES ('{$cat_title}')";
                                    $create_cat_query = mysqli_query($connection,$query);

                                    if(!$create_cat_query){

                                        die('query failed' . mysqli_error($connection));
                                    }

                                }

                            }
                         ?>
                        <form action="" method="POST"> 
                            <div class="form-group">

                                <label for="cat-title">Add Category</label>
                                <input type="text" class="form-control" name="cat_title">
                            </div>

                            <div class="form-group">
                                <input type="submit" name="submit" class="btn btn-primary" value="Add Category">
                            </div>

                        </form>
                    </div>


                    <!-- Add category form -->
                    <div class="col-xs-6">

                    <table class="table table-bordered table-hover">
                    <thead>
                        <tr>
                            <th>Id</th>
                            <th>category title</th>
                        </tr>
                    </thead>
                    <tbody>
                     <?php

                        $query = "SELECT * FROM category;";
                        $slect_all_category = mysqli_query($connection,$query);
                        while($row = mysqli_fetch_assoc($slect_all_category)){

                        $cat_id = $row['cat_id'];
                        $cat_title = $row['cat_title'];
                        echo "<tr>";
                        echo "<td> {$cat_id}</td>";
                        echo "<td> {$cat_title}</td>";
                        echo "<td> <a href = 'Categories.php?delete={$cat_id}'> Delete </a>";
                        echo "</tr>";
                        }
                        ?>
                        <tr>

                        </tr>                     
                    </tbody>
                    </table>


                    <?php 

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

                            $query = "DELETE FROM Category where cat_id = {$delete_id} ";

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

                        header("Location: Categories.php");    

                            if(!$delete_query)
                            {
                                die('Query Failed' . mysqli_error($connection) );
                            }


                        }

                    ?>



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

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

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

Move your post logic at the beginning of the page.

Once the html part of your script starts it automatically starts pushing data to the browser and the headers of the response should go first. Then you include some php file that probably wants to return an error to the browser but the output has already started and so the headers can't be modified so the php gives this error

<?php 

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

                            $query = "DELETE FROM Category where cat_id = {$delete_id} ";

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

                        header("Location: Categories.php");    

                            if(!$delete_query)
                            {
                                die('Query Failed' . mysqli_error($connection) );
                            }


                        }

                    ?>

//rest of the page

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM