...

View Full Version : deleting news - cms



rafiki
06-05-2007, 04:15 AM
hi PHPLords :) im stuck once again lol
as you can can see im trying to delete a news article from a cms, heres the code for the delete part

case "delete" :
$id = $_GET['id'];
$deletesql = mysql_query("SELECT * FROM `news` WHERE id = $id");
while ($row = mysql_fetch_array($deletesql)){
echo('Delete News item: ' . $row['Title'] . '?
<form action="index.php?do=confirm_delete&id='.$id.'" method="GET">
<input type="submit" value="confirm">
<input type="button" name="cancel" value="cancel" onClick="javascript:history.go(-1)">
');
}
break;

case "confirm_delete" :
$id = $_GET['id'];
mysql_query("DELETE FROM `news` WHERE `id` = '$id'");
echo "News Deleted";

break;
the url is as should

index.php?do=delete&id=2
view source is as should

Delete News item: test page?
<form action="index.php?do=confirm_delete&id=2" method="GET">
<input type="submit" value="confirm">
<input type="button" name="cancel" value="cancel" onClick="javascript:history.go(-1)">
so all is good, apart from it doesnt delete i click confirm and it takes me back to index.php with no querystring :S
if its a simple mistake sorry its 4:15 am, i kinda took my time on anouther part of the project, so im making up for it :)
thanks,
and rep for solution as per :)
Rafiki

Fumigator
06-05-2007, 05:51 AM
Check your query to make sure it was successful.

Fou-Lu
06-05-2007, 07:27 AM
No querystring is kinda weird, considering its being passed via get.
The question would be does the "News Deleted" message appear?
If so, the only thing I can think of that would cause the sql to fail without an error (assuming as well that error reporting is currently enabled) is lack of permissions to delete from the database. Thats not very common though.
On a side note, I forgot that php can use strings in switch statements. Too used to java now, lol
Let us know if you solve the problem, I'm kinda curious now :P

name _F1
06-05-2007, 11:25 AM
Echo mysql_error() after the query, if you haven't tried that already (or you don't report errors by default).

rafiki
06-05-2007, 12:12 PM
ofcourse my first line is


<?php
error_reporting(E_ALL);

and now added mysql_error() which i should have done, but as i said it was 4:15 am, (check post time)
no error with mysql_error, and @ fumigator i no its not successfull because it takes me back to index.php? which shows the article i was trying to delete

Fumigator
06-05-2007, 03:45 PM
You know it wasn't successful, but you don't know why it wasn't successful. So you start with the basics-- echo mysql_error, echo the text of the query... if you don't find anything there, then make sure the code is even being executed by peppering the code with echo statements. Follow the echo statements to find out what's going wrong. Primative but effective.

rafiki
06-05-2007, 04:27 PM
i cant even get on to the page
index.php?do=confirm_delete&id=2
without it redirecting me back to just index.php unless i enter it manually and then it does the query, so maybe its an error in the form? as the query did delete it and echo'd news deleted aswell as index.php just saying no news check later....

aedrin
06-05-2007, 04:37 PM
You keep saying that as though index.php is the wrong page for it to go to.

But your link clearly says:


<form action="index.php?do=confirm_delete&id=2" method="GET">


takes me back to index.php with no querystring

Perhaps you should post more of the HTML that is supposed to do this, because the code you have posted appears correct. And it appears to be an HTML problem, not a PHP problem. Unless your confirm_delete attempts to forward the browser somewhere else.

rafiki
06-05-2007, 05:22 PM
i have no html lol its a blank page, that just all html is in the php code above :(
ill post the whole thing taking out a few lines =]


<?php
include("sqlconnect.php");
mysql_select_db($db);
$do = $_GET['do'];

if(!$do){
$sql = mysql_query("SELECT * FROM `news` ORDER BY `id` DESC");
$news = mysql_num_rows($sql);
if($news == 0){
echo('No News Yet, Check Back Later.');
}else{
while ($row = mysql_fetch_array($sql, MYSQL_ASSOC))
{
$id = $row["id"];
$title = $row["Title"];
$date = $row["Date"];
$content = $row["Content"];
echo($title.' Added on: '.$date.'<br />'.$content.'<br /><a href="index.php?do=delete&id='.$id.'">[Delete News]</A> <A href="index.php?do=edit&id='.$id.'">[Edit News]</A><br>');
}

}
}

switch ($do)
{
case "add" :
echo('
<form name="add_news" method="post" action="index.php?do=added_news">
Title: <input type="text" name="title"> <br>
Content: <TEXTAREA name="content" ROWS=10 COLS=70></TEXTAREA> <br>
<input type="submit" value="submit" name="submit" />
</form>');
break;

case "added_news" :
$date = date("F dS, Y h:i A");
$title = $_POST['title'];
$content = $_POST['content'];
$content = nl2br($content);

mysql_query("INSERT INTO news (Title, Date, Content) VALUES ('$title', '$date', '$content')") or die(mysql_error());

echo "$title Added Successfully!";
break;

case "delete" :
$id = $_GET['id'];
$deletesql = mysql_query("SELECT * FROM `news` WHERE id = $id");
while ($row = mysql_fetch_array($deletesql)){
echo('Delete News item: ' . $row['Title'] . '?
<form action="index.php?do=confirm_delete&id='.$id.'" method="GET">
<input type="submit" value="confirm">
<input type="button" name="cancel" value="cancel" onClick="javascript:history.go(-1)">
');
}
break;

case "confirm_delete" :
$id = $_GET['id'];
mysql_query("DELETE FROM `news` WHERE `id` = '$id'") or die(mysql_error());
echo "News Deleted";

break;

case "edit" :
$id = $_GET['id'];
$sql = mysql_query("SELECT * FROM `news` WHERE `id` = '$id'");
while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)){

$title = $row["Title"];
$date = $row["Date"];
$content = $row["Content"];


echo('
<form action="index.php?do=confirm_edit&id='.$id.'" method="POST">
Title: <input type="text" name="title" value="'.$title.'">
Date: <input type="text" name="date" value="'.$date.'" disabled="disabled" />
Content: <TEXTAREA name="content" ROWS=10 COLS=70>'.$content.'</TEXTAREA>
<input type="submit" value="submit" align="left">
');
}
break;

case "confirm_edit" :
$id = $_GET['id'];
$title = $_POST['title'];
$content = $_POST['content'];
$content = nl2br($content);

mysql_query("UPDATE news SET `Title` = '$title' WHERE `id` = '$id'");
mysql_query("UPDATE news SET `Content` = '$content' WHERE `id` = '$id'");

echo "You editted $title";
break;

}
?>

Fumigator
06-05-2007, 05:35 PM
It's highly likely that the lack of </form> tags is causing problems. You shoul always validate your HTML.

Side question: Why create a <form> tag for every news item? Why not use a regular old link?



echo "<a href=\"index.php?do=confirm_delete&id=$id\">Delete this news item</a>\n";

rafiki
06-05-2007, 05:51 PM
i added </form> elements to all open forms im going to change GET to POST for the form's that change the database; ill let you no if it fixes it

rafiki
06-05-2007, 05:59 PM
solved, it was because the form elements were using get that overiding the id=$id so i changed the form method to post and the querystring was working



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum