Warning: Cannot modify header information


#1

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]Edit info

<?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); ?>
<?php while($rows=mysql_fetch_array($result)){ ?> <?php } ?>
id model name L_oa L_blade L_handle bl_dik Steel weight status discription
<? $id[]=$rows['id']; ?><? echo $rows['id']; ?>
<?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();
?>[/php]


#2

Yep, you have text output before you send the header, which you can’t do. move that edit info to below the header().


#3

Hello, (thanks for your replay !) :slight_smile:

Can you direct me to where it is going wrong, and what have to change?

Yes I found a lot of information about sending output, but I do not understand what it exactly means. PHP and English are not my own language. ;D

The strange thing is in an other page it works?!?!
I’ve used it before, see:
[php] http://www.phphelp.com/forum/index.php?topic=16145.0 [/php]
it is slidely deferent.
There I only get debug information : Notice: Undefined variable: Submit in … line 60 , but it works !

I just trying to understand PHP and use it to manage a Dbase, a nice goal and I learn something.
The original code comes from :
[php]http://www.phpeasystep.com/mysql/10.html[/php]
If this is the wrong way of getting where I want to, can someone direct me to the right way? :’(


#4

Well, a header is sent as the first item to a browser. So, your PHP code is handled and executed SERVER-SIDE.
Next, the code is packaged with a header at the beginning (outputs of PHP mixed with HTML) and this is sent
to the browser. The browser pulls in any CSS and “renders” it all to the display. Next, any Javascript on the
page is available for use on the CLIENT-SIDE.

Now, you can not send a second header. So, think of HTML. It has ,etc…
You can see there is a header in there. Actually, once something is placed into the such as a title,
or form or any HTML, you can not pass a second header. So, you can create a PHP page that is just code.
Something like <?PHP //do some code; //do some compares; change headers; ?> This can be an entire
page of PHP which will redirect to the correct header/or page.

The simple way out is to put this test of your submit button before any HTML code on the page. (Above the Set a variable and then display your page based on the compare.


#5

Ok I get the picture,
After googleling I made it to this.
[php]Edit info

<?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); ?>
<?php while($rows=mysql_fetch_array($result)){ ?> <?php } ?>
id model name L_oa L_blade L_handle bl_dik Steel weight status discription
<? $id[]=$rows['id']; ?><? echo $rows['id']; ?>
[/php] I get no errors anymore :D 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 !!


#6

Okay, Rob, we need to step back and go over the basics…

First, normally, you create a HTML page with a FORM on it. This is full of whatever FIELDS you need.
Then, you create a second page, a PHP version. This takes the FIELDS posted from the first page and does whatever calculations that are needed and usually redirects to a second HTML/or/PHP page that does the
next part of the project…

So, your first posts shows a redirect inside a page of HTML with PHP… This can be done, but, not the way you have it coded.

The second version shows no redirect and no ACTION in the FORM. So, this page will just “POST” to itself…
If the page posts to itself, it can not go anywhere except itself.

Lastly, this code:
[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);
[/php]
So, if you look at that code, you will see that if the form is submitted, it does a query and then the next thing it does is another query, so the first query never actually does anything. If the form is submitted, the QUERY for that part never happens… You would have to change this to something like this:
[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());
}
}else{
$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);
}
[/php]
So, you will see that this minor change, adding the ELSE clause does one query if the form is submitted and the other if it is not submitted. But, you should rethink exactly what you are trying to do. Eventually, you will want to go somewhere else with this page instead of just reposting back to itself. So, we can help get you sorted out, but, where are you heading with it? Good luck, hope this helps you a bit…


#7

Thanks, it make sense the way you tell it.
I think this is out of my league.

The reason for this is the I have a messed up database. I have created new fields to get a structure. The most simple way to start cleaning this for me is using excel / access.

But I thought: how hard can it be, there are so many tutorials. I’ve tried a sereral and started to like PHP.
After testing a few tuts I liked this one best. After pushing Submit get back and review what you have don.
For this page I used a tut from a professor, so I thought how hard can it be……

So if someone knows a tut that does this trick correct. I’ve made the changes like ErnieAlex suggested, but still not working.

Thanks for reading and help ! ;D


#8

Okay, you finally explained what you are trying to do. Are you using MySQL on a live hosted site?
If so, you can use the PHPmyAdmin tool which gives you an excel type display of your database.
It is fairly easy to use. It is located here: http://www.phpmyadmin.net/home_page/index.php

Now, I looked over all of this thread and found all your discussions were about headers, etc.
Nothing about fixing database layouts or data. So, the PHPmyAdmin might help. There are other
database editors that will work for you also.

