Passing variable but won,t delete

Discussion in 'PHP' started by ducca, Oct 8, 2006.

  1. #1
    Hi all,
    I'm on with learning php with some code I,ve downloaded ,
    the codes a simple quiz but will also let me edit and delete the questions,
    I,ve created the code top delete, but for some reason the code won,t delete the entry.
    I,m using xampp,mysl,php, on win xp and myphpadmin to create the databases.Input and output to the database is fine,
    I can see php is passing the correct id in the address bar but still won,t delete,
    thanks in advance for any help

    <?php

    include("contentdb.php");
    if(!isset($cmd))
    {
    $result = mysql_query("SELECT id, question FROM $table ORDER BY id",$db);

    echo "<table>";

    while ($row = mysql_fetch_array($result))
    {

    $id = $row["id"];
    $question = $row["question"];
    if ($alternate == "1")
    {
    $color = "#ffffff";
    $alternate = "2";

    }
    else
    {
    $color = "#efefef";
    $alternate = "1";
    }
    /*this is were I delete*/////////

    echo "<tr bgcolor=$color><td>$id:</td><td>$question</td><td><a href='deletequiz2.php?cmd=delete&id=$id'onClick=\"return confirm('Are you sure?')\">delete</a> ]</td></tr>";
    }
    }
    echo "</table>";
    ?>

    //////////////////////////////////
    And this is the seperate file in php to delete the entry

    <?php

    include("contentdb.php");
    if($_GET["cmd"]=="delete")
    {
    $sql = mysql_query("DELETE FROM $table WHERE id=$id",$db);
    //$result = mysql_query($sql);
    echo "Row deleted!";
    }
    ?>

    Thanks again
    ps Deleting is no problen in adminmyphp
     
    ducca, Oct 8, 2006 IP
  2. harsh789

    harsh789 Member

    Messages:
    29
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #2
    Try using $_GET to fetch value of id.

    include("contentdb.php");
    $id = $_GET["id"];
    if($_GET["cmd"]=="delete")
    {
    $sql = mysql_query("DELETE FROM $table WHERE id=$id",$db);
    //$result = mysql_query($sql);
    echo "Row deleted!";
    }
    PHP:
     
    harsh789, Oct 8, 2006 IP
  3. ducca

    ducca Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Cheers Matey,
    your a star,
    works a treat
    thanks again
     
    ducca, Oct 8, 2006 IP
  4. SoKickIt

    SoKickIt Active Member

    Messages:
    305
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    70
    #4
    Make sure you replace:

    $id = $_GET["id"];

    with:

    $id = intval($_GET["id"]);


    Something like this:

    deletequiz2.php?cmd=delete&id=1 or 1

    would empty your table.
     
    SoKickIt, Oct 8, 2006 IP