0

Here $id is integer value and it's not deleting from MySQL:

$Query="DELETE FROM table WHERE id='.$id.' and cid='".$cid."'";
6
  • 1
    OMG, SQL INJECTIONS - WELCOME!!!! PLEASE, use mysqli with prepared statements. Commented Apr 25, 2013 at 21:15
  • $Query="DELETE FROM table WHERE id=".mysql_real_scape_string($id)." and cid=".mysql_real_scape_string($cid).""; Commented Apr 25, 2013 at 21:16
  • 1
    I see two statements, and then a line of code. Not sure what to do with this... Commented Apr 25, 2013 at 21:17
  • @RobertRozas mysql_real_escape_string isn't much better, he should switch to mysqli or PDO. Also, how do you know he wasn't / isn't using mysql_real_escape_string somewhere before the line he posted? Commented Apr 25, 2013 at 21:18
  • Yes i know....but just seeing the way he is making the query, i realize he is using mysql extension...anyway, it was just a comment, not a full answer ;) Commented Apr 25, 2013 at 21:23

2 Answers 2

3

Your problem in short: you have mixed different quotation marks - " and '.

This problem would not arise if you would use prepared statements, as you would have had a single string literal:

$Query="DELETE FROM table WHERE id=? and cid=?";

This would also remove the possibility of SQL injections.

This would also speed-up you program if you need to execute the same prepared statement several times (the statement is already prepared and does not need to be parsed on the second+ invocation).

And finally, in case you are still using the officially deprecated PHP mysql extension you MUST switch to mysqli and use its full benefits like prepared statements. The mysql extension is no longer officially supported and may be removed in future (though I foresee that it will be moved to PEAR or so).


As a temporary solution, use mysql_real_escape_string to encode all variables which are derived from the user input. Please do NOT use mysql_escape_string as it is highly vulnerable to character encoding!

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

Comments

1

You forgot to close your "

The Solution:

$id = mysql_real_scape_string($id);
$cid = mysql_real_scape_string($cid);

$Query="DELETE FROM table WHERE id='".$id."' and cid='".$cid."'";

The Problem

So, if you were to echo out your statement as it was, the result would look like:

DELETE FROM table WHERE id='.1.' and cid='2'

See the problem with that?

2 Comments

What about $cid, wouldn't you escape that as well?
@Jasper Yes you would, I made a mistake and have since fixed it.

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.