· 10 years ago · Jan 21, 2016, 01:00 AM
1$tabela = $_GET['tabela']; $nivel = $_GET['grupo']; $page_id =
2
3// Se um destes campos estiver vazio: if($titulo=='' or $imagem=='' or $texto==''){
4
5 echo "Preencha todos os campos para o menu!";
6
7 exit(); }
8
9 // Se não houver campos vazios, ele faz: else {
10
11 $servername = "localhost";
12 $username = "root";
13 $password = "";
14 $dbname = "site";
15
16
17 // Ligação à base de dados:
18 $conn = new mysqli($servername, $username, $password, $dbname);
19
20
21 // Verifica a ligação:
22 if ($conn->connect_error) {
23 die("Connection failed: " . $conn->connect_error);
24 }
25
26 // Cria a nova tabela:
27 $sql = "CREATE TABLE IF NOT EXISTS $criar (
28 id INT(9) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
29 titulo VARCHAR(255),
30 imagem LONGBLOB,
31 texto TEXT,
32 grupo INT(9),
33 FOREIGN KEY (grupo) REFERENCES $tabela(id)
34 )";
35
36
37 // Se conseguir ligar-se à base de dados e criar uma nova tabela, ele insere os dados na nova tabela:
38 if ($conn->query($sql) === TRUE) {
39
40
41 include("includes/connect.php");
42 mysql_query("SET NAMES 'utf8'");
43
44 move_uploaded_file($image_tmp,"../imagens/$imagem");
45
46
47 $insert_query = "INSERT INTO $criar (titulo, imagem, texto, grupo) VALUES ('$titulo','$imagem','$texto','$page_id')";
48
49
50 // Se inserir os dados na nova tabela, ele dá uma mensagem de sucesso:
51 if(mysql_query($insert_query)){
52
53 echo "<script>alert('Menu inserido com sucesso!')</script>";
54 echo "<script>window.open('index.php','_self')</script>";
55 }
56
57 else{
58 echo "Erro: " . $insert_query . "<br>" . $conn->error;
59 }
60 }
61
62
63
64 // Caso ele não consiga criar uma nova tabela (porque já existe), ele insere os dados na tabela já existente:
65 else {
66
67
68
69 include("includes/connect.php");
70 mysql_query("SET NAMES 'utf8'");
71
72
73 // Cria a nova tabela:
74 $sql = "CREATE TABLE IF NOT EXISTS $criar (
75 id INT(9) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
76 titulo VARCHAR(255),
77 imagem LONGBLOB,
78 texto TEXT,
79 grupo INT(9),
80 FOREIGN KEY (grupo) REFERENCES $tabela(id)
81 )";
82
83 if(mysql_query($sql)){
84
85 echo "sim!";
86 }
87 else {
88 echo "não!";
89 }
90
91 move_uploaded_file($image_tmp,"../imagens/$imagem");
92
93 $insert_query = "INSERT INTO $criar (titulo, imagem, texto, grupo) VALUES ('$titulo','$imagem','$texto','$page_id')";
94
95
96 // Caso consiga inserir os dados na tabela já existente, dá uma mensagem de sucesso:
97 if(mysql_query($insert_query&&$sql)){
98
99 echo "<script>alert('Menu inserido com sucesso!')</script>";
100 echo "<script>window.open('index.php','_self')</script>";
101
102 }
103 else{
104
105 echo "isto não está a correr bem!";
106 }
107
108 // Fecha a ligação à base de dados:
109 $conn->close();
110
111 } } }