Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts

    deleting news - cms

    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
    PHP Code:
    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
    Code:
    index.php?do=delete&id=2
    view source is as should
    Code:
    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

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Check your query to make sure it was successful.

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #4
    Regular Coder
    Join Date
    Jun 2006
    Posts
    225
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Echo mysql_error() after the query, if you haven't tried that already (or you don't report errors by default).

  • #5
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    ofcourse my first line is
    PHP Code:
    <?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

  • #6
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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.

  • #7
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    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....

  • #8
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    You keep saying that as though index.php is the wrong page for it to go to.

    But your link clearly says:

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

  • #9
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    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 Code:
    <?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($sqlMYSQL_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($sqlMYSQL_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;

    }
    ?>

  • #10
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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?

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

  • #11
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    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

  • #12
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    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


  •  

    Posting Permissions

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