0

我正在处理应该能够处理多个表的功能,使用$variables.

但我得到一个语法错误..

有效的代码。

$query = "INSERT INTO table_com (tableID, komNAME, komDATE, komHTML, komSTATUS) 
VALUES ('$pID','$name','$time','$kom','$status')";
if(!$mysqli->query($query)) { die($mysqli->error); }    

和没有的代码

$sID = 'table':
$query = "INSERT INTO '$sID'_com ('$sID'ID, komNAME, komDATE, komHTML, komSTATUS) 
VALUES ('$pID','$name','$time','$kom','$status')";
if(!$mysqli->query($query)) { die($mysqli->error); }

只有表和列$variables不起作用。这是 my_sql_error:

You have an error in your SQL syntax; check the manual that corresponds to your 
MySQL server version for the right syntax to use near ''diy'_com ('diy'ID, 
komNAME, komDATE, komHTML, komSTATUS) VALUES ('1','cjkm,','1' at line 1

有没有办法解决这个问题?

4

2 回答 2

0

Your idea of database design is wrong. $sID should be a field in one single table, not table name.

Please redesign your multiple-table setup to one table where $sID used to identify particular category. It will save you not a day but weeks of pain.

于 2013-08-15T11:25:41.643 回答
0

改变

$query = "INSERT INTO '$sID'_com ('$sID'ID, komNAME, komDATE, komHTML, komSTATUS) 
VALUES ('$pID','$name','$time','$kom','$status')";

进入

$query = "INSERT INTO `{$sID}_com` ({$sID}ID, komNAME, komDATE, komHTML, komSTATUS) 
VALUES ('$pID','$name','$time','$kom','$status')";

这种方式$sID将与_com.

我在这里做了一个例子。

代码:

$sID = 'something';
echo "INSERT INTO `{$sID}_com` ({$sID}ID, komNAME, komDATE, komHTML, komSTATUS) 
    VALUES ('pID','name','time','kom','status')";

结果:

INSERT INTO `something_com` (somethingID, komNAME, komDATE, komHTML, komSTATUS) VALUES ('pID','name','time','kom','status')

请注意,为了解析器,我删除了变量。

于 2013-08-15T10:07:21.107 回答