0

I'm trying to use the following code however it is giving me errors.

Code:

$id = $_GET['id'];
$action = '['command'=>'get','target'=>'location']';

$query = "UPDATE ZeusUsers SET action = '$action' WHERE notification_id = '$id'";
$result = mysqli_query($link,$query) or exit("Error in query: $query. " . mysqli_error());

Error:

Parse error: syntax error, unexpected 'command'

If I change the $action to a standard word the statement works fine, it just seems to have issues with the single quotes and square brackets.

I've also tried using \ in front of the single quotes and it still fails.

Any ideas?

3
  • What is that $action supposed to be? The syntax is completely broken as is. Should it be an array? Commented Apr 2, 2015 at 13:44
  • It's JSON that I need to call later on, so storing it in the DB until I need to call it. Commented Apr 2, 2015 at 13:46
  • If $id is an int cast it that way as the minimum to prevent injections. $id = (int)$_GET['id'];. Other ways to prevent injections, stackoverflow.com/questions/60174/… Commented Apr 2, 2015 at 13:53

1 Answer 1

2

let php build the json string for you

$action = json_encode(array('command'=>'get','target'=>'location'));

You are starting and stoping a string literal with the single quotes so php is interpreting command as php code but it doesn't know what that keyword is.

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

3 Comments

Produces the the following error: Warning: mysqli_error() expects exactly 1 parameter, 0 given in /assets/scripts/location.php on line 10 Error in query: UPDATE ZeusUsers SET action = '['command'=>'get','target'=>'location']' WHERE notification_id = '1111'.
Just used \ before all single quotes and seems to fix it! Thanks!
just noticed how you are interpolating the $action variable in the query. You really should be using prepared statements there. That would have solved the problem and is more secure.

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.