Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: Delete Function Error

  1. #1
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Delete Function Error

    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:
    PHP Code:
    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:
    HTML Code:
    <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

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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:
    PHP Code:
    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.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

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by djr33 View Post
    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.../edit_news.php . Any idea whats going on with that?

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  7. #7
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Ok, here is my entire code for edit.php:
    PHP Code:
    <?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

  8. #8
    Join Date
    Jan 2007
    Location
    Surrey, UK
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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!]

  9. #9
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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...
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  10. #10
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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...

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •