使用$ _POST参数创建表

时间:2016-01-06 03:34:05

标签: php postgresql pg-query

我正在尝试创建一个名称是参数的表。可能吗?像这样:

$result = pg_query("CREATE TABLE '$_POST[nome_arquivo_software]' (
    id serial CONSTRAINT pk_'$_POST[nome_arquivo_software]' PRIMARY KEY,
    nome  varchar (80),
    email varchar (80),
    estado varchar (80),
    acessos numeric
)"); 

1 个答案:

答案 0 :(得分:3)

表名不是字符串文字,而是identifier - >将单引号更改为双引号 - >引用标识符。

id字段的名称不是pk_ + string literal,而是整个标识符 - > " PK _...."

// <--- intensive checks on $_POST[nome_arquivo_software] and $_POST[nome_arquivo_software] here
$result = pg_query("
    CREATE TABLE \"$_POST[nome_arquivo_software]\" (
    id serial CONSTRAINT \"pk_$_POST[nome_arquivo_software]\" PRIMARY KEY,
    nome  varchar (80),
    email varchar (80),
    estado varchar (80),
    acessos numeric
    )
");