Forum Moderators: open

Message Too Old, No Replies

SQL Delete, PHP

Need help.

         

unclekracker23

12:31 am on Feb 27, 2008 (gmt 0)

10+ Year Member



I am using a small news script. The script allows the adding and editing of news. I am trying to add the deletion of news. Here is my editnews.php file that I am trying to get the Delete working. BTW, I am a newb to PHP and SQL.


<?
//include our database connection file
include('dbconnect.php');

if($_POST['action']=="doedit"){

//grab the post vars
$title = $_POST['title'];
$id = $_POST['id'];
$news = $_POST['news'];

//update the database
$news = "UPDATE news SET title='$title', news='$news' WHERE id = $id";
$editnews = mysql_query($news);
echo("news edited.");
}

//print the news titles, with links to the edit page
$getnews = mysql_query("select * from news ORDER BY id DESC");
while($r=mysql_fetch_array($getnews)){
extract($r);
echo("> $title<a href=editnews.php?id=$id&action=edit> Edit</a>");
echo("> <a href=editnews.php?id=$id&action=delete> Delete</a><br />");
}
echo("<br /><br />");

//if we are editing a news item, print the following..
if($_GET['action']=="edit"){
$id = $_GET['id'];
$getnews = mysql_query("select * from news WHERE id=$id");
while($r=mysql_fetch_array($getnews)){
extract($r);
//our form

//if we are deleting a news item, print the following..
if($_GET['action']=="delete"){
$query1=mysql_query("delete from news where id=$id");
mysql_query($dbname, $query1) or die("Failed Query of " . $query1);
echo("> $title . $id . ' has been deleted as requested'.<br>");
}
//our form

?>

<form action="editnews.php" method="POST">
<input type="text" name="title" value="<? echo($title); ?>" /><br />
<textarea name="news" rows="6" cols="50"><? echo($news); ?></textarea>
<input type="submit" value="edit" />
<input type="hidden" name="id" value="<? echo($id); ?>" />
<input type="hidden" name="action" value="doedit" />
</form>
<?

}
}
?>

Thanks for any help you can give.

ZydoSEO

3:03 pm on Feb 27, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




You stated you are trying to DELETE, but you did not say what the problem was that you are trying to solve. Are you getting an error when you attempt to delete.

If you are getting some type of FORIEGN KEY VIOLATION related error then more than likely the ID for the news article you are attempting to delete is also being used as a foreign key in other tables. If this is the case, you need to delete all of the rows that reference ID in other related tables before attempting to delete the row for ID from NEWS.

unclekracker23

4:03 pm on Feb 27, 2008 (gmt 0)

10+ Year Member



Oh, I am sorry. The Delete is not working at all. Like I said earlier, I do not know much about PHP and SQL commands so the Delete section in the above script was written using samples found online.

When I click the delete link next to the news thread I want to delete nothing happens. I refresh the page to see if the topics are gone but they are still there.

jatar_k

5:05 pm on Feb 27, 2008 (gmt 0)