0

A variable is passed each time you click a link and on the new page this variable is used to identify the table in which the data (which you enter on the new page) will be stored. How to use this PHP variable inside a mysql prepared statement?

This works (when I explicitly define the table name in the code):

$stmt1 = $conn->prepare("INSERT INTO tableName (comment)
VALUES (?)");

This doesn't (when the table name is contained within a PHP variable):

$stmt1 = $conn->prepare("INSERT INTO '$phpVar' (comment)
VALUES (?)");

How to make the second variant work since the first one only works for one table while the table needs to change everytime you click a different link. It must be noted that when I echo the variable on the page it echoes the exact name of the table (the exact value that should be in the prepared statement).

mynex
  • 1
  • 5
  • 2
    wrap off `quotes` from `table name` instead use `backtick` – Saty Apr 15 '17 at 13:30
  • First, if I understand your question, do you want to prepare a query insert for multipel tables? – Jose Marques Apr 15 '17 at 13:40
  • @JoseMarques No, only for one table at a time. On the main page you have a list of links, when you click a link a variable is passed that contains the name of the table that corresponds with that link (each link "has" its own table). You enter a comment (input) and it should be sent to the table of that link. The name of the table (of that link) is contained inside the variable that is passed when the link is clicked. How to use this variable inside the prepared statement shown above. – mynex Apr 15 '17 at 13:43
  • Ask your question again. I would advise your question to include: How to get multiple id post form – Jose Marques Apr 15 '17 at 13:55
  • @Saty I tried using (backtick)phpVar(backtick) but it doesn't work. How would you do it? – mynex Apr 15 '17 at 14:21

0 Answers0