Insert Script in PHP Database

Asked

Viewed 101 times

-2

I am trying to enter data coming from a form in the database, but it is not saving in the BD. I could not detect the error. Any help will be valid.

<?php
require('login/autenticacao_usuario.php');
require('login/verificar_acesso.php');
require('BDconexao/conexao.php');
require('Agencia/funcoesAgencia.php'); //Função cadastrarAgencia encontra-se nesse arquivo
?>

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <!-- Meta, title, CSS, favicons, etc. -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title></title>

    <!-- Bootstrap -->
    <link href="../vendors/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- Font Awesome -->
    <link href="../vendors/font-awesome/css/font-awesome.min.css" rel="stylesheet">
    <!-- NProgress -->
    <link href="../vendors/nprogress/nprogress.css" rel="stylesheet">
    <!-- iCheck -->
    <link href="../vendors/iCheck/skins/flat/green.css" rel="stylesheet">

    <!-- bootstrap-progressbar -->
    <link href="../vendors/bootstrap-progressbar/css/bootstrap-progressbar-3.3.4.min.css" rel="stylesheet">
    <!-- JQVMap -->
    <link href="../vendors/jqvmap/dist/jqvmap.min.css" rel="stylesheet"/>
    <!-- bootstrap-daterangepicker -->
    <link href="../vendors/bootstrap-daterangepicker/daterangepicker.css" rel="stylesheet">


    <!-- Custom Theme Style -->
    <link href="../build/css/custom.min.css" rel="stylesheet">


  </head>

  <body class="nav-md">
    <div class="container body">
      <div class="main_container">
        <div class="col-md-3 left_col">
          <div class="left_col scroll-view">
            <div class="navbar nav_title" style="border: 0;">
              <a href="index.html" class="site_title"><!--<i class="fa fa-lightbulb-o"></i>--><img src="images/simbolo-logo.png"><span></span></a>
            </div>

            <div class="clearfix"></div>

            <!-- menu profile quick info -->
            <div class="profile clearfix">
              <div class="profile_pic">
                <img src="images/user.png" alt="..." class="img-circle profile_img">
              </div>
              <div class="profile_info">

                <span>Bem-vindo(a),</span>
                <h2><?php echo ucfirst($_SESSION['usuario']);?></h2>
              </div>
            </div>
            <!-- /menu profile quick info -->
            <br />

            <!-- sidebar menu -->
            <div id="sidebar-menu" class="main_menu_side hidden-print main_menu">
              <div class="menu_section">
                <h3>Menu Geral</h3>
                <ul class="nav side-menu">
                  <li><a href="Home-Admin.php"><i class="fa fa-home"></i> Home <span class="fa fa-chevron"></span></a>
                   </li>
                  <li><a><i class="fa fa-sitemap"></i> Agências <span class="fa fa-chevron-down"></span></a>
                    <ul class="nav child_menu">
                      <li><a href="cadastrar_agencia.html">Cadastrar Agência</a></li>
                      <li><a href="#">Lista de Agências</a></li>
                      </ul>
                  </li>                  
                   <li><a><i class="fa fa-briefcase"></i> Gerentes <span class="fa fa-chevron-down"></span></a>
                    <ul class="nav child_menu">
                      <li><a href="#">Cadastrar Gerente</a></li>
                      <li><a href="#">Lista de Gerentes</a></li>
                      </ul>
                  </li>  
                  <li><a><i class="fa fa-cogs"></i> MIP por Agência <span class="fa fa-chevron"></span></a>

                  </li>  
                </ul>
              </div>
              <div class="menu_section">
                <h3>Minha Agência</h3>
                <ul class="nav side-menu">
                  <li><a><i class="fa fa-user"></i>Funcionários <span class="fa fa-chevron-down"></span></a>
                    <ul class="nav child_menu">
                      <li><a href="#">Cadastrar Funcionário</a></li>
                      <li><a href="#">Lista de Funcionários</a></li>
                      </ul>
                  </li>
                   <li><a><i class="fa fa-users"></i>Quadro de Eletricistas<span class="fa fa-chevron-down"></span></a>
                    <ul class="nav child_menu">
                      <li><a href="#">Cadastrar Quadro</a></li>
                      <li><a href="#">Visualizar Quadro</a></li>
                      </ul>
                  </li> 
                   <li><a><i class="fa fa-clock-o"></i>Turnos<span class="fa fa-chevron-down"></span></a>
                    <ul class="nav child_menu">
                      <li><a href="#">Cadastrar Turno</a></li>
                      <li><a href="#">Visualizar Turnos</a></li>
                      </ul>
                  </li>

                  <li><a><i class="fa fa-cogs"></i> MIP <span class="fa fa-chevron-down"></span></a>
                    <ul class="nav child_menu">

                        <li><a>Ocorrências Previstas<span class="fa fa-chevron-down"></span></a>
                          <ul class="nav child_menu">
                            <li class="sub_menu"><a href="#">Cadastrar Ocorrências</a>
                            </li>
                            <li><a href="#">Visualizar Ocorrências</a>
                            </li>
                            </ul>
                        </li>
                        <li><a>Ocorrências Realizadas<span class="fa fa-chevron-down"></span></a>
                          <ul class="nav child_menu">
                            <li class="sub_menu"><a href="#">Cadastrar Ocorrências</a>
                            </li>
                            <li><a href="#">Visualizar Ocorrências</a>
                            </li>
                            </ul>
                        </li>
                    </ul>
                  </li> 


                </ul>
              </div>

            </div>

          </div>
        </div>

        <!-- top navigation -->

        <div class="top_nav">
        <center><span style="color:white;">Agência: <?php echo ($_SESSION['agencia']);?></span></center>             
          <div class="nav_menu">

            <nav>
              <div class="nav toggle">
                <a id="menu_toggle"><i class="fa fa-bars"></i></a>
                </div>

                             <ul class="nav navbar-nav navbar-right">

                <li class="">
                  <a href="javascript:;" class="user-profile dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
                    <img src="images/user.png" alt=""><?php echo ucfirst($_SESSION['usuario']);?>
                    <span class=" fa fa-angle-down"></span>
                  </a>
                  <ul class="dropdown-menu dropdown-usermenu pull-right">
                    <li><a href="javascript:;">Perfil</a></li>
                    <li><a href="login/logout.php"><i class="fa fa-sign-out pull-right"></i>Sair</a></li>
                  </ul>
                </li>

                            </ul>
            </nav>
          </div>
        </div>
        <!-- /top navigation -->

        <!-- page content -->
        <div class="right_col" role="main">
          <!-- top tiles -->

          <!-- /top tiles -->

          <div class="row">
            <div class="col-md-12 col-sm-12 col-xs-12">

            </div>

          </div>
          <br />

          <div class="row">


             <div class="col-md-6 col-xs-12">
                <div class="x_panel">
                  <div class="x_title">
                    <h2>Cadastrar nova agência</h2>

                    <div class="clearfix"></div>
                  </div>
                  <div class="x_content">
                    <br />
                    <form action="<?php cadastrarAgencia(); ?>" method="POST"  class="form-horizontal form-label-left">

                      <div class="form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12">Descrição Agência:*</label>
                        <div class="col-md-9 col-sm-9 col-xs-12">
                          <input name="descricao" type="text" class="form-control" placeholder="Ex: Consórcio Salvador" required="">
                        </div>
                      </div>

                      <div class="form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12">Estado:*</label>
                        <div class="col-md-9 col-sm-9 col-xs-12">
                          <select class="select2_single form-control" name="estado" tabindex="-1" required="">

                              <option value="">Selecione o Estado</option> 
                              <option value="ac">Acre</option> 
                              <option value="al">Alagoas</option> 
                              <option value="am">Amazonas</option> 
                              <option value="ap">Amapá</option> 
                              <option value="ba">Bahia</option> 
                              <option value="ce">Ceará</option> 
                              <option value="df">Distrito Federal</option> 
                              <option value="es">Espírito Santo</option> 
                              <option value="go">Goiás</option> 
                              <option value="ma">Maranhão</option> 
                              <option value="mt">Mato Grosso</option> 
                              <option value="ms">Mato Grosso do Sul</option> 
                              <option value="mg">Minas Gerais</option> 
                              <option value="pa">Pará</option> 
                              <option value="pb">Paraíba</option> 
                              <option value="pr">Paraná</option> 
                              <option value="pe">Pernambuco</option> 
                              <option value="pi">Piauí</option> 
                              <option value="rj">Rio de Janeiro</option> 
                              <option value="rn">Rio Grande do Norte</option> 
                              <option value="ro">Rondônia</option> 
                              <option value="rs">Rio Grande do Sul</option> 
                              <option value="rr">Roraima</option> 
                              <option value="sc">Santa Catarina</option> 
                              <option value="se">Sergipe</option> 
                              <option value="sp">São Paulo</option> 
                              <option value="to">Tocantins</option> 
                            </select>

                        </div>
                      </div>

                      <div class="form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12">Cidade:*</label>
                        <div class="col-md-9 col-sm-9 col-xs-12">
                          <input type="text" name="cidade" class="form-control" placeholder="Ex: Salvador" required="" >
                        </div>
                      </div>


                      <div class="ln_solid"></div>
                      <div class="form-group">
                        <div class="col-md-9 col-sm-9 col-xs-12 col-md-offset-3">
                          <button type="submit" class="btn btn-success">Salvar</button>
                          <button type="reset" class="btn btn-primary">Limpar</button>

                        </div>
                      </div>

                    </form>
                  </div>
                </div>
              </div>


                <!-- start of weather widget -->

                <!-- end of weather widget -->
              </div>
            </div>
          </div>
        </div>
        <!-- /page content -->

        <!-- footer content -->
        <footer>
          <div class="pull-right">
            Copyright &copy; 2017 <a target="_blank" href="#"></a> - Todos os direitos reservados
          </div>

          <div class="clearfix"></div>
        </footer>
        <!-- /footer content -->
      </div>
    </div>

    <!-- jQuery -->
    <script src="../vendors/jquery/dist/jquery.min.js"></script>
    <!-- Bootstrap -->
    <script src="../vendors/bootstrap/dist/js/bootstrap.min.js"></script>
    <!-- FastClick -->
    <script src="../vendors/fastclick/lib/fastclick.js"></script>
    <!-- NProgress -->
    <script src="../vendors/nprogress/nprogress.js"></script>
    <!-- Chart.js -->
    <script src="../vendors/Chart.js/dist/Chart.min.js"></script>
    <!-- gauge.js -->
    <script src="../vendors/gauge.js/dist/gauge.min.js"></script>
    <!-- bootstrap-progressbar -->
    <script src="../vendors/bootstrap-progressbar/bootstrap-progressbar.min.js"></script>
    <!-- iCheck -->
    <script src="../vendors/iCheck/icheck.min.js"></script>
    <!-- Skycons -->
    <script src="../vendors/skycons/skycons.js"></script>
    <!-- Flot -->
    <script src="../vendors/Flot/jquery.flot.js"></script>
    <script src="../vendors/Flot/jquery.flot.pie.js"></script>
    <script src="../vendors/Flot/jquery.flot.time.js"></script>
    <script src="../vendors/Flot/jquery.flot.stack.js"></script>
    <script src="../vendors/Flot/jquery.flot.resize.js"></script>
    <!-- Flot plugins -->
    <script src="../vendors/flot.orderbars/js/jquery.flot.orderBars.js"></script>
    <script src="../vendors/flot-spline/js/jquery.flot.spline.min.js"></script>
    <script src="../vendors/flot.curvedlines/curvedLines.js"></script>
    <!-- DateJS -->
    <script src="../vendors/DateJS/build/date.js"></script>
    <!-- JQVMap -->
    <script src="../vendors/jqvmap/dist/jquery.vmap.js"></script>
    <script src="../vendors/jqvmap/dist/maps/jquery.vmap.world.js"></script>
    <script src="../vendors/jqvmap/examples/js/jquery.vmap.sampledata.js"></script>
    <!-- bootstrap-daterangepicker -->
    <script src="../vendors/moment/min/moment.min.js"></script>
    <script src="../vendors/bootstrap-daterangepicker/daterangepicker.js"></script>

    <!-- Custom Theme Scripts -->
    <script src="../build/js/custom.min.js"></script>

  </body>
