473,385 Members | 1,912 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 1994
Dormilich
8,658 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

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

Similar topics

2
by: Barbara | last post by:
Hi, I have an sql database that has the primary key set to three fields, but has not been set as unique(I didn't create the table). I have 1 record that has 2 duplicates and I am unable to delete...
8
by: Steve | last post by:
I have several pairs of synchronized subforms in an application. I have a Delete button for each pair that uses the following code or similar to delete a record in the second subform: ...
3
by: Uwe Range | last post by:
Hi to all, I am displaying a list of records in a subform which is embedded in a popup main form (in order to ensure that users close the form when leaving it). It seems to be impossible to...
4
by: Susan Bricker | last post by:
I have a command button on a form that is supposed to Delete the record being displayed. The record is displayed one to a form. The form is not a Pop-Up nor is it Modal. Tracing the btnDelete...
7
by: travlintom | last post by:
Hi. I'm new to VB.net and am used to working with recordsets (much easier). I have a datagrid that, when the user double clicks on a row, I am giving them a chance to delete it. I thought this...
0
by: peterlewis | last post by:
Hi, I am unable to delete the current record from bindingnavigator - has a null bindingSource.Current although I can insert new records. The bindingnavigator was added as a control and the data...
2
by: mathewgk80 | last post by:
Hi all, i got an error "System.NotSupportedException: Deleting is not supported by data source 'SqlDataSource1' unless DeleteCommand is specified" when i try to delete a row.. the code is...
1
by: Markw | last post by:
Hi folks I think I've got a variable problem but not 100% sure. Background: I took the CMS example from chapter 6 in "Build your Own Database Driven Website Using PHP&MySQL" and have attempted to...
3
by: Constantine AI | last post by:
Hi i am trying to DELETE a Record if the details do not exist within a table. Here is my code below; Dim strSQL As String Dim strSQL2 As String Dim Reply As String Dim db As...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.