动态INSERT INTO查询

时间:2014-10-24 14:46:17

标签: php sql

我正在做一个自动数据库填充程序,我遇到了一个我无法理解的错误。

我正在开发一个应该处理大量文件的脚本,其中包含多个值。文件中的信息具有以下结构:

id time_stamp(value1 value2 value3 value4 value5)* i(value1 value2 value3 value4 value5)* n(value1 value2) - >每行文件

i n (n = nGrupos)已知且 i 从1到 i 和< em> n 从1到2不等。每个括号集都要插入到不同的表中,因此下面是5个值。

我遇到问题的代码是(我无法使用其他括号集,但是如果它适用于它们,则它应该适用于其余部分):

$pos = 2;
$query = $con->query($query);
$rows = $stmt->fetch(PDO::FETCH_ASSOC);

for($i = 0; $i < $rows['nGrupos'] ; $i++)
{
    $stmt = "INSERT INTO `registo grupos majo` 
    VALUES ('',
           '1',
           '$line[1]',
           '$line[($pos+$i)]',  //line 109
           '$line[(($pos+1)+$i)]', 
           '$line[(($pos+2)+$i)]',
           '$line[(($pos+3)+$i)]',
           '$line[(($pos+4)+$i)]')";

   $query = $con->query($stmt);
}

当我尝试运行时,我收到错误:解析错误:

syntax error, unexpected '(', expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in (location of the .php) on the line 109.

1 个答案:

答案 0 :(得分:1)

$stmt = "INSERT INTO `registo grupos majo` 
    VALUES ('',
           '1',
           '" . $line[1] . "',
           '" . $line[($pos+$i)] . "',  #line 109
           '" . $line[(($pos+1)+$i)] . "', 
           '" . $line[(($pos+2)+$i)] . "',
           '" . $line[(($pos+3)+$i)] . "',
           '" . $line[(($pos+4)+$i)] . "')";
相关问题