Jump to content


Won't delete! - Still need HELP


12 replies to this topic

#1 Dat

    Young Padawan

  • Members
  • Pip
  • 55 posts
  • Gender:Male

Posted 13 September 2007 - 08:49 PM

This code won't delete my entry!!! ARG and when I add: header("Refresh: edit.php"); after echo "The entry has been deleted"; it says an error: Warning: Cannot modify header information - headers already sent by (output started at /**/delete.php:22) in /**/delete.php on line 23

Here is the code:
<?php
//Connecting to the MySQL database
Connect...
//Naming some variables here.
$id = $_GET['id'];

if($_POST['deny']) {
//We shall not delete the entry, my lord.
$path = "http://admin.romanceanime.com/edit.php";
header("Location: $path");
exit;
} elseif($_POST['confirm']) {
//We shall proceed to delete!

//The MySQl query. Deletes an entry from the database.
$query = "DELETE FROM anime_reviews WHERE ID = '$id'";
//Execute the query.
$result = mysql_query($query) or die(mysql_error());
echo "The entry has been deleted";
// header("Refresh: edit.php");
} else {
?>
<table align="center">
<form action="delete.php" method="post">
<tr><td>Do you really want to delete this entry?</td></tr>
<tr><td><input type="hidden" name="id" value="<?php echo "$id"; ?>" /><input type="submit" name="confirm" value="Yes" /><input type="submit" name="deny" value="No" /></td></tr>
</form>
</table>
<?php
}
?>

I know I'm making these nooby topic's but this forum has improved my skills greatly thanks.

Edited by Dat, 15 September 2007 - 12:27 PM.


#2 Mr. Matt

    Moderator

  • P2L Staff
  • PipPipPipPip
  • 1,945 posts
  • Gender:Not Telling

Posted 13 September 2007 - 09:13 PM

A header modification can only be done in PHP before any content has been sent to the browser. In your case you have echo'd somthing to the browser before trying to do a header redirect.

Other options to fix what you are trying to do is do a javascript redirect which you can set to do after x seconds. Or you can redirect to a page to show your message and then redirect onto the page you want to go on to, a bit like what IPB does ( i think ). So e.g.:

When the item has been deleted you don't echo out but do a redirect like this:

header( 'Location: /show_message.php?message=1&redirect=edit.php' ); exit;

Then on your show_message.php page you do a simple check to see which message id has been sent, then show a message according to that ID, then you redirect off to the redirect page you sent across.

This is just basic, and has a few security flaws, but in a private admin area if its protected ok then it should be fine.

Matt

#3 Dat

    Young Padawan

  • Members
  • Pip
  • 55 posts
  • Gender:Male

Posted 13 September 2007 - 10:52 PM

Alright setting aside the redirect thing which I think I can solve later. Why isn't it deleting!?

#4 nitr0x

    Young Padawan

  • Members
  • Pip
  • 201 posts

Posted 14 September 2007 - 06:43 AM

<?php
	 //Connecting to the MySQL database
	 //Connect...
	 //Naming some variables here.
	 $id = $_GET['id'];
	 if( !$id ){
		  die("ID needs to be set.");
	 }

	 if( $_POST['deny'] ) {
		  //We shall not delete the entry, my lord.
		  $path = "http://admin.romanceanime.com/edit.php";
		  print '<meta http-equiv="Refresh" content="0; URL='. $path .'" />';
	 }elseif( $_POST['confirm'] ) {
		  //We shall proceed to delete!

		  //The MySQl query. Deletes an entry from the database.
		  mysql_query("DELETE FROM `anime_reviews` WHERE `id` = ".$id)or die( mysql_error() );
		  print '<meta http-equiv="Refresh" content="0; URL=edit.php" />The entry has been deleted.';
	 }else{
	 ?>
	 <table align="center">
	 <form action="delete.php" method="post">
	 <tr><td>Do you really want to delete this entry?</td></tr>
	 <tr><td><input type="hidden" name="id" value="<?php echo "$id"; ?>" />
	 <input type="submit" name="confirm" value="Yes" /><input type="submit" name="deny" value="No" />
	 </td>
	 </tr>
	 </form>
	 </table>
	 <?php
	 }
?>

Try that, changes:

1. Took out the header location changed it with meta refresh.
2. Add ID security, is it set?
3. Query changed to

mysql_query("DELETE FROM `anime_reviews` WHERE `id` = ".$id)or die( mysql_error() );

Edited by nitr0x, 14 September 2007 - 06:44 AM.


#5 Mr. Matt

    Moderator

  • P2L Staff
  • PipPipPipPip
  • 1,945 posts
  • Gender:Not Telling

Posted 14 September 2007 - 07:03 AM

The reason why it wouldn't delete as it got to a php error which means the whole script would not run, in turn not deleteing the image, changing the redirect and getting that to work fine may get it working, but like nitr0x has done, you may get an error with your query. So fix the header redirect first because for now that is your main problem.

Matt

#6 Dat

    Young Padawan

  • Members
  • Pip
  • 55 posts
  • Gender:Male

Posted 14 September 2007 - 09:15 AM

How do I set the id?

#7 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 14 September 2007 - 02:54 PM

 Mr. Matt, on Sep 14 2007, 06:03 AM, said:

The reason why it wouldn't delete as it got to a php error which means the whole script would not run, in turn not deleteing the image, changing the redirect and getting that to work fine may get it working, but like nitr0x has done, you may get an error with your query. So fix the header redirect first because for now that is your main problem.

Matt
Wrong, headers() cause non-fatal runtime errors, meaning, the error will show up at the point the script err's. Everything upto that point and typically beyond will be executed.

