user2154825
user2154825

Reputation: 3

PHP - Insert into table with name of previous select statement result

I am trying to insert into a mySql table, the tables name is the result of a sql select query, the first query returns the correct result but the second query is where the error seems to lie, any help would be very very much appreciated

$query = mysql_query("SELECT council from users where username = '$username'");

 $x = mysql_result($query,0, "council");
 $councilArea = (string)$x;


// mysql inserting a new row
    $result = mysql_query("INSERT INTO '$councilArea' ('barcode', 'productname', 'bin', 'info', 'addedby') VALUES('$barcode', '$productname', '$bin', '$info', '$username')");

Upvotes: 0

Views: 101

Answers (1)

Lorenz Meyer
Lorenz Meyer

Reputation: 19915

You get the error because a table name must not be quoted by single quotes, but by backticks.

There is also a fundamental problem with your database design. You save data in different tables, that really should be stored in one table. Add a field counselArea in the table to distinguish the data, instead of storing it in different tables.

Upvotes: 0

Related Questions