Comum
Comum

Reputation: 453

Dynamic INSERT INTO query

I'm doing a automatic database filler and I've come accross a error I cannot understand.

I am developing a script that is supposed to handle lots of files, containing several values. The information in the files has a structure that follows:

id time_stamp (value1 value2 value3 value4 value5)*i (value1 value2 value3 value4 value5)*n (value1 value2) -> per line of the file

i and n (n = nGrupos) are known and i varies from 1 to i and n varies from 1 to 2. Each brackets set is to be inserted into a different table, hence the 5 values below.

The code that I'm having trouble is (i can't get the other brackets sets to work with, but if it works for this one them it should work for the rest):

$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);
}

When I try and run this I get the error: Parse error:

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.

Upvotes: 0

Views: 73

Answers (1)

Popnoodles
Popnoodles

Reputation: 28409

$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)] . "')";

Upvotes: 1

Related Questions