Problem entering data into database

Asked

Viewed 32 times

0

A Notice appeared stating that the variable was not defined, but everything is right in my view.

Page addir_categoria.php

<!DOCTYPE html> <?php  session_start(); ?> <html>

<head>   <meta charset="utf-8">   <meta name="viewport" content="width=device-width, initial-scale=1">   <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" type="text/css">   <link rel="stylesheet" href="css/style.css" type="text/css">   <title>Área Administrativa</title> </head>

<body>   <?php include_once("includes/menu.php");?>   <div class="py-5">
    <div class="container">
      <div class="row">
        <div class="col-md-12">
          <div class="card">
            <div class="card-header">Adicionar Categoria
              <br>          </div>
            <div class="card-body">
                <form method="POST" action="includes/adcCategoria.php">
                    <input type="text" name="categoria" class="form-control" placeholder="Categoria" required="required">
                        <br>
                            <p class="text-center bg-danger text-white">            
                                <?php
                                    if(isset($_SESSION['categoriaErro'])){
                                    echo $_SESSION['categoriaErro'];
                                    unset($_SESSION['categoriaErro']);
                                    }
                                ?>
                            </p>
                            <p class="text-center bg-success text-white">           
                                <?php
                                    if(isset($_SESSION['categoriaSucesso'])){
                                    echo $_SESSION['categoriaSucesso'];
                                    unset($_SESSION['categoriaSucesso']);
                                    }
                                ?>
                            </p>                            
                    <a href="includes/adcCategoria.php" class="btn btn-light btn-lg btn-block">Cadastrar</a>
                </form>
            </div>
          </div>
        </div>
      </div>
    </div>   </div>   <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>   <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js" integrity="sha384-b/U6ypiBEHpOf/4+1nzFpr53nxSS+GLCkfwBdFNTxtclqqenISfwAzpKaMNFNmj4" crossorigin="anonymous"></script>   <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script> </body>

</html>

adcCategoria.php

<?php
session_start();
$adicionar_categoria = $_POST['categoria'];
include_once("../../config/conexao.php");
$resultCategoria = mysql_query("INSERT INTO categorias(nomeCategoria) VALUES ('$adicionar_categoria')");

//echo "Categoria: ".$resultCategoria['nomeCategoria'];

if(empty($resultCategoria)){
    $_SESSION['categoriaErro'] = "Por favor, adicione uma categoria válida.";
    header("Location: ../adicionar_categoria.php");
}else{
    $_SESSION['categoriaSucesso'] = "Categoria adicionada com sucesso.";
    header("Location: ../adicionar_categoria.php");
}
?>

When I try to create a new category, it really works and creates in the database, but does not insert the text, in this case, the data written in the input.

  • Welcome, for starters you could do the Tour, Answering your question, you are using a link to submit the form ?? Wouldn’t it be <input type="submit" value="Cadastrar" class="btn btn-light btn-lg btn-block" /> ??

1 answer

1


The problem is because you are not using a button to send the message.

Alter:

<a href="includes/adcCategoria.php" class="btn btn-light btn-lg btn-block">Cadastrar</a>

To:

<input type="submit" value="Cadastrar" class="btn btn-light btn-lg btn-block" />
  • Thanks, you really solved kk'

  • Dude, taking advantage of the topic, the button does not change the cursor when the mouse is over, but in other Uttons yes. I use bootstrap in this project.

  • See, this one reply can help you.

Browser other questions tagged

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