1

I have an MySQL database which contains columns for ID as well as some answer possibilities for questions on client.

ID |    q1     |   q2     | ...
1  | answer 1  | answer 2 | ...

I have to adjust the entry-row dynamically, depending from the approach of the website-user (no static form).

I tried the following statement for UPDATE the database entry:

'UPDATE questions SET ? = ? WHERE id = ?', [data.qId, data.answer, id],

but it doesn´t works. when I change the first ? to the exactly column name q2 (and clear the array to this ?)it works great.

Is there any possibility for this kind of dynamic column statement, because the http.request contains only the specific answer and I hope, I can solve this with an SQL statement and not with more logic in the backend.

Thanks in advance.

Sash84
  • 39
  • 1
  • 10
  • 4
    Possible duplicate of [Variable column names using prepared statements](http://stackoverflow.com/questions/3135973/variable-column-names-using-prepared-statements) Short answer: You can't put column names as parameters to a prepared statement. Long Answer: This indicates bad database design. A better table would be: `ID | QuestionNumber | Answer` – JNevill Apr 27 '17 at 15:34
  • 2
    @JNevill I would call this Main Problem and Bigger Problem ;) – Juan Carlos Oropeza Apr 27 '17 at 15:41
  • @JuanCarlosOropeza I regret my choice of words. That is definitely the better way to put it. – JNevill Apr 27 '17 at 15:45
  • @JNevill thank you very much for yoir answer! – Sash84 Apr 27 '17 at 16:09
  • Don't splay an array (answers, in this case) across columns. – Rick James Apr 30 '17 at 19:23

0 Answers0