-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);
– Wallace Magalhães
@Wallacemagalhães did not roll, it completes the flow saying that the product was successfully registered, but when consulting the bank nothing was inserted :\
– Rafael Lander
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.
– Rafael Lander
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.
– Angelo Soares