SQLSTATE: parameter was not defined

Started by mileageglobal, Sep 26, 2022, 01:34 AM

Previous topic - Next topic

mileageglobalTopic starter

Dear programmers, I need your assistance with the following code:

$select_table = $this->query("SELECT title FROM dct")->fetchAll();
       
foreach($select_table as $key) {
    $k[] = $key['title'];
    $d[] = [':'.$key['title'] => $form[$key['title']].','];
}
$l = implode(',',$k);
$ls = implode(', :', $k);

$this->query('INSERT INTO dc_m (name,'.$l.', pruef, file_id) VALUES (:name, :'.$ls.', :pruef, :file_id)', [
    ':name'=>$form['name'],
    $d.':pruef'=> 1,
    ':file_id' => $file_id
]);

I receive an error message when executing the insert into statement: SQLSTATE[HY094]: Invalid parameter number: parameter was not defined. Can you please advise me on how to resolve this issue?

To provide some context, I have a table and employees can add rows to it. Based on this table, I need to add data to another table. The name passed from $_POST corresponds to the name of the title column.

The dct table contains the titles of the columns in the dc_m table. The dct table is updated dynamically, and its titles correspond to the columns of the dc_m table.

When adding data, an array of $_POST is passed, which includes all parameters that match the dct table's title column data. I need to output all dct titles dynamically, assign them to $_POST, and add them all to the dc_m table.
  •  

Bukvarix

To clarify, can employees modify the fields of another table by adding rows? If so, you may not need to store a separate list of fields in another table. Instead, you can retrieve the list of fields directly from the table by selecting * from the table with any condition, even an empty selection.

In case you need to use custom column names, you can utilize column comments. Comments are supported in both MySQL and PGSQL.

Alternatively, you can perform all operations using a single table. Simply generate the lists as usual - a list of field names, a list of parameter names, and a list of field values (parameters) as an array.

Using unnamed parameters could potentially simplify the process.
  •