简体   繁体   English

使用数组中的PHP变量将其插入SQL数据库。 (语法错误)

[英]Using PHP variables from an array to insert into SQL database. (Syntax error)

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING 解析错误:语法错误,意外的T_ENCAPSED_AND_WHITESPACE,预期为T_STRING或T_VARIABLE或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 在这里看看我complex (curly) braces解释: php变量转义问题

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. 使用implode,您可以使用' , '作为分隔符来获取数组中的所有项目。

您应该阅读有关字符串PHP文档,以了解代码错误的原因。

$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: 此外,假设title列是VARCHAR字段,而其他两个是INT字段,则必须在title值两边加上引号:

$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. 如果idyear字段也是VARCHAR字段,则必须执行相同的操作。

This is a string parsing problem. 这是一个字符串解析问题。 See the PHP documentation on variable parsing . 请参阅有关变量解析PHP文档 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. 这不是一个直接的答案,但是我强烈建议您尽可能迁移到PHP数据对象 Supports many of the higher level mysql functions like prepared statements (no more worries of sql injection), pulling data directly into objects, etc. 支持许多更高级别的mysql函数,例如准备好的语句(不再担心sql注入),将数据直接拉入对象等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM