-1

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

Here's the line throwing the error.

$result = mysql_query("INSERT INTO movies (id, title, year) VALUES($arr['title_id'], $arr['title'], $arr['year'])");    
1
  • 1
    plsease, please use prepared statements Commented Sep 8, 2011 at 21:17

7 Answers 7

3

You'll have to surround your variables in curly braces:

$result = mysql_query("INSERT INTO movies (id, title, year) VALUES({$arr['title_id']}, {$arr['title']}, {$arr['year']})");

Take a look at my complex (curly) braces explanation here: Problem escaping php variable

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

Comments

2

The code that worked for me is as follows:

$result = mysql_query("
INSERT INTO movies (id, title, year) 
VALUES(" . implode($arr,', ').";

Using implode you get all the items on the array, using the ', ' as separation character.

Comments

1

You should read the PHP documentation about strings to find out why your code is wrong.

Comments

1
$result = mysql_query("INSERT INTO movies (id, title, year) VALUES($arr['title_id'], $arr['title'], $arr['year'])"); 

I would do it this way:

$result = mysql_query("INSERT INTO movies (id, title, year) VALUES(" . $arr['title_id']. ", " . $arr['title'] . ", " . $arr['year'] . ")"); 

Comments

1

As others have mentioned, you should use curly braces, or string concatenation using the . operator to embed the variables in your string.

Additionally, assuming that the title column is a VARCHAR field and the other two are INT fields, you have to put quotes around the title value:

$result = mysql_query("INSERT INTO movies (id, title, year) VALUES(" . $arr['title_id'] . ", '" . $arr['title'] . "', " . $arr['year'] . ")");

You would have to do the same for the id and year fields if they were also VARCHAR fields.

Comments

1

This is a string parsing problem. See the PHP documentation on variable parsing. The problem is that the index is written without quotes when using simple syntax (without curly braces).

You can either use curly braces around your variables:

$result = mysql_query("INSERT INTO movies (id, title, year) VALUES({$arr['title_id']}, {$arr['title']}, {$arr['year']})");

Or you can concatenate strings and variables together with dots, avoiding string parsing altogether:

$result = mysql_query('INSERT INTO movies (id, title, year) VALUES('.$arr['title_id'].', '.$arr['title'].', '.$arr['year'].')');

Or leave out the quotes, but this doesn't look too clean:

$result = mysql_query("INSERT INTO movies (id, title, year) VALUES($arr[title_id], $arr[title], $arr[year])");

Comments

0

Not a direct answer, but I'd highly recommend moving to PHP Data Objects if possible. Supports many of the higher level mysql functions like prepared statements (no more worries of sql injection), pulling data directly into objects, etc.

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.