Making your own is fun and useful. If you want to change your current project to display and allow
changes of a table, you should use two pages. The first would be the page that displays the database table you want to change. When you change something, it should post out to the second PHP page which would update the data and then redirect back to the first page. (Redirection can be done inside the second PHP file since there will be no html code inside it!) Then, since you are starting back at the first page, it will reload the new version of the data from the database for further changes. I think this is what you are trying to do.

If you want help to go that way instead of PHPmyAdmin, repost your current code and we can help you sort it all out. Let us know…


#9

Sorry it took the long way to get there…

Making your own is fun and useful. If you want to change your current project to display and allow changes of a table, you should use two pages. The first would be the page that displays the database table you want to change. When you change something, it should post out to the second PHP page which would update the data and then redirect back to the first page. (Redirection can be done inside the second PHP file since there will be no html code inside it!) Then, since you are starting back at the first page, it will reload the new version of the data from the database for further changes. I think this is what you are trying to do.

If you want help to go that way instead of PHPmyAdmin, repost your current code and we can help you sort it all out. Let us know…

But I am familiar with phpmyadmin, the only problem is I can edit one record at the time.
In the early times I had over 8000 items. The Dbase is now so messy I only have over a 2000 items.

I don’t mind that I have to use 2 pages and use un other way to get back to the original.
That kind of java scripts are useful for this.

If I only had one (two pages) that really works I can make my other pages like that !

The tutorials I found where not working correct.
I build a lamp to test, afterwards a wamp, that seems not to be the problem.
Now I am working on a NAS and every thing runs the same like the wamp or lamp. (localhost)
Updating one record is no problem, I use that for counting product hits etc.

Only this part keeps me bugging and I really want to have it working.
So any suggestion is welcome.

And yes I would like to get this working :smiley:


#10

Well, perhaps you are not understanding what I meant.

It would be very simple to create a form page that displays a segment or all of your current database with input fields to allow changing the data. Then, you just post it to a PHP file that reads ALL of the changed values and updates the database. Then, it goes back to the original page showing the new values and lets you change them further. Isn’t that what you want?

To do this, just create a table that contains the records you need to change. Then, add input fields to the table to allow changes to be made. And, a submit button to post the changes. Simple!

If that is what you want, I took your original post and broke it up into two pages. Without access to your data, I am not sure it this will work. But, basically I broke it into a display page called admin1.php and a processing page called update1.php. Here they are… Hope it helps…

admin1.php
[php]
Edit info

<?php // 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); ?>
<?php while($rows=mysql_fetch_array($result)){ ?> <?php } ?>
id model name L_oa L_blade L_handle bl_dik Steel weight status discription
<? $id[]=$rows['id']; ?><? echo $rows['id']; ?>
[/php] (Just add your html page around the above...)

update1.php
[php]

<?php // Connect DBase include ("../inc/connect_Shop.inc.php"); // Check if button name "Submit" is active, do this if($submit){ foreach($_POST['id'] as $key=>$id){ $model = $_POST['model'][$key]; $name = $_POST['name'][$key]; $description = $_POST['description'][$key]; $length_oa = $_POST['length_oa'][$key]; $length_blade = $_POST['length_blade'][$key]; $blade_spine = $_POST['blade_spine'][$key]; $length_handle = $_POST['length_handle'][$key]; $weight = $_POST['weight'][$key]; $steel = $_POST['steel'][$key]; $status = $_POST['status'][$key]; $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='$key' "; $result1=mysql_query($sql1); } } mysql_close(); // Go back to display page... header("location: admin1.php");} ?>

[/php]
Please make sure to check the spelling of all the fields. Hope this works. It is supposed to, but, not sure if I missed anything… Let us know…


#11

Thank you !! So much, it looks promising!!

But at the update1.php I get an error :

Parse error: syntax error, unexpected ‘}’ in ……/admin/update1.php on line 30
Somehow I have a “}” to much (or missing one). :smiley:
( line 30 = header(“location: admin1.php”);} )

When I delete the “}” behind the:
[php] $result1=mysql_query($sql1);
}
}
mysql_close();

// Go back to display page…
header(“location: admin1.php”);
?>
[/php]
Then the page just flashes but doesn’t update the dbase.

Is it possible to display what is updated in page “update1.php” and use a “browser history back (java-script) button ?

I will look into it tomorrow, today I run out of time .

Thanks for you feedback!


#12
( line 30 = header("location: admin1.php");} )
Well, that line should NOT have a } in it... Just remove it...

If I typed it by mistake, I am sorry… Helping tooooo many people at one time! LOL…

Let me know if that works…

Oh, also, once you get this working a bit, you can add options to select various data in the SELECT query.
That way you could limit how much is shown on the screen to edit and make it all run faster. Just FYI…


#13

Thanks for the help again.

Yes it works !!
Sorry for the late replay.

Rob


#14

Great! Always nice to get a programming puzzle solved! See you in another thread…