0

I'm trying to create a table where the table name is a parameter. Is it possible? Like this:

$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
)"); 
Jason Aller
  • 3,541
  • 28
  • 38
  • 38

1 Answers1

3

The table name is not a string literal but an identifier -> change single-quote to double-quotes -> quoted identifier.

The name of the id field is not pk_+string literal but the whole thing is an identifier -> "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
    )
");
VolkerK
  • 95,432
  • 20
  • 163
  • 226
  • problem solved! Your code worked too! But I think removing quotes of parameters is easier. Thanks for all tips. Stackoverflow always helps me :) – Léo Eduardo Silva Jan 06 '16 at 04:13
  • if you want uppercase letters or other unusual symbols in the name of the table then the quotes are required. – Jasen Jan 06 '16 at 04:31