Data is not being entered into the database

Asked

Viewed 49 times

-1

I am developing a form to send some information to a bank.

I tested the connection to the bank by php and is connecting right.

However the data are not being entered in the table, I am checking a few days but I can not find where the error might be.

ps. I’m a beginner in programming, so maybe I’m making a mistake in something simple, I appreciate if someone can help me.

Follows the codes:

HTML

<!DOCTYPE html>
<html lang="ptbr">
<head>
    <meta charset="utf-8">
    <title>Test form</title>
    <link rel="stylesheet" type="text/css" href="reset.css">
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
    <header class="titulo">
        <h1>Personal control</h1>
    </header>

    <div>
        <a href="index.html">Cadastro</a>
        <a href="search.html">Pesquisa</a>
    </div>

    <main class="box">
        <form class="formulario" name="form" action="processa.php" method="post">
            <label for="nomeproduto">Nome do produto</label>
            <input type="text" id="nomeproduto" name="nome" required maxlength="30">

            <fieldset>
                <legend>Categoria do produto</legend>
                <select name="categoria">
                    <option value="none">-none-</option>
                    <option value="bebidas">Bebidas</option>
                    <option value="comida">Comida</option>
                    <option value="doces">Doces</option>
                    <option value="roupas">Roupas</option>
                    <option value="eletronicos">Eletronicos</option>
                    <option value="combustivel">Combustível</option>
                    <option value="moradia">Moradia</option>
                    <option value="planos_e_digitais">Planos e digitais</option>
                </select>
            </fieldset>

            <label for="datacompra">Data da compra</label>
            <input type="date" id="data_compra" required>

            <label for="precocompra">Valor da compra</label>
            <input type="number" id="precocompra" name="valor_compra" required placeholder="Ex: 15,00" step="0.01" min="0">

            <fieldset>
                <p>Prioridade</p>

                <label for="alta">Alta</label>
                <input type="radio" name="prioridade" value="A" id="alta" checked>

                <label for="normal">Normal</label>
                <input type="radio" name="prioridade" value="N" id="normal">

                <label for="baixa">Baixa</label>
                <input type="radio" name="prioridade" value="B" id="baixa">

                <input type="submit" name="enviar" value="Cadastrar produto" class="enviar">
            </fieldset>
        </form>
    </main>

    <footer>
    <p>© 2020 Copyright Personal control - Desenvolvido por Rafael Lander</p>
    </footer>

</body>
</html>

PHP

<?php


$servername = "localhost";
$database = "personal_control";
$username = "user";
$password = "senha";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $database);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$nome = $_POST['nome'];
$categoria = $_POST['categoria'];
$data_compra = $_POST['data_compra'];
$valor_compra = $_POST['valor_compra'];
$prioridade = $_POST['prioridade'];


$sql = "INSERT INTO produtos(NOME, CATEGORIA, DATA_COMPRA, VALOR_COMPRA, PRIORIDADE) VALUES ('$nome','$categoria','$data_compra','$valor_compra','$prioridade')";

mysqli_query($sql, $conn);

if (mysqli_query($sql, $conn)) {
      echo "Produto cadastrado com sucesso";
      echo "<a href='index.html'>Clique aqui para realizar um novo cadastro</a><br>";
      echo "<a href='search.html'>Clique aqui para realizar uma consulta</a><br>";  
} else {
      echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}


mysqli_close($conn);

?>

  • You placed the query in a variable, but did not execute it. Use mysqli_query($sql);

  • @Wallacemagalhães did not roll, it completes the flow saying that the product was successfully registered, but when consulting the bank nothing was inserted :\

  • I edited the php code here, the way it is here in my environment, I believe that the instructions I was given here should be working after the correction.

  • Oops, good night, uh... I couldn’t identify your error, but one thing I noticed is that your code needs to escape the strings that will be inserted in the database... use $string = mysqli_real_escape_string($Conn, $_POST['given']); and another: avoid exposing the sql code in the error message, just inform that there was an error when entering, do not need to expose your code.

1 answer

0


you opened the connection you defined the query($sql) but did not execute, follow an example:

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
 if ($conn->connect_error) {
 die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', '[email protected]')";

if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();

looks at the specific snippet that executes your query: $Conn->query($sql)

edited your code in my environment, noticed that olocal and worked perfectly, except that you were reversing the variables in the function, the correct: mysqli_query($Conn, $sql), and that same line was being repeated.

<?php

$servername = "localhost";
$database = "myblog";
$username = "root";
$password = "";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $database);
// Check connection
if (!$conn) {
 die("Connection failed: " . mysqli_connect_error());
}

$nome = $_POST['nome'];
$categoria = $_POST['categoria'];
$data_compra = $_POST['data_compra'];
$valor_compra = $_POST['valor_compra'];
$prioridade = $_POST['prioridade'];



$sql = "INSERT INTO produtos(NOME, CATEGORIA, DATA_COMPRA, VALOR_COMPRA, 
PRIORIDADE)VALUES('.$nome.','.$categoria.','.$data_compra.',
'.$valor_compra.',
'.$prioridade.')";


  if (mysqli_query($conn, $sql)) {
  echo "Produto cadastrado com sucesso";
  echo "<a href='index.html'>Clique aqui para realizar um novo cadastro</a> 
   <br>";
    echo "<a href='search.html'>Clique aqui para realizar uma consulta</a> 
  <br>";  
  }else{
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
   }


     mysqli_close($conn);
  • Yes, that part of running I saw that I had not even put, but for example after assigning the query to the $sql variable in the bottom row I added mysqli_query($sql); Even so the data is not being entered.

  • Marcos, he is returning : Error: INSERT INTO produtos(NOME, CATEGORIA, DATA_COMPRA, VALOR_COMPRA, PRIORIDADE) VALUES ('','','','','') But I still don’t understand the pq ?

  • tries like this: $sql = 'INSERT INTO Myguests (firstname, lastname, email) VALUES (". $first name." , ". $last name." , ". $email.")';

  • Olá Marcos, $sql = "INSERT INTO produtos(NOME, CATEGORIA, DATA_COMPRA, VALOR_COMPRA, PRIORIDADE) VALUES (".$nome.",".$categoria.",".$data_compra.",".$valor_compra.",".$prioridade.")"; I changed as you recommended me, but still it is not inserting :\

  • Well... on the spot where I put this line: echo "Error: " . $sql . "<br>" . mysqli_error($sql); for it to show me the error, it is returning the line of the Insert query: Error: INSERT INTO produtos(NOME, CATEGORIA, DATA_COMPRA, VALOR_COMPRA, PRIORIDADE) VALUES (".$nome.",".$categoria.",".$data_compra.",".$valor_compra.",".$prioridade.")

  • Yes, exactly as you recommended !

  • note that I edited the answer with your data and it worked, you were inverting the variables mysqli_query($sql, $Conn); the correct is mysqli_query($Conn, $sql); and was inserting twice the same function

  • That’s right, the variables were reversed. Thank you very much Marcos, you don’t know how much you helped me, thanks.

Show 3 more comments

Browser other questions tagged

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