1

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); )
3
  • Its a varchar...some text including numbers and special characters Commented Aug 16, 2013 at 4:35
  • To debug this, I recommend you echo (or vardump) the contents of the query string, set a variable equal to the SQL text. e.g. $sql = "SELECT ... "; echo $sql; $r= mysql-query($sql); I strongly suspect that there are "special characters" which need to be escaped, or the length of the string exceeds the maximum length of the column. Less likely, there may be an issue with characterset conversion. But these are just guesses. (If this is new development, use mysqli_ or PDO instead of mysql_ interface.) Commented Aug 16, 2013 at 6:27
  • sorry i don't know either one of them... Commented Aug 16, 2013 at 9:28

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

13 Comments

Its still not working ... i don't know what the problem... here is the entire code 'code'
Are you sure $feed variable is not blank?
Its still not working here is the entire code @Sutandiono 'code' ($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'"); this one works but not in the way i want $r = mysql_query ("DELETE FROM feed WHERE feed = '" . mysql_real_escape_string ($feed) . "'") or die ("Query failed with {$feed} and mysql error: " . mysql_error); ) If it helps, $feed is a varchar.
no its not blank .. i used echo() and it shows the feed just fine.
Can you post the output from mysql_error? Also, which line worked but not in the way you wanted? I couldn't figure out from the comment above
|
0

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

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

1 Comment

this one also doesn't work for me @Becs Carter 'code' mysql_query("DELETE FROM feed WHERE feed = '{$feed}'") 'code' If it helps $feed is varchar
0

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);

1 Comment

it is very long...its a varchar(2000)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.