PHP-插入到带有上一个select语句结果名称的表中


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

我正试图插入到mySql表中,表名是sql选择查询的结果,第一个查询返回正确的结果,但第二个查询似乎是错误所在,如果有任何帮助,将不胜感激

$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')");

您会得到错误,因为表名不能用单引号引用,而是用反引号引用。

数据库设计还有一个基本问题。您将数据保存在不同的表中,这些表实际上应该存储在一个表中。在表中添加字段counseArea以区分数据,而不是将其存储在不同的表中。