0

I have an array with the data below:

arr_fields[0] = 'a bigint'
arr_fields[1] = 'b bit'

I want to structure it just like the array structure below:

$fields = array(
            'a' => array(
                'type' => 'bigint',             
            ),
            'b' => array(
                'type' => 'bit',                
            )
);

Below is the rest of the code and it's working fine.

$schema = array(
    'description' => t ('Contains a particular templates data inputted from the MyID Inputting System'),
    'fields' => $fields,
);

db_create_table('myid_template_' . $template_id, $schema);

I tried the code below to structure it like the above, but it throws an error

foreach ($arr_fields as $value) {
    $arr = explode(' ' , $value);
    $fields[$arr[0]] = array('type' => $arr[1]);
}

$schema = array(
    'description' => t ('Contains a particular templates data inputted from the MyID Inputting System'),
    'fields' => $fields,
);

db_create_table('myid_template_' . $template_id, $schema);

An error says:

<em class="placeholder">PDOException</em>: SQLSTATE[42601]: Syntax error:  7 ERROR:  syntax error at end of input
LINE 4: )
     ^: CREATE TABLE {myid_template_489} (
a ,
b 
); Array
(
)
in <em class="placeholder">db_create_table()</em> (line <em class="placeholder">2720</em> of <em class="placeholder">C:\xampp\htdocs\drupal-7.34\includes\database\database.inc</em>).

How should I structure it?

foreach ($arr_fields as $value) {
    $arr = explode(' ' , $value);
    $fields[$arr[0]] = array('type' => $arr[1]);
}

UPDATE 1:

Here's my form input pass through AJAX POST method:

enter image description here

4
  • What is the contents of $arr_fields? Could you please provide that too? Commented Feb 8, 2016 at 3:46
  • @MarioSteinitz --> It was stated above. $arr_fields[0] = 'a bigint' and $arr_fields[1] = 'b bit'. :)
    – user46175
    Commented Feb 8, 2016 at 6:26
  • Sure? Would you mind copy/pasting the part of your form as well that is supposed to return these values? Commented Feb 8, 2016 at 6:34
  • @MarioSteinitz -> See my update above, Sir :)
    – user46175
    Commented Feb 8, 2016 at 7:50

1 Answer 1

1

As I read the output of the error message, I suppose the values you are trying to parse do not contain the type of the database field.

Did you check your PHP error log as well? There might be PHP warnings, stating that $arr[1] is not set.

Verify your parsed values and make sure they contain the type information.

An additional note: I do not know your special use case. But creating a new table on each template might easily bloat your database structure. Maybe you find a better solution to store your information within one dedicated table, using your template ID as identifier and serialized arrays holding the information?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.