I can not insert the bank

0

I'm using the bootstrap on my first college project and I'm having difficulty with PHP, HTML, and MySQL interaction

In addition to not being able to insert into the database, I need to insert into tipousuario_idtipousuario the values that are in select

Example:

1 -> Professor  
2 -> Técnico  
3 -> Administrador  
<div class="content">
        <div class="container-block">
            <div class="row">
                <div class="col-md-11">
                    <div class="card">
                        <div class="header">
                            <h4 class="title"></h4>
                        </div>
                        <div class="content">
                            <form action="CadastrarUsuario.php" method="POST">
                                  <div class="row">
                                    <div class="col-md-4">
                                        <div class="form-group">
                                            <label>Nome</label>
                                            <input type="text" class="form-control" placeholder="Nome" >
                                        </div>
                                    </div>

                                    <div class="col-md-4">
                                        <div class="form-group">
                                            <label>Nome Reduzido</label>
                                            <input type="text" class="form-control" placeholder="Nome Reduzido" >
                                        </div>
                                    </div>

                                      <div class="col-md-4">
                                        <div class="form-group">
                                            <label>Cpf</label>
                                            <input type="text" class="form-control" placeholder="sem traços ou pontos" >
                                        </div>
                                    </div>
                                </div>


                                <div class="row">
                                    <div class="col-md-4">
                                        <div class="form-group">
                                            <label>Senha</label>
                                            <input type="text" class="form-control" placeholder="Senha" >
                                        </div>
                                    </div>
                                    <div class="col-md-4">
                                        <div class="form-group">
                                            <label>Repete a Senha</label>
                                            <input type="text" class="form-control" placeholder="Repete a Senha" >
                                        </div>
                                    </div>
                                    <div class="col-md-4">
                                        <div class="form-group">
                                            <label>Tipo de Usuário</label>
                                            <select  class="form-control" >
                                                <option value="0">Selecione o tipo usuário: </option>
                                                <option value="1"> Professor</option>
                                                <option value="2"> Técnico</option>
                                                <option value="3"> Administrador</option>
                                            </select>   
                                        </div>
                                    </div>
                                </div>
                                <button type="submit" class="btn btn-info btn-fill pull-left">Cadastrar Usuário</button>
                                <div class="clearfix"></div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

Here's the php:

            <?php include 'VerificarSession2.php'?>
            <?php
            try{
                include 'Conexao.php';
                include 'registrarusuario.php';

                $nome = $_POST['nomeusuario'];
                $nomereduzido = $_POST['nomereduzido'];
                $cpf= $_POST['cpfusuario'];
                $senha = $_POST['senha'];
                $tipousuario_idtipousuario = $_POST['tipousuario_idtipousuario'];
                $descricao= $_POST['descricaousuario'];
                $idtipousuario= $_POST['idtipousuario'];

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

                $nome = $_POST['nomeusuario'];

            //Cadastrar Alunos na tabela 'alunos' e Disciplinas na tabela 'aluno_disciplina'
                //Inserir aluno 
            $stmt = $conexao->prepare("insert into usuario(nomeusuario,nomereduzido, cpfusuario,senha,tipousuario_idtipousuario) values (?,?,?,?,?)");



            $stmt -> bindParam(1,$nome);
            $stmt -> bindParam(2,$nomereduzido);
            $stmt -> bindParam(3,$cpf);
            $stmt -> bindParam(4,$senha);
            $stmt -> bindParam(5,$tipousuario_idtipousuario);

            $stmt->execute(); 
            //Busca o ID do aluno inserido
            $stmt = $conexao->prepare("SELECT tipousuario_idtipousuario FROM usuario WHERE idtipousuario = ?");

                $stmt -> bindParam(1,$idtipousuario);



                    $stmt->execute();

                    if($stmt->rowCount()<4){




                    $resultado = $stmt->fetchAll();
                    foreach($resultado as $linha){ 
                     $Idaluno = $linha['idaluno'];



                    }

                    }




            <!-- for($i=0;$i<count($cpfusuario);$i++){

            $stmt = $conexao->prepare("insert into aluno_disciplina(aluno_idaluno,disciplina_iddisciplina,disciplinaativo) values (?,?,1)");
            $stmt -> bindParam(1,$Idaluno);
            $stmt -> bindParam(2,$cpfusuario[$i]);

             $stmt->execute();

            }
             -->





            $_SESSION['mensagem'] = "Usuário cadastrado com sucesso!";
                    echo "<script language= 'JavaScript'>
            location.href='../mensagem.php?msg=Cadastrado&url=registraralunos.php'
            </script>"; 






            }
            else{

             $_SESSION['erromatricula'] = "Você deve selecionar ao menos uma Disciplina!";
                    echo "<script language= 'JavaScript'>
            location.href='../SelecionarDisciplinas.php?nome=".$Nome."&matricula=".$Matricula."'
            </script>";   


            } 


            }catch(PDOException $e){
            echo 'ERROR: ' . $e->getMessage();
            }
            ?>
    
asked by anonymous 11.11.2017 / 11:41

2 answers

0

In all probability this is passing POSTS that do not exist, after all its inputs do not have name and your select does not either. In your field

 <input type="text" class="form-control" ... >

Make them have names that will match their POST, as I'll show below

 <input type="text"  name ="nome" class="form-control" placrholder="Nome">
 <input type="text"  name ="nome_reduzido" class="form-control" placrholder="Nome Reduzido">
 <input type="text"  name ="cpf" class="form-control" placrholder="Cpf">
 <input type="text"  name ="senha" class="form-control" placrholder="nome">
 <select name="id_tipousuario" class="form-control"> 

Your post will look like this

$nome  = $_POST['nome'];
$nome_reduzido  = $_POST['nome'_reduzido];
$cpf  = $_POST['cpf'];
$senha  = $_POST['senha'];
$id_tipousuario  = $_POST['id_tipousuario'];

Also change your insert so that the variables are equivalent to POSTS in a way that there is no conflict in the data passing and can insert them, as I will show below

$stmt -> bindParam(1,$nome);
$stmt -> bindParam(2,$nome_reduzido);
$stmt -> bindParam(3,$cpf);
$stmt -> bindParam(4,$senha);
$stmt ->bindParam(5,$id_tipousuario);

I have already adapted the names and POSTS so that you can include in your project, if another error persists post here in the comment

    
11.11.2017 / 14:30
3

You did not name the inputs. So it does not capture the value sent by the form and can not insert into the bank. Every insertion field needs to be identified with the name attribute.

<input type="text" name="sobrenome" placeholder="Insira seu sobrenome">

And to capture in PHP you use the same name you put in the name attribute, which in our case is last name:

$sobrenome = $_POST['sobrenome'];
    
11.11.2017 / 12:48