</html>

File Functions Agency:

<?php

   function cadastrarAgencia(){


  if(isset($_POST['submit'])){

   $descricao = $_POST['descricao'];
   $estado = $_POST['estado'];
   $cidade = $_POST['cidade'];


  try{

 $pdo = Conexao::getInstance();
 $insere = $pdo->prepare("INSERT INTO agencia VALUES(null,:descricao_agencia,:estado_agencia,:cidade_agencia)");

    $insere->bindParam(':descricao_agencia',$descricao, PDO::PARAM_STR);
    $insere->bindParam(':estado_agencia',$estado, PDO::PARAM_STR);
    $insere->bindParam(':cidade_agencia',$cidade, PDO::PARAM_STR);
    $insere->execute();

        if($insere!=NULL){

            print "<script> alert ('Agência cadastrada com sucesso!'); window.location.href='../cadastra_agencia.php'; </script>";


        }else{


            print "<script> alert ('Agência não cadastrada!'); window.location.href='../cadastra_agencia.php'; </script>";

        }

}catch(PDOException $e){

       echo $e->getMessage();

                       }

                        }
      }

     ?>
  • No error has appeared ?

  • No, it only gives Reload on the page. I did some tests, it seems that the form data is coming as null.

1 answer

0


You cannot put the php function call in the form action. It does not recognize. The correct thing is to submit your form to the html page itself and check if it was sent post. If so, call the registration functionAgency().

Change the form action:

<form action="nome_arquivo_html_formulario" method="POST"  class="form-horizontal form-label-left">

At the beginning of the file after the requires do a test if a post is being sent.

<?php
require('login/autenticacao_usuario.php');
require('login/verificar_acesso.php');
require('BDconexao/conexao.php');
require('Agencia/funcoesAgencia.php'); //Função cadastrarAgencia encontra-se nesse arquivo
if(isset($_POST)){
  $this->cadastrarAgencia();
?>

Browser other questions tagged

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