Ritesh kukreja
Ritesh kukreja

Reputation: 109

DELETE in mysql using PHP not working

I'm using php to delete a row from a table in a database in mysql, but it's not working. I'm not even trying to do anything fancy, just delete one row. Here's the code:

mysql_query("DELETE FROM feed WHERE feed = '$feed'") or die("Query failed! with '$feed'");

This just doesn't work while this does

mysql_query("DELETE FROM feed WHERE feed = 'hello'") or die("Query failed! with '$feed'");

Please help me...

Complete code (based on comment below):

$feed = $_POST['feed'];
$date = $_POST['date'];
$time = $_POST['time'];
echo $feed;
echo $date;
echo $time;
//$r = mysql_query("DELETE FROM feed WHERE date = '$date' AND time = '$time'") or die("Query failed! with '$feed'");
$r = mysql_query ("DELETE FROM feed WHERE feed = '" . mysql_real_escape_string ($feed) . "'") or die ("Query failed with {$feed} and mysql error: " . mysql_error); )

Upvotes: 1

Views: 2828

Answers (3)

Sutandiono
Sutandiono

Reputation: 1750

For debugging purpose, print mysql_error to get better idea on what's causing the error.

My guess is that your $feed might contain single quote. Get around that with mysql_real_escape_string

mysql_query ("DELETE FROM feed WHERE feed = '" . mysql_real_escape_string ($feed) . "'") or
die ("Query failed with {$feed} and mysql error: " . mysql_error());

On a side note, mysql extension is deprecated, so you might want to consider mysqli or PDO.

Upvotes: 1

Dave
Dave

Reputation: 386

When you say varchar, is this a fairly long string that contains maybe a URL?

If the string contains any backslashes these will be interpreted as escape characters. Also, you should never run a sql query directly from a POST variable without cleaning it with mysql_real_escape_string().

Also make sure the $feed var doent have any trailing spaces with trim($feed);

Upvotes: 0

Becs Carter
Becs Carter

Reputation: 1250

Try this... add the curly braces around your $feed variable

mysql_query("DELETE FROM feed WHERE feed = '{$feed}'")

Upvotes: 0

Related Questions