471,319 Members | 1,364 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

Unable To Delete Record

[PHP]<?php
$host="localhost";
$username="user";
$password="rmypwd";
$db_name="mydb";
$tbl_name="articles";

$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("DELETE from $tbl_name WHERE id=$id ");
$sql=("DELETE from $tbl_name WHERE id='$id'");
mysql_query($sql);
print $sql;
echo("User Deleted Successfully&cmd='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="#0099CC"><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">&nbsp;</td>
<td width="64%" align="center" valign="top"><p align="left">&nbsp;<?

$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_array($retid)) {
$headline = $row["headline"];
$description = $row["description"];
$id = $row["id"];

echo ("<TR>");
echo ("<TD>$headline</TD>\n");
echo($cmd);
echo ("<TD><A HREF=\"manageedit.php?id=$id\">Edit</A></TD>");
echo ("<TD><A HREF=\"delete.php?id=$id&cmd=del\">Delete</A></TD>");
// echo ("<TD><A HREF=\"delete.php?&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">&nbsp;</td>
</tr>
</table></td>
</tr>
<tr>
<td bgcolor="#0099CC">&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&cmd='none'

However The record still exists in the database I am using php 4.47 version on server
Oct 9 '08 #1
8 1866
Dormilich
8,656 Expert Mod 8TB
maybe the missing ; at the end of the sql statement causes this (though I'm not sure about that)
Expand|Select|Wrap|Line Numbers
  1. $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
Oct 9 '08 #2
Atli
5,058 Expert 4TB
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:
Expand|Select|Wrap|Line Numbers
  1. DELETE FROM myTbl WHERE myID = $id LIMIT 1
  2.  
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:
Expand|Select|Wrap|Line Numbers
  1. $sql = "SELECT cols FROM tbl";
  2. $result = mysql_query($sql) or die("Query failed: ". mysql_error());
  3.  
That will stop the code execution and display the error returned by MySQL.
Oct 9 '08 #3
Markus
6,050 Expert 4TB
Please do not put things like 'URGENT' in the title of a thread.

Thank you.
Oct 9 '08 #4
pbmods
5,821 Expert 4TB
Heya, Harry.

Try adding a call to mysql_affected_rows() (http://php.net/mysql_affected_rows) to confirm that the row was actually deleted.
Oct 10 '08 #5
chelvan
90
hi
i've noted the followings on your code. please remove them & try your query again.

Expand|Select|Wrap|Line Numbers
  1. $cmd=$_GET["cmd"];
  2.  
remove the double code, use $_GET['cmd'];


2nd one...
Expand|Select|Wrap|Line Numbers
  1.  DELETE * FROM tb_name WHERE id='$id' ;
  2.  
the id may be an integer. so remove the code.

then try it

regards
chel-1
Oct 10 '08 #6
Markus
6,050 Expert 4TB
Chel, using double quotes in array key's doesn't affect them.
Oct 10 '08 #7
Atli
5,058 Expert 4TB
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.
Oct 10 '08 #8
chelvan
90
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
Oct 15 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

8 posts views Thread by Steve | last post: by
4 posts views Thread by Susan Bricker | last post: by
7 posts views Thread by travlintom | last post: by
1 post views Thread by Markw | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.