涉及连字符的PHP MySQL语法错误

$names = array();
$names['full-name'] = 'John Doe';

$query = "INSERT INTO users (full_name) VALUES ('$names[full-name]')";
$result = mysql_query($query) or die(mysql_error());

I have been getting syntax errors whenever I attempt to create a mysql query using an array key that contains a hyphen:

Parse error: syntax error, unexpected '-', expecting ']'

How am I able to fix this? Thank you.

Since it hasn't been mentioned yet, here's an another alternative syntax:

$query = "INSERT INTO users (full_name) VALUES ('{$names['full-name']}')";

Adding the {} braces around the variable/array index allows you to quote it as you would if it were outside of the string. The {} notation also allows you to embed a multi-dimensional array inside a string, e.g.

echo "$x[1][2]";

would normally be see as array $x[1] followed by literal [2] text by PHP, since its array parsing is not 'greedy'. But putting braces aroudn the whole array reference forces PHP parse the entire thing.

Having a - in a variable is invalid. Use a _ instead

$query = "INSERT INTO users (full_name) VALUES ('".$names[full-name]."')";

a) Take a look at PDO (specifically prepared statements), you might be vulnerable to SQL Injection by simply using variables

b) If you just want to get that piece fixed:

$query = "INSERT INTO users (full_name) VALUES ('".$names["full-name"]."')";

('$names[full-name]') should be changed to ('$names[\"full-name\"]')