Does not register in MySQL PHP database

0

Good evening, I'd like some help with my code. I'm trying to save 3 values in my database and I'm not getting it. I have already reviewed everything correctly, I have seen and reviewed the code and I do not find the error. At the time of registering it gives the error in the echo (Error when inserting data in the database!). Here is my code:

cadastro.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>

connection.php

$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");
    }   
    
asked by anonymous 25.04.2018 / 05:38

2 answers

0

I discovered the error! was giving error because thc is int and registered as string. What I did was do the string conversion to int and it worked!

$thc = filter_input(INPUT_POST, 'thc', FILTER_SANITIZE_STRING);

//converte string para INT
$thc_int = (int)$thc;

//variável $thc_int convertida
$insere_s->bindParam(':thc', $thc_int);
    
29.04.2018 / 05:59
1

Change the line:

$result = "INSERT INTO strains ('nome', 'tipo', 'thc') VALUES (:nome, :tipo, :thc)";

To:

$result = "INSERT INTO strains (nome, tipo, thc) VALUES (:nome, :tipo, :thc)";

In addition, you are opening the database connection with PDO and mysqli, 2 connections (and you are only using the PDO), I recommend choosing only one for your project, other than the fact that the You can connect using mysqli you close the connection (it does not make much sense, this block of code can be excluded).

$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);
    
25.04.2018 / 14:46