Page 1 of 1

cannot modify header information error

Posted: Wed May 16, 2012 12:23 pm
by Mythri
I have been trying to store image and display it from the database. Storing happens with some error. Display i couldn't do it. I am screwed up with the error.

Please help me. i want to do it in a single page. if i remove header relocation statement, warning goes but it will not check any conditions.

here is my complete code. please help me both in displaying and storing :(

Code: Select all

<?php

session_start(); 

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="style.css" rel="stylesheet" type="text/css" />
<title>Welcome</title>


<script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">
$(document).ready(function () { 

    $('#nav li').hover(
        function () {
            //show its submenu
            $('ul', this).slideDown(100);

        }, 
        function () {
            //hide its submenu
            $('ul', this).slideUp(100);         
        }
    );

});
    </script>
</head>

<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td class="header">&nbsp;</td>
  </tr>
  <tr>
    <td class="menu"><table align="center" cellpadding="0" cellspacing="0" width="80%">
    <tr>
    <td>

    <ul id="nav">
    <li><a href="#">Catalog</a>
    <ul><li><a href="#">Products</a></li>
        <li><a href="#">Bulk Upload</a></li>
        </ul>
        <div class="clear"></div>
        </li>


    <li><a href="#">Purchase  </a>

    </li>
    <li><a href="#">Customer Service</a>
    <ul>
        <li><a href="#">Contact Us</a></li>
        <li><a href="#">CS Panel</a></li>

    </ul>           
        <div class="clear"></div>
    </li>
    <li><a href="#">All Reports</a></li>
    <li><a href="#">Configuration</a>
    <ul> <li><a href="#">Look and Feel </a></li>
         <li><a href="#">Business Details</a></li>
         <li><a href="#">CS Details</a></li>
         <li><a href="#">Emaqil Template</a></li>
         <li><a href="#">Domain and Analytics</a></li>
         <li><a href="#">Courier</a></li>
         </ul>
    <div class="clear"></div>
    </li>
    <li><a href="#">Accounts</a>
    <ul><li><a href="#">Ledgers</a></li>
        <li><a href="#">Account Details</a></li>
        </ul>
         <div class="clear"></div></li>

</ul></td></tr></table></td>
  </tr>
  <tr>
    <td valign="top"><table width="80%" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="2">
          <tr>
            <td width="22%" height="327" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="2">
              <tr>
                <td>&nbsp;</td>
                </tr>
              <tr>
                <td height="45"><strong>-> Products</strong></td>
                </tr>
              <tr>
                <td height="61"><strong>-> Categories</strong></td>
                </tr>
              <tr>
                <td height="48"><strong>-> Sub Categories</strong></td>
                </tr>
            </table></td>
            <td width="78%" valign="top"><table width="100%" border="0" cellpadding="0" cellspacing="0">
              <tr>
                <td>&nbsp;</td>
                </tr>
              <tr>
                <td>
                  <table width="90%" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                      <td width="26%">&nbsp;</td>
                      <td width="74%"><h2>Manage Categories</h2></td>
                    </tr>
                  </table></td>
                </tr>
              <tr>
                <td height="30">&nbsp;

                </td>
                </tr>
              <tr>
                <td>


</td>
                </tr>

                <tr>
                <td>
                <table width="49%" align="center" cellpadding="0" cellspacing="0">
                <tr><td>
                <?php

if (isset($_SESSION['error']))

                {

                    echo "<span id=\"error\"><p>" . $_SESSION['error'] . "</p></span>";

                    unset($_SESSION['error']);

                }
                ?>
                <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">

                <p>
                 <label class="style4">Category Name</label>

                   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type="text" name="categoryname" /><br /><br />

                    <label class="style4">Category Image</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

                    <input type="file" name="image" /><br />

                    <input type="hidden" name="MAX_FILE_SIZE" value="100000" />

                   <br />
<br />
 <input type="submit" id="submit" value="UPLOAD" />

                </p>

                </form>

//to store the image into database
<?php

require("includes/conn.php");

function is_valid_type($file)

{

    $valid_types = array("image/jpg", "image/jpeg", "image/bmp", "image/gif", "image/png");



    if (in_array($file['type'], $valid_types))

        return 1;

    return 0;
}

function showContents($array)

{

    echo "<pre>";

    print_r($array);

    echo "</pre>";
}


$TARGET_PATH = "images/category/";

$cname = $_POST['categoryname'];

$image = $_FILES['image'];

$cname = mysql_real_escape_string($cname);

$image['name'] = mysql_real_escape_string($image['name']);

$TARGET_PATH .= $image['name'];


if ( $cname == "" || $image['name'] == "" )

{

    $_SESSION['error'] = "All fields are required";
    

   header("Location: managecategories.php");
   
   
 exit();
    

}

if (!is_valid_type($image))

{

    $_SESSION['error'] = "You must upload a jpeg, gif, or bmp";

    header("Location: managecategories.php");
   

    exit;

}

if (file_exists($TARGET_PATH))

{

    $_SESSION['error'] = "A file with that name already exists";

    header("Location: managecategories.php");
  

    exit;

}


if (move_uploaded_file($image['tmp_name'], $TARGET_PATH))

{



    $sql = "insert into Categories (CategoryName, FileName) values ('$cname', '" . $image['name'] . "')";

    $result = mysql_query($sql) or die ("Could not insert data into DB: " . mysql_error());

  header("Location: managecategories.php");


    exit;

}

else

{

    $_SESSION['error'] = "Could not upload file.  Check read/write persmissions on the directory";

    header("Location: mangagecategories.php");

    exit;

}

?> 

                  </td>
                </tr>
            </table></td>
          </tr>
          <tr><td height="25">&nbsp;</td></tr>
                <tr>
                  <td></td></tr>
                <tr>
                  <td valign="top">

                  //to display but not working
                  
                  <?php  
				  require("includes/conn.php"); 

                // Grab the data from the table

                $sql = "select CategoryID, CategoryName, FileName from Categories";

                $result = mysql_query($sql) or die ("Could not access DB: " . mysql_error());

 

                while ($row = mysql_fetch_assoc($result))

                {
					
					 echo "<table border='0' cellpadding='10'>";
                                        
                                      
                                        echo "<tr><td>  </td><td>Category ID</td><td>Category Name</td><td>Status</td><td>Edit</td><td>Delete</td></tr>";
										
										echo "<tr><td> <img src=\"images/" . $row['FileName'] . "\" alt=\"\" /> </td>";
										echo "<td>". $row['CategoryID'] . "</td>";
										    echo "<td>". $row['CategoryName'] . "</td>";
											echo "<td>". $row['Status']. "</td>";
											echo "<td> <a href= 'edit.php?CategoryID=" .$row['id']. "'> Edit </a></td>";
											echo "<td> <a href= 'delete.php?CategoryID=" .$row['id']. "'> Edit </a></td>";
										 echo "</tr> </table>";

                }

            ?>
                  
       
                  </td>
                </tr>
            </table></td>
          </tr>
          
        </table></td>
      </tr>
      
    </table></td>
  </tr>
  
  
</table>
</body>
</html>


Re: cannot modify header information error

Posted: Wed May 16, 2012 12:40 pm
by Celauran
header() needs to be called before any output is sent to the browser.

Re: cannot modify header information error

Posted: Wed May 16, 2012 12:44 pm
by Mythri
Can you please give me an example in my code. Please

Re: cannot modify header information error

Posted: Wed May 16, 2012 1:02 pm
by Mythri
Hey can you plz help me in this?

Re: cannot modify header information error

Posted: Wed May 16, 2012 1:07 pm
by Celauran
Move this

Code: Select all

<?php

require("includes/conn.php");

function is_valid_type($file)

{

    $valid_types = array("image/jpg", "image/jpeg", "image/bmp", "image/gif", "image/png");



    if (in_array($file['type'], $valid_types))

        return 1;

    return 0;
}

function showContents($array)

{

    echo "<pre>";

    print_r($array);

    echo "</pre>";
}


$TARGET_PATH = "images/category/";

$cname = $_POST['categoryname'];

$image = $_FILES['image'];

$cname = mysql_real_escape_string($cname);

$image['name'] = mysql_real_escape_string($image['name']);

$TARGET_PATH .= $image['name'];


if ( $cname == "" || $image['name'] == "" )

{

    $_SESSION['error'] = "All fields are required";
    

   header("Location: managecategories.php");
   
   
 exit();
    

}

if (!is_valid_type($image))

{

    $_SESSION['error'] = "You must upload a jpeg, gif, or bmp";

    header("Location: managecategories.php");
   

    exit;

}

if (file_exists($TARGET_PATH))

{

    $_SESSION['error'] = "A file with that name already exists";

    header("Location: managecategories.php");
  

    exit;

}


if (move_uploaded_file($image['tmp_name'], $TARGET_PATH))

{



    $sql = "insert into Categories (CategoryName, FileName) values ('$cname', '" . $image['name'] . "')";

    $result = mysql_query($sql) or die ("Could not insert data into DB: " . mysql_error());

  header("Location: managecategories.php");


    exit;

}

else

{

    $_SESSION['error'] = "Could not upload file.  Check read/write persmissions on the directory";

    header("Location: mangagecategories.php");

    exit;

}

?>
to the top of the file.

Re: cannot modify header information error

Posted: Wed May 16, 2012 1:31 pm
by Mythri
If i move that to file at the top it says too many redirects and page cannot be displayed.

Re: cannot modify header information error

Posted: Wed May 16, 2012 1:39 pm
by Celauran
First, wrap that code in a conditional such that it only executes if the form has been submitted.

Code: Select all

if (!empty($_FILES))
should do the trick.

Second, you haven't mentioned the name of the file in question. It's possible you've got it redirecting to itself, creating an infinite loop.

Re: cannot modify header information error

Posted: Wed May 16, 2012 1:50 pm
by Mythri
file name is managecategories.php

where and how to use if (!empty($_FILES)) function :(. I am not getting. Sorry

Re: cannot modify header information error

Posted: Wed May 16, 2012 1:58 pm
by Celauran

Code: Select all

<?php

session_start();

require("includes/conn.php");

function is_valid_type($file)
{
    $valid_types = array("image/jpg", "image/jpeg", "image/bmp", "image/gif", "image/png");

    if (in_array($file['type'], $valid_types))
        return 1;
    return 0;
}

function showContents($array)
{
    echo "<pre>";
    print_r($array);
    echo "</pre>";
}

if (!empty($_FILES))
{
    $TARGET_PATH = "images/category/";

    $cname = $_POST['categoryname'];
    $image = $_FILES['image'];
    $cname = mysql_real_escape_string($cname);

    $image['name'] = mysql_real_escape_string($image['name']);

    $TARGET_PATH .= $image['name'];


    if ($cname == "" || $image['name'] == "")
    {
        $_SESSION['error'] = "All fields are required";
        header("Location: managecategories.php");
        exit();
    }

    if (!is_valid_type($image))
    {
        $_SESSION['error'] = "You must upload a jpeg, gif, or bmp";
        header("Location: managecategories.php");
        exit;
    }

    if (file_exists($TARGET_PATH))
    {
        $_SESSION['error'] = "A file with that name already exists";
        header("Location: managecategories.php");
        exit;
    }


    if (move_uploaded_file($image['tmp_name'], $TARGET_PATH))
    {
        $sql = "insert into Categories (CategoryName, FileName) values ('$cname', '" . $image['name'] . "')";
        $result = mysql_query($sql) or die("Could not insert data into DB: " . mysql_error());
        header("Location: managecategories.php");
        exit;
    }
    else
    {
        $_SESSION['error'] = "Could not upload file.  Check read/write persmissions on the directory";
        header("Location: mangagecategories.php");
        exit;
    }
}
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <link href="style.css" rel="stylesheet" type="text/css" />
        <title>Welcome</title>


        <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript">
            $(document).ready(function () { 

                $('#nav li').hover(
                function () {
                    //show its submenu
                    $('ul', this).slideDown(100);

                }, 
                function () {
                    //hide its submenu
                    $('ul', this).slideUp(100);         
                }
            );

            });
        </script>
    </head>

    <body>
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
                <td class="header">&nbsp;</td>
            </tr>
            <tr>
                <td class="menu">
                    <table align="center" cellpadding="0" cellspacing="0" width="80%">
                        <tr>
                            <td>
                                <ul id="nav">
                                    <li><a href="#">Catalog</a>
                                        <ul>
                                            <li><a href="#">Products</a></li>
                                            <li><a href="#">Bulk Upload</a></li>
                                        </ul>
                                        <div class="clear"></div>
                                    </li>
                                    <li><a href="#">Purchase  </a></li>
                                    <li><a href="#">Customer Service</a>
                                        <ul>
                                            <li><a href="#">Contact Us</a></li>
                                            <li><a href="#">CS Panel</a></li>

                                        </ul>           
                                        <div class="clear"></div>
                                    </li>
                                    <li><a href="#">All Reports</a></li>
                                    <li><a href="#">Configuration</a>
                                        <ul>
                                            <li><a href="#">Look and Feel </a></li>
                                            <li><a href="#">Business Details</a></li>
                                            <li><a href="#">CS Details</a></li>
                                            <li><a href="#">Emaqil Template</a></li>
                                            <li><a href="#">Domain and Analytics</a></li>
                                            <li><a href="#">Courier</a></li>
                                        </ul>
                                        <div class="clear"></div>
                                    </li>
                                    <li><a href="#">Accounts</a>
                                        <ul>
                                            <li><a href="#">Ledgers</a></li>
                                            <li><a href="#">Account Details</a></li>
                                        </ul>
                                        <div class="clear"></div></li>
                                </ul>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td valign="top">
                    <table width="80%" border="0" align="center" cellpadding="0" cellspacing="0">
                        <tr>
                            <td valign="top">
                                <table width="100%" border="0" cellspacing="0" cellpadding="2">
                                    <tr>
                                        <td width="22%" height="327" valign="top">
                                            <table width="100%" border="0" cellspacing="0" cellpadding="2">
                                                <tr>
                                                    <td>&nbsp;</td>
                                                </tr>
                                                <tr>
                                                    <td height="45"><strong>-> Products</strong></td>
                                                </tr>
                                                <tr>
                                                    <td height="61"><strong>-> Categories</strong></td>
                                                </tr>
                                                <tr>
                                                    <td height="48"><strong>-> Sub Categories</strong></td>
                                                </tr>
                                            </table>
                                        </td>
                                        <td width="78%" valign="top">
                                            <table width="100%" border="0" cellpadding="0" cellspacing="0">
                                                <tr>
                                                    <td>&nbsp;</td>
                                                </tr>
                                                <tr>
                                                    <td>
                                                        <table width="90%" border="0" cellspacing="0" cellpadding="0">
                                                            <tr>
                                                                <td width="26%">&nbsp;</td>
                                                                <td width="74%"><h2>Manage Categories</h2></td>
                                                            </tr>
                                                        </table>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td height="30">&nbsp;

                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td>
                                                        <table width="49%" align="center" cellpadding="0" cellspacing="0">
                                                            <tr>
                                                                <td>
                                                                    <?php
                                                                    if (isset($_SESSION['error']))
                                                                    {
                                                                        echo "<span id=\"error\"><p>" . $_SESSION['error'] . "</p></span>";
                                                                        unset($_SESSION['error']);
                                                                    }
                                                                    ?>
                                                                    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">

                                                                        <p>
                                                                            <label class="style4">Category Name</label>

                                                                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type="text" name="categoryname" /><br /><br />

                                                                            <label class="style4">Category Image</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

                                                                            <input type="file" name="image" /><br />

                                                                            <input type="hidden" name="MAX_FILE_SIZE" value="100000" />

                                                                            <br />
                                                                            <br />
                                                                            <input type="submit" id="submit" value="UPLOAD" />
                                                                        </p>
                                                                    </form>
                                                                </td>
                                                            </tr>
                                                        </table>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td height="25">&nbsp;</td>
                                                </tr>
                                                <tr>
                                                    <td></td>
                                                </tr>
                                                <tr>
                                                    <td valign="top">
                                                        <?php

                                                        // Grab the data from the table
                                                        $sql = "select CategoryID, CategoryName, FileName from Categories";
                                                        $result = mysql_query($sql) or die("Could not access DB: " . mysql_error());

                                                        while ($row = mysql_fetch_assoc($result))
                                                        {

                                                            echo "<table border='0' cellpadding='10'>";
                                                            echo "<tr><td>  </td><td>Category ID</td><td>Category Name</td><td>Status</td><td>Edit</td><td>Delete</td></tr>";
                                                            echo "<tr><td> <img src=\"images/" . $row['FileName'] . "\" alt=\"\" /> </td>";
                                                            echo "<td>" . $row['CategoryID'] . "</td>";
                                                            echo "<td>" . $row['CategoryName'] . "</td>";
                                                            echo "<td>" . $row['Status'] . "</td>";
                                                            echo "<td> <a href= 'edit.php?CategoryID=" . $row['id'] . "'> Edit </a></td>";
                                                            echo "<td> <a href= 'delete.php?CategoryID=" . $row['id'] . "'> Edit </a></td>";
                                                            echo "</tr> </table>";
                                                        }
                                                        ?>
                                                    </td>
                                                </tr>
                                            </table>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    </body>
</html>
Having looked more closely at the code, though, this is very much solving the wrong problem. Why are you writing errors to session data and constantly redirecting to the same page?

Re: cannot modify header information error

Posted: Thu May 17, 2012 1:10 am
by Mythri
Hey, thank you very much. It really helped me. i haven't put the code at the beginning but i have used if (!empty($_FILES)) in the same place.
As you suggested i have removed the session and had put the echo to display errors. Now it works fine but when page loads tables content displays , after adding the image it is not displaying.
please correct my mistake

Re: cannot modify header information error

Posted: Thu May 17, 2012 11:20 am
by Mythri
Someone plz help me in this...