Not registered in PHP Mysql database

Asked

Viewed 739 times

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.

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

  • Usually no need to even call mysqli_close() because it will be closed when the script is finished.

  • only a comment, the connection with the database is all ok, the connected part.php

  • 1

    The problem is in line 4 character 20

2 answers

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 connection to the bank with PDO and mysqli, 2 connections (and only making use of PDO), recommend choosing only one for your project, other than the fact that soon after checking if it was possible to connect using mysqli you close the connection (it doesn’t make much sense, this block of code can be cleared).

$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);
  • It didn’t work either! I made the changes to the.php connection and the $result line. It keeps making mistakes when entering into the database.

  • You put down the mistake that happens here?

  • The error that returns is the Else of insert.php. if($inse_s->execute()){ echo"Success"; }Else{ echo "Error inserting data into data stream!" ; }

0


I found the error! was giving error because Thc is int and registers as string. What I did was to convert string 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);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.