View Full Version : PHP/MYSQL update function?

07-11-2005, 10:37 PM

I'm trying to do an update to a mysql record.
but seem to be getting some errors,

my code:

$update = $_REQUEST['update'];
$content = $_REQUEST['content'];
$section = $_REQUEST['section'];

mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database");
@mysql_select_db("$DBName") or die("Unable to select database $DBName");
$sqlquery = "UPDATE content SET cData='$content' WHERE id='$update'";

header("Location: admin.php?edit=$section");



Notice: Undefined variable: query in C:\Program Files\Apache Group\Apache2\htdocs\national\admin\func.php on line 14

line 14 is: mysql_query($query);

Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\Apache Group\Apache2\htdocs\national\admin\func.php:14) in C:\Program Files\Apache Group\Apache2\htdocs\national\admin\func.php on line 16

line 16: header("Location: admin.php?edit=$section");

why am i getting this header error? this is the only code in the script right now, so i'm a bit confused. also why is my update statement not working?

I thought it would be the same as the INSERT statement, cept use UPDATE insted??

thanks in advance for your time!

07-11-2005, 10:42 PM
ok, i've got the UPDATE working,
just needed to change the mysql_query($sqlquery); to reflect query statement. but just wondering why i get this header error so much?

thanks again for your time!

07-11-2005, 11:00 PM
header: outset already sent:

this means you have HTML inputted somewhere prior to the "location" line. This is usually cause by a blank line.

Kid Charming
07-11-2005, 11:02 PM
When you're using a relocation header, nothing can have been sent to the browser before that header function is called. In your original code, the undefined variable error message is being sent to the browser, so your header() call is getting hosed.

07-12-2005, 04:37 PM
yah, still getting this error:

Warning: Cannot modify header information - headers already sent by (output started at index.php:6) in index.php on line 124,

so how can i do a redirect if data has already been sent to the browser?


07-12-2005, 04:44 PM
hmm, ok i'm using this JavaScript to do it,, not sure that this is the best way though, but it works.

<script language="Javascript" type="text/javascript">
<!-- Hide script
//]]> End script hiding -->


07-12-2005, 04:49 PM
You should take a look at PHP output buffering. It's a very useful tool which will basically allow you to capture all browser output in a buffer variable. You can then decide if any headers need to be sent and finally echo your buffer variable as the last thing you do in the script. That will ensure that no content is sent before headers are sent :) Definitely much better than javascript, which won't work on everyone's browsers.

07-12-2005, 05:06 PM
kool, that sounds interesting, i'll look into it.


07-12-2005, 05:10 PM
...you have several blank lines in your code... also afaik the mysqlclose() wont execute cos its after the redirect.
If you're redirecting to another page then nothing should be sent to the browser since you're not displaying any of it

Kid Charming
07-12-2005, 06:47 PM
Actually, the redirect won't stop execution of the original script, so the mysql_close() should be fine. You could actually run the header() call very first thing and quietly run the UPDATE afterward, but you'd lose any error handling. Your die() would still kill the script, but the error message wouldn't be sent (though you could always set up an administrative email notification/log file to capture them).

07-12-2005, 07:38 PM
thanks for all the ideas guys!

I'm picking up php pretty fast because of this great forum! :thumbsup: