Unable to delete information from a table


#1

Hi, I am trying to help a friend whose PHP programmer went AWOL. Only I know very little about PHP myself. He has, on the backend of his site, a piece of functionality that allows a user to upload a filewhich will be linked to from the home page of the website. The code I want to implement is as follows:

Delete File

This link passes information on to a file called modules/class_news.php, which has several scenarios set up. The eigth scenario, which I’ve created, is as follows:

case 8:
    # Erase news
    // el archivo viejo lo borramos
    mysql_connect($dbhost,$usuario,$clave);

    // erase the registry
    mysql_db_query($dbname,"update db_news set `file` = '' where id = $id ");
    mysql_db_query($dbname,$sql);
   
    $url = "../news.php?acc=8";
    Header("Location: $url");
    break;

The error I am getting is as follows:

Warning: Cannot modify header information - headers already sent by (output started at /home/virtual/site48/fst/var/www/html/controlpanel/modules/class_news.php:130) in /home/virtual/site48/fst/var/www/html/controlpanel/modules/class_news.php on line 150

Anybody know an easy way to do this? Thanks!


#2

Warning: Cannot modify header information - headers already sent by (output started at class_news.php:130) in class_news.php on line 150

I’d advise you to learn what the various messages mean that you could get in PHP, and how to solve them. It’s really not that tough, since in most cases, the warning message tells you perfectly what’s going on.

Cannot modify header information: seems pretty straightforward.
Headers already sent by: explains why you cannot modify them.
(output started at class_news.php:130): this is the culprit: line 130.
in class_news.php on line 150: this is where you’re trying to resend headers.

Now, I’m thinking that the following is line 150:
[php]Header(“Location: $url”);[/php]

Now, you should know that after echoing/printing even a space or a character, headers are being sent and there’s no way to resend it (this is also a problem when setting cookies). And I’m thinking you’re using echo or print in line 130.


#3

Hi, thank you for your response! I understand the error message somewhat. My only problem is that the headers should not have been sent. The way the code appears to be set up is in cases. Line 130 exists in case 7, which states:

case 7:
	# Actualizar ultima news
	
	$title = quitar($HTTP_POST_VARS["title"]);
	// conecto
	
	if($file){
			// el archivo viejo lo borramos
			mysql_connect($dbhost,$usuario,$clave);
			$rs1 = mysql($dbname,"Select * From db_news Where id = $id ");
			$data1 = mysql_fetch_object($rs1);
			unlink("../../files/news/".$data1->file);
			
			$nombrefinal = $file_name; 
			copy($file, "../../files/news/".$nombrefinal);
	}		

unlink("…/…/files/news/".$data1->file); is line 130.

Line 150 is part of this statement, and is in bold:

    # Borrar news
    // el archivo viejo lo borramos
    mysql_connect($dbhost,$usuario,$clave);

    // borro el registro
    mysql_db_query($dbname,"update db_news set `file` = '' where id = $id ");
    mysql_db_query($dbname,$sql);
   
    $url = "../news.php?acc=8";
    [b]Header("Location: $url");[/b]
    break;

Theoretically, if the code were working properly, wouldn’t it never even access the information in line 130, and skip straight to case 8, which passes the headers for the first time?


#4

As far as I can tell… They are both in the same case. As case statements are setup as such

[code]Switch $num
{
case 1:
break;

case 2:
break;

case 3:
break;
}[/code]

As I can tell you have one case and both are contained in case 7. Unless I am missing the break and the case 8 or whatever the next case would be.


#5

@Heaversm:

Do you think you could put the source code online (in a zipfile or somethin) so we can have a look at the full source code?


#6

Thanks again! The source code is online at:

http://www.kaptivate.com/news.zip

Let me know if you have trouble accessing it.


#7

Hmm, the only thing I can think of, is to use @unlink() instead of unlink() and see if the problem still occurs. If not, then unlink() is failing, if so, it’s sending your headers prematurely.

On a sidenote, I see you’re using #, // and /* */ for comments. The use of # is valid for comments, but discouraged (and you don’t know if it’s gonna be supported in future versions of PHP). But that’s not the issue here :wink:


#8

It seems to be error free now, but the link to delete the file does not clear the field containing the file name.