Unable To Delete Record

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • harryjohal
    New Member
    • Oct 2008
    • 1

    Unable To Delete Record

    [PHP]<?php
    $host="localhos t";
    $username="user ";
    $password="rmyp wd";
    $db_name="mydb" ;
    $tbl_name="arti cles";

    $cmd=$_GET["cmd"];
    $id=$_GET['id'];

    $cid = mysql_connect($ host,$username, $password);
    if (!$cid) { echo("ERROR: " . mysql_error() . "\n"); }

    mysql_select_db ('db_name');

    if($cmd=='del')
    {
    // mysql_query("DE LETE from $tbl_name WHERE id=$id ");
    $sql=("DELETE from $tbl_name WHERE id='$id'");
    mysql_query($sq l);
    print $sql;
    echo("User Deleted Successfully&cm d='none'");
    exit();

    }


    ?>
    <html>
    <head>
    <title>Edit Lyrics</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>

    <body>
    <div align="center">
    <table width="700" border="0">
    <tr>
    <td bgcolor="#0099C C"><p>&nbsp; </p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p></td>
    </tr>
    <tr>
    <td><table width="100%" border="0" align="center">
    <tr>
    <td width="18%" align="center" valign="top">&n bsp;</td>
    <td width="64%" align="center" valign="top"><p align="left">&n bsp;<?

    $SQL = " SELECT * FROM $tbl_name ";
    $retid = mysql_db_query( $db_name, $SQL, $cid);
    if (!$retid) { echo( mysql_error()); }
    else {
    echo ("<P><TABLE CELLPADDING=4>\ n");
    while ($row = mysql_fetch_arr ay($retid)) {
    $headline = $row["headline"];
    $description = $row["descriptio n"];
    $id = $row["id"];

    echo ("<TR>");
    echo ("<TD>$headline </TD>\n");
    echo($cmd);
    echo ("<TD><A HREF=\"manageed it.php?id=$id\" >Edit</A></TD>");
    echo ("<TD><A HREF=\"delete.p hp?id=$id&cmd=d el\">Delete</A></TD>");
    // echo ("<TD><A HREF=\"delete.p hp?&cmd=del&id\ ">Delete</A></TD>");
    echo ("</TR>");
    }
    echo ("</TABLE>");
    }
    ?>
    </p>
    <p>
    </p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p></td>
    <td width="18%" align="center" valign="top">&n bsp;</td>
    </tr>
    </table></td>
    </tr>
    <tr>
    <td bgcolor="#0099C C">&nbsp;</td>
    </tr>
    </table>
    </div>
    </body>
    </html>[/PHP]


    This is the message I get After executing the command

    DELETE from articles WHERE id='69'User Deleted Successfully&cm d='none'

    However The record still exists in the database I am using php 4.47 version on server
    Last edited by Atli; Oct 9 '08, 11:01 AM. Reason: Added [code] tags.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    maybe the missing ; at the end of the sql statement causes this (though I'm not sure about that)
    Code:
    $sql="DELETE from $tbl_name WHERE id=\'$id\';";
    mysql_query() will return false if the query was not successful. then you may use mysql_error() to get more information.

    regards

    PS please use [code] tags when posting code

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      Originally posted by Dormilich
      maybe the missing ; at the end of the sql statement causes this (though I'm not sure about that)
      Nope, that has no effect.

      I am guessing that this query is failing because of the added quote-tags around your integer ID. (I assume it is an integer?).

      Only string data should be quoted. If you quote a number, it will be treated as a string, and the string "1" is not the same as the number 1.

      Try something like:
      [code=mysql]
      DELETE FROM myTbl WHERE myID = $id LIMIT 1
      [/code]
      You should also make sure you clean up the user input before using it, or you are vulnerable to SQL Injection. (Any novice hacker could easily wipe out your database if you were using that code on a live web.)

      Also, as Dormilich says, the mysql_query function returns false when it fails, so you could use that to detect if the query fails.

      The standard method is to do something like:
      [code=php]
      $sql = "SELECT cols FROM tbl";
      $result = mysql_query($sq l) or die("Query failed: ". mysql_error());
      [/code]
      That will stop the code execution and display the error returned by MySQL.

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Please do not put things like 'URGENT' in the title of a thread.

        Thank you.

        Comment

        • pbmods
          Recognized Expert Expert
          • Apr 2007
          • 5821

          #5
          Heya, Harry.

          Try adding a call to mysql_affected_ rows() (http://php.net/mysql_affected_rows) to confirm that the row was actually deleted.

          Comment

          • chelvan
            New Member
            • Aug 2008
            • 90

            #6
            hi
            i've noted the followings on your code. please remove them & try your query again.

            Code:
            $cmd=$_GET["cmd"];
            remove the double code, use $_GET['cmd'];


            2nd one...
            Code:
             DELETE * FROM tb_name WHERE id='$id' ;
            the id may be an integer. so remove the code.

            then try it

            regards
            chel-1

            Comment

            • Markus
              Recognized Expert Expert
              • Jun 2007
              • 6092

              #7
              Chel, using double quotes in array key's doesn't affect them.

              Comment

              • Atli
                Recognized Expert Expert
                • Nov 2006
                • 5062

                #8
                Originally posted by chelvan
                the id may be an integer. so remove the code.

                then try it
                To clarify, you mean quotes, not code... right?

                If you do, then yes. Like I said earlier; numbers should not be quoted.

                Comment

                • chelvan
                  New Member
                  • Aug 2008
                  • 90

                  #9
                  Originally posted by Atli
                  To clarify, you mean quotes, not code... right?

                  If you do, then yes. Like I said earlier; numbers should not be quoted.

                  yes.
                  you are right.

                  chel-1

                  Comment

                  Working...