By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,888 Members | 1,058 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,888 IT Pros & Developers. It's quick & easy.

Unable To Delete Record

P: 1
[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
Share this Question
Share on Google+
8 Replies


Dormilich
Expert Mod 5K+
P: 8,639
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
Expert 5K+
P: 5,058
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
Expert 5K+
P: 6,050
Please do not put things like 'URGENT' in the title of a thread.

Thank you.
Oct 9 '08 #4

pbmods
Expert 5K+
P: 5,821
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
P: 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
Expert 5K+
P: 6,050
Chel, using double quotes in array key's doesn't affect them.
Oct 10 '08 #7

Atli
Expert 5K+
P: 5,058
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
P: 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.