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'])");
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
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.
$result = mysql_query("INSERT INTO movies (id, title, year) VALUES($arr['title_id'], $arr['title'], $arr['year'])");
我会这样:
$result = mysql_query("INSERT INTO movies (id, title, year) VALUES(" . $arr['title_id']. ", " . $arr['title'] . ", " . $arr['year'] . ")");
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.
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])");
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.