HELP ME FIND ERROR TO DELETE RECORD

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
happy_boy
Forum Newbie
Posts: 5
Joined: Wed Oct 01, 2008 9:22 pm

HELP ME FIND ERROR TO DELETE RECORD

Post by happy_boy »

I am unable to delete the record with following Code . WHERE IS ERROR in it ????

<?php
$host="localhost";
$username="username";
$password="mypassword";
$db_name="mydb";
$tbl_name="articles";

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

if ($task=="del") {

$SQL = " DELETE FROM articles ";
$SQL = $SQL . " WHERE id = $id ";
mysql_db_query($db_name, $SQL, $cid);

}


?>
<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 ("<TD><A HREF=\"manageedit.php?id=$id\">Edit</A></TD>");
echo ("<TD><A HREF=\"delete.php?id=$id&task=del\">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>
User avatar
pcoder
Forum Contributor
Posts: 230
Joined: Fri Nov 03, 2006 5:19 am

Re: HELP ME FIND ERROR TO DELETE RECORD

Post by pcoder »

First of all check your delete $SQL before passing it to the mysql_db_query().
And try to run it in PHPMYADMIN.
User avatar
The_Anomaly
Forum Contributor
Posts: 196
Joined: Fri Aug 08, 2008 4:56 pm
Location: Tirana, Albania

Re: HELP ME FIND ERROR TO DELETE RECORD

Post by The_Anomaly »

The fact that you're sticking a GET value directly into your query is the classic example of SQL Injection. Escape it, or you have probably the most well-known security flaw, in the most well known format.

Are you getting an error? Is your conditional even running? You say that if $task is equal to "del," then run the query--but I don't see $task defined anywhere.
happy_boy
Forum Newbie
Posts: 5
Joined: Wed Oct 01, 2008 9:22 pm

Re: HELP ME FIND ERROR TO DELETE RECORD

Post by happy_boy »

if ($task=="del") {

$SQL = " DELETE FROM articles WHERE id = $id ";

// $SQL = $SQL . " WHERE id = $id ";
mysql_db_query($db_name, $SQL, $cid);


}

This condition is not being executed. Any alternative to run it
happy_boy
Forum Newbie
Posts: 5
Joined: Wed Oct 01, 2008 9:22 pm

Re: HELP ME FIND ERROR TO DELETE RECORD

Post by happy_boy »

pcoder wrote:First of all check your delete $SQL before passing it to the mysql_db_query().
And try to run it in PHPMYADMIN.
Not being executed in program
if ($task=="del") {

$SQL = " DELETE FROM articles WHERE id = $id ";

// $SQL = $SQL . " WHERE id = $id ";
mysql_db_query($db_name, $SQL, $cid);


}
User avatar
ironhamster88
Forum Newbie
Posts: 3
Joined: Fri Oct 03, 2008 8:51 am
Location: Essex, UK

Re: HELP ME FIND ERROR TO DELETE RECORD

Post by ironhamster88 »

Code: Select all

mysql_connect('localhost', 'username', 'password');
mysql_select_db('db_name');
 
if(tsk == 'del') {
    $id = mysql_real_escape_string($_GET['id']);
 
    $query = "DELETE FROM articles WHERE id = '$id'";
    $result = mysql_query($query);
}
simple, but effective.
happy_boy
Forum Newbie
Posts: 5
Joined: Wed Oct 01, 2008 9:22 pm

Re: HELP ME FIND ERROR TO DELETE RECORD

Post by happy_boy »

Still Unable to Delete record.

Same Script is used in another program of mine it is working fine.
happy_boy
Forum Newbie
Posts: 5
Joined: Wed Oct 01, 2008 9:22 pm

Re: HELP ME FIND ERROR TO DELETE RECORD

Post by happy_boy »

<?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>
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

I made following changes to above program


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

if($cmd=='del')
{
$sql=("DELETE from $tbl_name WHERE id='$id'");
mysql_query($sql);
print $sql;
echo("User Deleted Successfully&cmd='none'");
exit();

}



echo ("<TD><A HREF=\"delete.php?id=$id&cmd=del\">Delete</A></TD>");
I need a solution for this Very important. I dont know why UPDATE and DELETE COMMAND NOT WORKING
Post Reply