Warning: Cannot modify header information
Results 1 to 12 of 12

Thread: Warning: Cannot modify header information

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    10

    Angry Warning: Cannot modify header information

    I used this before and it should work.

    But for this page I keep getting an error:
    Warning: Cannot modify header information - headers already sent by (output started at.... line 60.... etc.

    (See code)
    When I use one single “id” there is no loop and it works fine.
    After using the original query I get the warning.
    The DBase gets new values but the GUI (html/PHP page) shows the old one.
    After pushing the button again the old value is put back.

    I’ve been google-ling but can’t find the answer.

    Is there a someone who sees what I’m doing wrong?

    PHP Code:
    <strong>Edit info</strong><br>
    <?php
    ini_set
    ('display_errors'1);
    error_reporting(E_ALL E_STRICT);
        
            
    // Connect DBase
                        
    include ("../inc/connect_Shop.inc.php");

    $sql="SELECT * FROM Prod WHERE manufacturer_id='6' ORDER BY model";
    // query for one single item         $sql="SELECT * FROM Prod WHERE id='113' ORDER BY model";
                                
    $result=mysql_query($sql);

    // Count table rows
    $count=mysql_num_rows($result);
    ?>
    <table width="500" border="1" cellspacing="1" cellpadding="0">
    <form name="form1" method="post" action="">
    <tr>
    <td>
    <table width="100%" border="1" color="white" cellspacing="1" cellpadding="0">
    <tr>
    <td align="center"><strong>id</strong></td>
    <td align="center"><strong>model</strong></td>
    <td align="center"><strong>name</strong></td>
    <td align="center"><strong>L_oa</strong></td>
    <td align="center"><strong>L_blade</strong></td>
    <td align="center"><strong>L_handle</strong></td>
    <td align="center"><strong>bl_dik</strong></td>
    <td align="center"><strong>Steel</strong></td>
    <td align="center"><strong>weight</strong></td>
    <td align="center"><strong>status</strong></td>
    <td colspan="13"><strong>discription</strong></td>
    </tr>
    <?php
    while($rows=mysql_fetch_array($result)){
    ?>
    <tr>
    <td align="center"><? $id[]=$rows['id']; ?><? echo $rows['id']; ?></td>
    <td align="center"><input name="model[]" type="text" id="model" value="<? echo $rows['model']; ?>" size="25"></td>
    <td align="center"><input name="name[]" type="text" id="name" value="<? echo $rows['name']; ?>" size="8"></td>
    <td align="center"><input name="length_oa[]" type="text" id="length_oa" value="<? echo $rows['length_oa']; ?>" size="8"></td>
    <td align="center"><input name="length_blade[]" type="text" id="length_blade" value="<? echo $rows['length_blade']; ?>" size="8"></td>
    <td align="center"><input name="length_handle[]" type="text" id="length_handle" value="<? echo $rows['length_handle']; ?>" size="8"></td>
    <td align="center"><input name="blade_spine[]" type="text" id="blade_spine" value="<? echo $rows['blade_spine']; ?>" size="8"></td>
    <td align="center"><input name="steel[]" type="text" id="steel" value="<? echo $rows['steel']; ?>" size="8"></td>
    <td align="center"><input name="weight[]" type="text" id="weight" value="<? echo $rows['weight']; ?>" size="8"></td>
    <td align="center"><input name="status[]" type="text" id="status" value="<? echo $rows['status']; ?>" size="8"></td>
    <td align="center"><input name="description[]" type="text" id="description" value="<? echo $rows['description']; ?>" size="50"></td>
    </tr>
    <?php
    }
    ?>

    </table>
    </td>
    </tr>
    <tr>
    <td colspan="11" align="center"><input type="submit" name="Submit" value="Submit"></td>
    </tr>
    </form>
    </table>
    <?php
    // Check if button name "Submit" is active, do this
    if($Submit){
    for(
    $i=0;$i<$count;$i++)
                {
        
    $sql1="UPDATE Prod SET model='$model[$i]', name='$name[$i]' , description='$description[$i]' ,    length_oa='$length_oa[$i]' , length_blade='$length_blade[$i]' ,    blade_spine='$blade_spine[$i]' , length_handle='$length_handle[$i]' , weight='$weight[$i]' , steel='$steel[$i]' , status='$status[$i]' WHERE id='$id[$i]' ";
        
                
    $result1=mysql_query($sql1);
            }


    if(
    $result1){header("location:forumquestion.php");}
    mysql_close();
    ?>

  2. #2
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,354
    The problem is that you're trying to use header() after you've got some output. Functions (such as header()) that modify the HTTP headers of the response to the client must be called before you have any output (e.g. anything outside of <?php .. ?> tags or passed to functions like echo, print, etc.).

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    10

    Thumbs up Header info used ?

    Hello, (thanks for your replay !)

    Yes I found that information too, but I do not understand what it exactly means.
    PHP and English are not my own language.

    I just trying to understand PHP and use it to manage a Dbase, a nice goal and I learn something.
    My code comes from :
    http://www.phpeasystep.com/mysql/10.htmlhttp://
    I’ve used it before, see:
    http://www.phpbuilder.com/board/show...php?t=10384006
    There I only get debug information : Notice: Undefined variable: Submit in … line 60 , but it works !

    If this is the wrong way of getting where I want to, can someone direct me to the right way?

  4. #4
    Beware: Crazy Scientist leatherback's Avatar
    Join Date
    Mar 2002
    Location
    Small border town between Netherlands and Germany
    Posts
    5,395
    As brad said: You cannot use the header() function after you have generatedpart of the page. So you rheader() command must come as the first thing the browser gets from the script.

    So start the page with this part:

    PHP Code:
    <?php
    // Check if button name "Submit" is active, do this
    if($Submit){
    for(
    $i=0;$i<$count;$i++)
                {
        
    $sql1="UPDATE Prod SET model='$model[$i]', name='$name[$i]' , description='$description[$i]' ,    length_oa='$length_oa[$i]' , length_blade='$length_blade[$i]' ,    blade_spine='$blade_spine[$i]' , length_handle='$length_handle[$i]' , weight='$weight[$i]' , steel='$steel[$i]' , status='$status[$i]' WHERE id='$id[$i]' ";
        
                
    $result1=mysql_query($sql1);
            }
    }

    if(
    $result1){header("location:forumquestion.php");}
    mysql_close();
    naturally, after the header() command you cannot close the mysql anymore, so move the mysql_close command up:
    PHP Code:
    if($result1)
      {
      
    mysql_close();
      
    header("location:forumquestion.php");
      } 
    Php 4.* to 5.*. A small step for programmers. But a leap for me!

  5. #5
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,354
    Quote Originally Posted by leatherback View Post
    after the header() command you cannot close the mysql anymore
    Why do you say that? It makes no difference whether you call mysql_close() before or after the call to header().

    What I would expect to see after the call to header(), however, would be an exit or a die() so that none of the subsequent code is executed.

  6. #6
    Junior Member
    Join Date
    Apr 2012
    Posts
    10

    Header informatio

    Thanks,

    I have made the changes but now it will not updata the data.
    This page is bugging me for 2 whole days now........

    The only error now is :
    Notice: Undefined variable: Submit in /volume1/web/01 ontwikkel/V44/admin/forumquestion.php on line 17 Notice: Undefined variable: result1 in /volume1/web/01 ontwikkel/V44/admin/forumquestion.php on line 27

    This is what I made:
    PHP Code:
    <strong>Edit info</strong><br>
    <?php
            ini_set
    ('display_errors'1);
                
    error_reporting(E_ALL E_STRICT);
        
            
    // Connect DBase
                        
    include ("../inc/connect_Shop.inc.php");

                
    $sql="SELECT * FROM Prod WHERE manufacturer_id='6' ORDER BY model";
                
    $result=mysql_query($sql);

                
    // Count table rows
                
    $count=mysql_num_rows($result);

                
    // Check if button name "Submit" is active, do this
                    
    if($Submit){
                    for(
    $i=0;$i<$count;$i++)
                {
                    
    $sql1="UPDATE Prod SET model='$model[$i]', name='$name[$i]' , description='$description[$i]' ,    length_oa='$length_oa[$i]' , length_blade='$length_blade[$i]' ,    blade_spine='$blade_spine[$i]' , length_handle='$length_handle[$i]' , weight='$weight[$i]' , steel='$steel[$i]' , status='$status[$i]' WHERE id='$id[$i]' ";
                        
    $result1=mysql_query($sql1);
                }
                                } 

    mysql_close();
    if(
    $result1){header("location:forumquestion.php");}

    ?>
    <table width="500" border="1" cellspacing="1" cellpadding="0">
    <form name="form1" method="post" action="">
    <tr>
    <td>
    <table width="100%" border="1" color="white" cellspacing="1" cellpadding="0">
    <tr>
    <td align="center"><strong>id</strong></td>
    <td align="center"><strong>model</strong></td>
    <td align="center"><strong>name</strong></td>
    <td align="center"><strong>L_oa</strong></td>
    <td align="center"><strong>L_blade</strong></td>
    <td align="center"><strong>L_handle</strong></td>
    <td align="center"><strong>bl_dik</strong></td>
    <td align="center"><strong>Steel</strong></td>
    <td align="center"><strong>weight</strong></td>
    <td align="center"><strong>status</strong></td>
    <td align="center"><strong>discription</strong></td>
    </tr>
    <?php
    while($rows=mysql_fetch_array($result)){
    ?>
    <tr>
    <td align="center"><? $id[]=$rows['id']; ?><? echo $rows['id']; ?></td>
    <td align="center"><input name="model[]" type="text" id="model" value="<? echo $rows['model']; ?>" size="25"></td>
    <td align="center"><input name="name[]" type="text" id="name" value="<? echo $rows['name']; ?>" size="8"></td>
    <td align="center"><input name="length_oa[]" type="text" id="length_oa" value="<? echo $rows['length_oa']; ?>" size="8"></td>
    <td align="center"><input name="length_blade[]" type="text" id="length_blade" value="<? echo $rows['length_blade']; ?>" size="8"></td>
    <td align="center"><input name="length_handle[]" type="text" id="length_handle" value="<? echo $rows['length_handle']; ?>" size="8"></td>
    <td align="center"><input name="blade_spine[]" type="text" id="blade_spine" value="<? echo $rows['blade_spine']; ?>" size="8"></td>
    <td align="center"><input name="steel[]" type="text" id="steel" value="<? echo $rows['steel']; ?>" size="8"></td>
    <td align="center"><input name="weight[]" type="text" id="weight" value="<? echo $rows['weight']; ?>" size="8"></td>
    <td align="center"><input name="status[]" type="text" id="status" value="<? echo $rows['status']; ?>" size="8"></td>
    <td align="center"><input name="description[]" type="text" id="description" value="<? echo $rows['description']; ?>" size="50"></td>
    </tr>
    <?php
    }
    ?>

    </table>
    </td>
    </tr>
    <tr>
    <td colspan="11" align="center"><input type="submit" name="Submit" value="Submit"></td>
    </tr>
    </form>
    </table>
    It's so depressing here at the Dutch coast...
    And today every snipped of code I work on isn't doing what I want it to do.

    Can you see what is wrong?

  7. #7
    Junior Member
    Join Date
    Apr 2012
    Posts
    10
    I've changed :

    if($result1)
    {
    mysql_close();
    {header("location:forumquestion.php");}
    }

    Bur that didn't the trick ?!?!

  8. #8
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,354
    General comment for all of your code: You need to find a consistent style of whitespace/indentation that suits you best and stick with it...
    Code:
    im p  r  o  per l   y sp  a c  e   d c o d ei s   h  a   rdt o re ad!    !!
    Next problem comes with this section of code:
    PHP Code:
                // Check if button name "Submit" is active, do this 
                    
    if($Submit){ 
                    for(
    $i=0;$i<$count;$i++) 
                { 
                    
    $sql1="UPDATE Prod SET model='$model[$i]', name='$name[$i]' , description='$description[$i]' ,    length_oa='$length_oa[$i]' , length_blade='$length_blade[$i]' ,    blade_spine='$blade_spine[$i]' , length_handle='$length_handle[$i]' , weight='$weight[$i]' , steel='$steel[$i]' , status='$status[$i]' WHERE id='$id[$i]' "
                        
    $result1=mysql_query($sql1); 
                } 
                                } 
    Where do you ever define the variables $Submit, $model, $name, ... etc. (basically $Submit plus every variable that appears inside of the definition of the $sql1 variable) ? As far as I can see, all of those variables are undefined (hence the first error message).

    Finally, this:
    PHP Code:
    if($result1)
    {
    mysql_close();
    {
    header("location:forumquestion.php");}

    doesn't make sense unless it appears within the block of code that checks if the form was submitted. Otherwise, $result1 won't be defined (hence the second error message).

  9. #9
    Beware: Crazy Scientist leatherback's Avatar
    Join Date
    Mar 2002
    Location
    Small border town between Netherlands and Germany
    Posts
    5,395
    Quote Originally Posted by bradgrafelman View Post
    Why do you say that? It makes no difference whether you call mysql_close() before or after the call to header().

    What I would expect to see after the call to header(), however, would be an exit or a die() so that none of the subsequent code is executed.
    I guess I am just a sucker for wanting to end things .
    Php 4.* to 5.*. A small step for programmers. But a leap for me!

  10. #10
    Junior Member
    Join Date
    Apr 2012
    Posts
    10

    Unhappy And now? what to do next

    Ok, white spaces indents ?!?!?!

    I've used before posting notepad++ to find them, but there seems to be any.....

    I am a real nope on this, new and I use snippets and try to learn something on the way.

    What is the best way to get it working?
    Can someone modify it correct?
    Any help is welcome..

    Thanks..

  11. #11
    Junior Member
    Join Date
    Apr 2012
    Posts
    10

    Uodate multiple rows

    Ok I get the picture,
    After googleling I made it to this.
    PHP Code:
    <strong>Edit info</strong><br>

    <?php
    ini_set
    ('display_errors'1);
    error_reporting(E_ALL E_STRICT);
        
    // Connect DBase
    include ("../inc/connect_Shop.inc.php");
        

    // Check if button name "Submit" is active, do this
    if (isset($_POST['Submit'])){
    for(
    $i=0;$i<$count;$i++)
                {
    $sql="UPDATE Prod SET model=`$model[$i]`, name=`$name[$i]` , description=`$description[$i]` ,    length_oa='$length_oa[$i]' , length_blade='$length_blade[$i]' ,    blade_spine='$blade_spine[$i]' , length_handle='$length_handle[$i]' , weight='$weight[$i]' , steel='$steel[$i]' , status='$status[$i]' WHERE id='$id[$i]' ";
    $result=mysql_query($sql) or die(mysql_error());
                }


    $sql="SELECT * FROM Prod WHERE manufacturer_id='6' ORDER BY model";
            
    // $sql="SELECT * FROM Prod WHERE id='113' ORDER BY model";

        
    $result=mysql_query($sql);

    // Count table rows
    $count=mysql_num_rows($result);
    ?>



    <table width="500" border="1" cellspacing="1" cellpadding="0">
    <form name="form1" method="post" action="">
    <tr>
    <td>
    <table width="100%" border="1" color="white" cellspacing="1" cellpadding="0">
    <tr>
    <td align="center"><strong>id</strong></td>
    <td align="center"><strong>model</strong></td>
    <td align="center"><strong>name</strong></td>
    <td align="center"><strong>L_oa</strong></td>
    <td align="center"><strong>L_blade</strong></td>
    <td align="center"><strong>L_handle</strong></td>
    <td align="center"><strong>bl_dik</strong></td>
    <td align="center"><strong>Steel</strong></td>
    <td align="center"><strong>weight</strong></td>
    <td align="center"><strong>status</strong></td>
    <td colspan="13"><strong>discription</strong></td>
    </tr>
    <?php
    while($rows=mysql_fetch_array($result)){
    ?>
    <tr>
    <td align="center"><? $id[]=$rows['id']; ?><? echo $rows['id']; ?></td>
    <td align="center"><input name="model[]" type="text" id="model" value="<? echo $rows['model']; ?>" size="25"></td>
    <td align="center"><input name="name[]" type="text" id="name" value="<? echo $rows['name']; ?>" size="8"></td>
    <td align="center"><input name="length_oa[]" type="text" id="length_oa" value="<? echo $rows['length_oa']; ?>" size="8"></td>
    <td align="center"><input name="length_blade[]" type="text" id="length_blade" value="<? echo $rows['length_blade']; ?>" size="8"></td>
    <td align="center"><input name="length_handle[]" type="text" id="length_handle" value="<? echo $rows['length_handle']; ?>" size="8"></td>
    <td align="center"><input name="blade_spine[]" type="text" id="blade_spine" value="<? echo $rows['blade_spine']; ?>" size="8"></td>
    <td align="center"><input name="steel[]" type="text" id="steel" value="<? echo $rows['steel']; ?>" size="8"></td>
    <td align="center"><input name="weight[]" type="text" id="weight" value="<? echo $rows['weight']; ?>" size="8"></td>
    <td align="center"><input name="status[]" type="text" id="status" value="<? echo $rows['status']; ?>" size="8"></td>
    <td align="center"><input name="description[]" type="text" id="description" value="<? echo $rows['description']; ?>" size="50"></td>
    </tr>
    <?php
    }
    ?>

    </table>
    </td>
    </tr>
    <tr>
    <td colspan="11" align="center"><input type="Submit" name="($_POST['Submit'])" value="Submit"></td>
    </tr>
    </form>
    </table>
    I get no errors anymore
    BUT.... it wil not update the data...
    I fount some artikels about "($_POST['Submit'])" so I also changed that.
    Am I on the right way ???

    By the way thanks for the help so far !!

  12. #12
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,354
    Few problems I see here:

    PHP Code:
    for($i=0;$i<$count;$i++) 
                { 
    $sql="UPDATE Prod SET model=`$model[$i]`, name=`$name[$i]` , description=`$description[$i]` ,    length_oa='$length_oa[$i]' , length_blade='$length_blade[$i]' ,    blade_spine='$blade_spine[$i]' , length_handle='$length_handle[$i]' , weight='$weight[$i]' , steel='$steel[$i]' , status='$status[$i]' WHERE id='$id[$i]' "
    $result=mysql_query($sql) or die(mysql_error()); 
                } 
    1. $count is undefined, so the contents that for() loop will never be executed.
    2. Where do you define $model, $name, $description, etc. ... again, every variable inside that SQL query string is undefined.
    3. You've got backticks (`) around some of the string values; string values must be delimited by single quotes (in MySQL, backticks are reserved for delimiting identifiers - such as names of databases/tables/columns/etc.).


    EDIT: Also note that the fact that you get no errors (despite the numerous errors you have) suggests that the above code isn't even being executed. Thus, you might want to do a print_r() on the entire $_POST array to verify what it contains.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •