-1

Possible Duplicate:
MySQL - when to use single quotes, double quotes, and backticks?

i have this piece of code, i can't get to work properly.

require_once("../Packages/Connection.php");

        $text = mysql_real_escape_string($_POST["articleText"]);

        $method = $_POST['method'];
        $articleId = $_POST['articleId'];

        if($method == "update")
        {
            mysql_query("UPDATE Articles SET 'text'='".$text."' WHERE 'id'='".$articleId."'") or die(mysql_error());
        }

It is annoying me so much, This is the error i get - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''text'='tester2' WHERE 'id'='29'' at line 1...

Thank in advance

Community
  • 1
  • 1
JapSeyz
  • 35
  • 1
  • 8
  • 2
    I suggest escaping the $articleId as well, since your SQL is not this way. And even better you should use PDO and prepared statements. – Niborb Oct 10 '12 at 20:26
  • 1
    Have you checked the Mysql manual as suggested in the error message? – hakre Oct 10 '12 at 21:05

5 Answers5

1

Why do you have 'text' in your SQL...

It should simply be text as is and thats all: (Same thing for ID)

mysql_query("UPDATE Articles SET text = '".$text."' WHERE id='".$articleId."'") or die(mysql_error());

What you might have confused the "'" with, is the backtick or "`" that escapes characters and are good for reserved keywords...

Mathieu Dumoulin
  • 12,126
  • 7
  • 43
  • 71
  • Okay thanks :) but i had a friend help me, right after i posted this.. Sorry. Heres the answear mysql_query("UPDATE Articles SET text='$text' WHERE id='$articleId'") or die(mysql_error()); – JapSeyz Oct 10 '12 at 20:33
  • This is exactly what i posted except that you have used "Variable parsing" inside of a string while i ended the string cleanly. Depends on taste... – Mathieu Dumoulin Oct 10 '12 at 20:37
0
 mysql_query("UPDATE Articles SET text='".$text."' WHERE id='".$articleId."'") or die(mysql_error());
Lior
  • 5,841
  • 9
  • 32
  • 46
0

try

   mysql_query("UPDATE Articles SET `text`='".$text."' WHERE `id`='".$articleId."'")
Teena Thomas
  • 5,139
  • 1
  • 13
  • 17
0

First build the query, then execute it:

$sql = "UPDATE Articles SET 'text'='".$text."' WHERE 'id'='".$articleId."'";
$r = mysql_query($sql);
if (!$r) {
    echo "Query: ", $sql, "\n";
    echo "Error: ", mysql_error();
    die();
}

This will allow you to better review what exactly you've send to the database so that you can actually check the syntax as was suggested to you by the error message.

hakre
  • 193,403
  • 52
  • 435
  • 836
-2

you should use this

mysql_query("UPDATE Articles SET text ={$text} WHERE id ={$articleId}") or die(mysql_error ());
bolaji
  • 129
  • 1
  • 6