php - can't create automatic sql tables after first use -


i'm new php , sql...

i'm trying create new tables based on url, it's working first time use it. after that, it's not possible.

here php code:

 if(isset($_get['id'])){  $tabela = $_get['tabela'];  $_get['id'];  $criar = $tabela . $nivel . $page_id;   // se clicar no botão 'confirmar', então ele faz o seguinte:  if(isset($_post['submit'])){  $titulo = $_post['titulo'];  $_files['imagem']['tmp_name']; $texto = $_post['texto'];    // se um destes campos estiver vazio: if($titulo=='' or $imagem=='' or $texto==''){              echo "preencha todos os campos para o menu!";              exit();             }                      // se não houver campos vazios, ele faz:            else {              $servername = "localhost";             $username = "root";             $password = "";             $dbname = "site";               // ligação à base de dados:             $conn = new mysqli($servername, $username, $password, $dbname);                       // verifica ligação:                     if ($conn->connect_error) {                         die("connection failed: " . $conn->connect_error);                     }               // cria nova tabela:             $sql = "create table if not exists $criar (                     id int(9) unsigned auto_increment primary key,                      titulo varchar(255),                     imagem longblob,                     texto text,                     grupo int(9),                     foreign key (grupo) references $tabela(id)                     )";                       // se conseguir ligar-se à base de dados e criar uma nova tabela, ele insere os dados na nova tabela:                     if ($conn->query($sql) === true) {                           include("includes/connect.php");                         mysql_query("set names 'utf8'");                          move_uploaded_file($image_tmp,"../imagens/$imagem");                           $insert_query = "insert $criar (titulo, imagem, texto, grupo) values ('$titulo','$imagem','$texto','$page_id')";                                   // se inserir os dados na nova tabela, ele dá uma mensagem de sucesso:                                 if(mysql_query($insert_query)){                                      echo "<script>alert('menu inserido com sucesso!')</script>";                                     echo "<script>window.open('index.php','_self')</script>";                                 }                                  else{                                     echo "erro: " . $insert_query . "<br>" . $conn->error;                                 }                     }                        // caso ele não consiga criar uma nova tabela (porque já existe), ele insere os dados na tabela já existente:                     else {                            include("includes/connect.php");                         mysql_query("set names 'utf8'");                           // cria nova tabela:                         $sql = "create table if not exists $criar (                                 id int(9) unsigned auto_increment primary key,                                  titulo varchar(255),                                 imagem longblob,                                 texto text,                                 grupo int(9),                                 foreign key (grupo) references $tabela(id)                                 )";                                  if(mysql_query($sql)){                                      echo "sim!";                                 }                                 else {                                     echo "não!";                                 }                          move_uploaded_file($image_tmp,"../imagens/$imagem");                              $insert_query = "insert $criar (titulo, imagem, texto, grupo) values ('$titulo','$imagem','$texto','$page_id')";                                   // caso consiga inserir os dados na tabela já existente, dá uma mensagem de sucesso:                                 if(mysql_query($insert_query&&$sql)){                                      echo "<script>alert('menu inserido com sucesso!')</script>";                                     echo "<script>window.open('index.php','_self')</script>";                                  }                                 else{                                      echo "isto não está correr bem!";                                 }                      // fecha ligação à base de dados:                     $conn->close();                      }           }       }$nivel = $_get['grupo'];       $page_id =$imagem = $_files['imagem']['name'];          $image_tmp = 

what mean 1st time, 2nd time? try create table same name have created? you'll table exists error.

create table if not exists $criar 

this explicitly tells mysql not create table if exists, if pass same query parameters php it'll not able create same table again.

probably change to:

$page_id = $_get['id']; $criar = $tabela . $nivel . $page_id; 

and pass different id and/or different tabela every time.


Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -