472,983 Members | 2,747 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,983 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 1976
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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.