0
Good evening, I would like a help in my code. I am trying to save 3 values in my database and am not getting. I’ve gone over the whole thing straight, and I’ve seen and reviewed the code, and I can’t find the error. When registering it gives the error in echo (Error when entering data in the data group!). Follow my code:
php.
Nome <input type="text" name="nome" placeholder="Nome da Planta"><br>
tipo <input type="text" name="tipo" placeholder="tipo da planta"><br>
thc <input type="text" name="thc" placeholder="thc"><br>
php connection.
$pdo = new PDO("mysql:host=localhost;dbname=bd_winfo", "root", "");
$link = mysqli_connect("localhost", "root", "", "bd_winfo");
if (!$link) {
echo "Error: Falha ao conectar-se com o banco de dados MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
mysqli_close($link);
insert.php
session_start();
include_once("conexao.php");
//Verifica se o usuario clicou no botao, se clicou, acessa o if e cadastra...se não....
$enviarCadastro=filter_input(INPUT_POST, 'enviarCadastro', FILTER_SANITIZE_STRING);
if($enviarCadastro) {
//Recebe dados do formulario
$nome = filter_input(INPUT_POST, 'nome', FILTER_SANITIZE_STRING);
$tipo = filter_input(INPUT_POST, 'tipo', FILTER_SANITIZE_STRING);
$thc = filter_input(INPUT_POST, 'thc', FILTER_SANITIZE_STRING);
//Inserindo no banco de dados
$result = "INSERT INTO strains ('nome', 'tipo', 'thc') VALUES (:nome, :tipo, :thc)";
$insere_s = $pdo->prepare($result);
$insere_s->bindParam(':nome', $nome);
$insere_s->bindParam(':tipo', $tipo);
$insere_s->bindParam(':thc', $thc);
if($insere_s->execute()){
echo"Sucesso";
}else{
echo"Erro ao inserir dados no bando de dados!";
}
}else{
$_SESSION['msg'] = "Cadastro nao realizado";
header("Location: cadastroPlantas.php");
}
From what I understand you’re closing the connection on include itself
mysqli_close($link);
.... when you arrive at the query execution the connection has already been closed.– Sam
I don’t know is this, maybe it has nothing to do, because I don’t know much about connection using PHP, but I found it strange.
– Sam
Usually no need to even call mysqli_close() because it will be closed when the script is finished.
– epx
only a comment, the connection with the database is all ok, the connected part.php
– Diego Souza
The problem is in line 4 character 20
– Bacco