Problem registering an item

Asked

Viewed 21 times

-2

Hello there is an error where it does not return to the defined page, but in the url it appears that it enters the page controlProduct that has been defined. The page just turns white.

This is the form.

<form class="" method="post" action="controleProduto.php">
        <h1 class="">
          <b>Adicione Produtos</b>
        </h1>
        <div class="form-group">
          <label for="nome">
            <b>Nome:</b>
          </label>
          <input type="text" class="form-control" placeholder="Nome do Produto" id="nome" name="nome"> </div>
        <div class="form-group">
          <label for="valor">
            <b>Valor:</b>
          </label>
          <input type="text" class="form-control" id="valor" name="valor" placeholder="Valor do produto" > 
        </div>
        <button type="submit" class="btn btn-success" name="opcao" id="Cadastrar">Confirmar
        </button>
        <a href="paginaSessao.php" class="btn btn-light">
          <b>Voltar</b>
        </a>
      </form>

This is the file called after the form." controlProduct".

include 'crudProduto.php';

if(isset($_POST["opcao"])){

$opcao = $_POST["opcao"];

if($opcao=="Cadastrar"){
    $nome=$_POST["nome"];
    $valor=$_POST["valor"];
    cadastraProduto($nome,$valor);
    header("Location: paginaInicial.php");
}
    else if($opcao=="Alterar"){
        $codigo=$_POST["codigo"];
        $nome=$_POST["nome"];
        $valor=$_POST["valor"];
        alterarProduto($codigo, $nome, $valor);
        header("Location: visualizarProduto.php");
    }
    else if($opcao=="Excluir"){
        $codigo=$_POST["codigo"];
        excluirProduto($codigo);
        header("Location: visualizarProduto.php");
    }

This is the file that contains the functions. "crudProduct"

include 'conexaoBD.php';
function cadastraProduto($nome,$valor){
    conectar();
    query("INSERT INTO produto (nome, valor) VALUES('$nome',$valor)");
    fechar();
}
  • I’m new to php. When you say error log, the one that appears on the page? if it is nothing appears

  • Error log means the file on the server where all errors are recorded. One of the first things a person has to learn to use to develop in PHP.

  • Anyway, while not sanitizing the strings (and it depends on which DB and library you use, information missing in the question) will give problem.

  • https://answall.com/questions/3864/70

  • In case it’s just a college job, but anyway I’m using mysql

1 answer

3


The page turns white because in controleProduto.php you are making conditional test statements that according to your code have no logic. You are setting a button as follows:

<button type="submit" class="btn btn-success" name="opcao" id="Cadastrar">Confirmar</button>

In your file controleProduto.php you are checking if it has the attribute value equal to "Cadastrar", "Alterar" e "Excluir" which is obviously incorrect because in your HTML not even the attribute value of the element button was set.

Browser other questions tagged

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