View Full Version : Delete Function Error
Titan85
01-08-2007, 02:36 AM
I am trying to make a function that will delete a news entry and each entry will have a delete function attached to it. Here is the code I have for it:
if($_GET['cmd'] == "delete") {
$sql = "DELETE FROM `news` WHERE id = '$id'";
$result = mysql_query($sql) or die ('Could not delete the post! <br />' .mysql_error());
echo 'The article has been deleted! <a href="edit_news.php">Return to Edit Page</a>';
}Here is the link that is added to each post:
<a href="edit_news.php?cmd=delete&id='.$id.'">Delete</a>The code looks like it should work to me, but when I click on the link, I get a blank page with no code on it and it does not delete the news post. I appreciate any help :)
djr33
01-08-2007, 03:29 AM
1. The html link doesn't seem to be within PHP. To have that work, you'd need php to be echoing he $id, not just typing that randomly in your html code. If it is being echoed, then that's fine.
2. I don't see you setting $id from $_GET['id']. That might be the issue as well.
If that's not it, then please link if possible or tell us what error(s) you are getting.
Titan85
01-08-2007, 03:39 AM
Ok, I added $id = $_GET['id']; to the code, but it still doesn't do anything. The code where the link is is in an echo statement:
if(!isset($cmd)) {
$sql = "SELECT * FROM `news` ORDER BY id DESC"; // SQL for query
$result = mysql_query($sql) or die ('Could not get news from the database! <br />' .mysql_error()); // Execute Query
while($r = mysql_fetch_array($result)) {
extract($r);
echo '
<b>'.$title.' Added on '.$date.'</b>
<br />
'.$message.'
<br />
<a href="edit_news.php?cmd=edit&id='.$id.'">Edit</a> | <a href="edit_news.php?cmd=delete&id='.$id.'">Delete</a>
<br /><br />
';
}Here is a link to the script: http://www.echo-designes.com/testing/news_cms/edit_news.php.When I click on the link for the first article, it takes me to the page, "http://www.echo-designes.com/testing/news_cms/edit_news.php?cmd=delete&id=3" but it is completely blank. I really don't know what it could be, hope this helps you figure it out. Thanks again
djr33
01-08-2007, 03:41 AM
Here's the issue:
if(!isset($cmd)) {
if NOT isset($cmd), then.... so it just skips that whole thing.
Take out the ! and see if that helps.
Titan85
01-08-2007, 04:05 AM
Here's the issue:
if(!isset($cmd)) {
if NOT isset($cmd), then.... so it just skips that whole thing.
Take out the ! and see if that helps.Well, now it just displays a blank page on edit_news.php. Have a look: http://www.echo-designes.com/testing/news_cms/edit_news.php . Any idea whats going on with that?
djr33
01-08-2007, 04:10 AM
Hmm... that's no good.
The thing is... you have an if, so what's the else?
to really understand this, seeing more (even all) of the code would be great.
Sorry, I just don't see anything yet.
Titan85
01-08-2007, 11:47 AM
Ok, here is my entire code for edit.php:
<?php
require('config.php');
if(isset($cmd)) {
$sql = "SELECT * FROM `news` ORDER BY id DESC"; // SQL for query
$result = mysql_query($sql) or die ('Could not get news from the database! <br />' .mysql_error()); // Execute Query
while($r = mysql_fetch_array($result)) {
extract($r);
echo '
<b>'.$title.' Added on '.$date.'</b>
<br />
'.$message.'
<br />
<a href="edit_news.php?cmd=edit&id='.$id.'">Edit</a> | <a href="edit_news.php?cmd=delete&id='.$id.'">Delete</a>
<br /><br />
';
}
?>
<?php
if($_GET['cmd'] == "edit") { // If edit link is selected
if(!isset($_POST['submit'])) { // If submit not pressed
$id = $_GET['id'];
$sql = "SELECT * FROM `news` WHERE id = '$id'";
$result = mysql_query($sql) or die (mysql_error());
$myrow = mysql_fetch_array($result);
?>
<form action="edit_news.php" method="post">
<input type=hidden name="id" value="<?php echo $myrow["id"] ?>">
<b>Title</b>:
<br />
<input type="text" name="title" value="<?php echo $myrow["title"] ?>" size=30>
<br /><br />
<b>Message</b>:
<br />
<textarea name="message" rows="10" cols="43"><? echo $myrow["message"] ?></textarea>
<br /><br />
<input type="hidden" name="cmd" value="edit">
<input type="submit" name="submit" value="Update">
</form>
<?
}
?>
<?php
if($_POST['submit']) {
// Define variables
$title = $_POST['title'];
$message = $_POST['message'];
$sql = "UPDATE `news` SET title = '$title', message = '$message' WHERE id = $id";
$result = mysql_query($result) or die ('Failded to update the news entry! <br />' .mysql_error());
echo 'Information has been updated. <a href="edit_news.php">Return</a>';
}
}
?>
<?php
if($_GET['cmd'] == "delete") {
$id = $_GET['id'];
$sql = "DELETE FROM `news` WHERE id = '$id'";
$result = mysql_query($sql) or die ('Could not delete the post! <br />' .mysql_error());
echo 'The article has been deleted! <a href="edit_news.php">Return to Edit Page</a>';
}
}
?>Hope you see something in there :)
mike_p
01-08-2007, 03:49 PM
I think djr33 missed the point. The first section with ! is designed to show the news when cmd is not set to edit or delete. So you can put the ! back in or you won't get to see selected news items!
When I checked your test page it was working. Don't forget that until you refresh the page, the item you have deleted will still show up.
[I also recommend taking the page down or password protecting it ASAP so that no script kiddies can play with your database!]
djr33
01-08-2007, 06:54 PM
Oh, hmm... yeah, I must have just confused both code bit, since I thought the first was a variation of the second. Sorry for skimming.
Glad it's working...
Titan85
01-08-2007, 09:05 PM
Hey, mike, how did you delete my posts on the script? When I tired to go to the page, it still would not have anything on it. Then I put the ! back in and it displays, but then it will not delete. I am confused...
mike_p
01-09-2007, 04:32 PM
I realised that without the ! it would not show any entries. So I sent the parameter cmd=xxx. That allowed me to see things but wasn't going to attempt an edit or delete.
I then simply clicked on delete and that worked fine. Having deleted a couple of items, I found a way to create my own item and added it. (Just so the page wouldn't be blank for anyone else!)
Titan85
01-09-2007, 08:50 PM
Hmm, I took the ! back out and did what you did and it worked fine, but the news articles do not show up on the edit_news.php. It seems that there is something preventing it from showing the news there when there is no !, but allowing the delete function to work. Then when the ! is added back in, it displays the articles on edit_news.php, but the delete function does not work. Anyone see something in the code that may be causing this?
djr33
01-09-2007, 10:09 PM
Well, then there's an error in the logic. You should find another way to check to see what it should do.
mike_p
01-10-2007, 09:21 PM
I think you problem is the oreder in which you do things.
You should first of all handle commands like edit or delete.
Then you should should show existing entries - and don't test isset($cmd) because you want to show existing entries whether or not you're sending a command.
Titan85
01-10-2007, 09:24 PM
I think you problem is the oreder in which you do things.
You should first of all handle commands like edit or delete.
Then you should should show existing entries - and don't test isset($cmd) because you want to show existing entries whether or not you're sending a command.Thanks for the idea, I'll try it and let you know how it works out :)
djr33
01-10-2007, 10:15 PM
Yeah. if ($cmd == THIS) do THAT; ...etc
Titan85
01-11-2007, 03:28 AM
Thanks a ton guys, I rewrote the code and got it working perfectly, even got an edit feature up and running :)
nagelkruid
09-19-2008, 03:28 PM
Thanks a ton guys, I rewrote the code and got it working perfectly, even got an edit feature up and running :)
Man, I am strugling with the exact same thing, driving me nuts.
Could you please post your working version of the delete and edit so i can learn from it.
Thanks!
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.