0

I getting error "Query error!" with this code:

$result = $connection->query("INSERT INTO EMP_TBLE (NAME, AGE, CATEGORY, UPDATE_COUNT) VALUES('$NAME', '$AGE', $CAT, 1
ON DUPLICATE KEY UPDATE UPDATE_COUNT = UPDATE_COUNT + 1"));
echo "<br>".$result;die;

I also check different example like this example was running success but I found error in above code for my demo project.

3
  • Where do you get $NAME and $AGE etc from? If these are from user input, be careful about SQL injection. You should be using parameter binding here. Commented Sep 10, 2015 at 13:36
  • at first you should use NAME with backticks NAME Commented Sep 10, 2015 at 13:40
  • Print your query and post it into the question. See what you are running. Most probably, you'll find the error yourself. Commented Sep 10, 2015 at 13:41

2 Answers 2

1

The closing brace for VALUES is missing. There is also a non-needed brace at the end of your code line.

Please change

$result = $connection->query("INSERT INTO EMP_TBLE (NAME, AGE, CATEGORY, UPDATE_COUNT) VALUES('$NAME', '$AGE', $CAT, 1 ON DUPLICATE KEY UPDATE UPDATE_COUNT = UPDATE_COUNT + 1"));

to

$result = $connection->query("INSERT INTO EMP_TBLE (NAME, AGE, CATEGORY, UPDATE_COUNT) VALUES('$NAME', '$AGE', $CAT, 1) ON DUPLICATE KEY UPDATE UPDATE_COUNT = UPDATE_COUNT + 1");
Sign up to request clarification or add additional context in comments.

Comments

0

Apparently you did not close the parentesis in the right place:

$result = $connection->query("INSERT INTO EMP_TBLE (NAME, AGE, CATEGORY, 
UPDATE_COUNT) VALUES('$NAME', '$AGE', $CAT, 1)
ON DUPLICATE KEY UPDATE UPDATE_COUNT = UPDATE_COUNT + 1");

Also... you should at least escape all the variables before concatenating in queries like this to avoid SQL injection

Comments

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.