Parse errors however, are fatal errors and would do as you described (not allow the script to run).

 Dat, on Sep 14 2007, 08:15 AM, said:

How do I set the id?
	 //Naming some variables here.
	 $id = $_GET['id'];
Now i'm confused. You're already setting the id, do you mean how to you pass the id to the script?

http://php.net/manual/en/reserved.variable...d.variables.get

#8 Dat

    Young Padawan

  • Members
  • Pip
  • 55 posts
  • Gender:Male

Posted 14 September 2007 - 06:57 PM

 rc69, on Sep 14 2007, 12:54 PM, said:

 Mr. Matt, on Sep 14 2007, 06:03 AM, said:

The reason why it wouldn't delete as it got to a php error which means the whole script would not run, in turn not deleteing the image, changing the redirect and getting that to work fine may get it working, but like nitr0x has done, you may get an error with your query. So fix the header redirect first because for now that is your main problem.

Matt
Wrong, headers() cause non-fatal runtime errors, meaning, the error will show up at the point the script err's. Everything upto that point and typically beyond will be executed.

Parse errors however, are fatal errors and would do as you described (not allow the script to run).

 Dat, on Sep 14 2007, 08:15 AM, said:

How do I set the id?
	 //Naming some variables here.
	 $id = $_GET['id'];
Now i'm confused. You're already setting the id, do you mean how to you pass the id to the script?

http://php.net/manual/en/reserved.variable...d.variables.get

Before alter the code(before posting this thread) it printed: success but it never did delete it.
After altering the code it still doesn't work but now it says ID needs to be set because of this:
if( !$id ){
die("ID needs to be set.");
}


#9 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 14 September 2007 - 09:29 PM

Lol, simple problem guys.

Your form is using the POST method, thus, your variable will be available not in the $_GET superglobal array, but in $_POST.

$id = (int)$_POST['id'];


#10 Dat

    Young Padawan

  • Members
  • Pip
  • 55 posts
  • Gender:Male

Posted 14 September 2007 - 10:03 PM

Still not working:

<?php
//Connect to database
//Naming some variables here.
$id = (int)$_POST['id'];
if( !$id ){
die("ID needs to be set.");
}
if( $_POST['deny'] ) {
//We shall not delete the entry, my lord.
$path = "http://admin.romanceanime.com/edit.php";
print '<meta http-equiv="Refresh" content="0; URL='. $path .'" />';
}elseif( $_POST['confirm'] ) {
//We shall proceed to delete!
//The MySQl query. Deletes an entry from the database.
mysql_query("DELETE FROM `anime_reviews` WHERE `id` = ".$id)or die( mysql_error() );
print '<meta http-equiv="Refresh" content="0; URL=edit.php" />The entry has been deleted.';
}else{
?>
<table align="center">
<form action="delete.php" method="post">
<tr><td>Do you really want to delete this entry?</td></tr>
<tr><td><input type="hidden" name="id" value="<?php echo "$id"; ?>" />
<input type="submit" name="confirm" value="Yes" /><input type="submit" name="deny" value="No" />
</td>
</tr>
</form>
</table>
<?php
}
?>


#11 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 15 September 2007 - 12:30 PM

Lol, demonslay, you want a simple problem eh? Wheres the id coming from now?
<input type="hidden" name="id" value="<?php echo "$id"; ?>" />
<?php
//Connect to database
//Naming some variables here.
$id = (int)$_POST['id'];
if( !$id ){
	die("ID needs to be set.");
}

if( $_POST['deny'] ) {
	/* .... */
}else{
?>
	<table align="center">
	<form action="delete.php" method="post">
	<tr><td>Do you really want to delete this entry?</td></tr>
	<tr><td><input type="hidden" name="id" value="<?php echo "$id"; ?>" />
	<input type="submit" name="confirm" value="Yes" /><input type="submit" name="deny" value="No" />
	</td>
	</tr>
	</form>
	</table>
<?php
}
?>
$id = (int)($_GET['id'] ? $_GET['id'] : $_POST['id']);

p.s. Dat, please format that code a bit, it's impossible to read...

Edited by rc69, 15 September 2007 - 12:31 PM.


#12 Dat

    Young Padawan

  • Members
  • Pip
  • 55 posts
  • Gender:Male

Posted 15 September 2007 - 09:48 PM

 rc69, on Sep 15 2007, 10:30 AM, said:

Lol, demonslay, you want a simple problem eh? Wheres the id coming from now?
<input type="hidden" name="id" value="<?php echo "$id"; ?>" />
<?php
//Connect to database
//Naming some variables here.
$id = (int)$_POST['id'];
if( !$id ){
	die("ID needs to be set.");
}

if( $_POST['deny'] ) {
	/* .... */
}else{
?>
	<table align="center">
	<form action="delete.php" method="post">
	<tr><td>Do you really want to delete this entry?</td></tr>
	<tr><td><input type="hidden" name="id" value="<?php echo "$id"; ?>" />
	<input type="submit" name="confirm" value="Yes" /><input type="submit" name="deny" value="No" />
	</td>
	</tr>
	</form>
	</table>
<?php
}
?>
$id = (int)($_GET['id'] ? $_GET['id'] : $_POST['id']);

p.s. Dat, please format that code a bit, it's impossible to read...
Thanks,

So what was the error? Why did this work?
$id = (int)($_GET['id'] ? $_GET['id'] : $_POST['id']);

BTW: Can you refer me to a good php editor; I currently use Coda from Panic. Also, I use a Mac.

#13 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 15 September 2007 - 10:23 PM

Notepad's a good editor. Personally though, i use dreamweaver... auto-tab/color coding helps a lot!

For the fix though, ref: http://php.net/opera...rison#id2539511

Edited by rc69, 15 September 2007 - 10:23 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users