I can’t get the seat

Asked

Viewed 53 times

0

I’m using bootstrap in my first high school project and I’m having difficulty in PHP, HTML and Mysql interaction

Besides not being able to insert in the bank, I need to insert in the tipousuario_idtipousuario the values respective who are in the 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 is 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();
            }
            ?>
  • I advise using OO in this project, procedural and very bad maintenance. Better which Exception is appearing?

2 answers

3

You have not named the inputs. So you do not capture the value sent by the form and cannot enter it into the database. 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 surname:

$sobrenome = $_POST['sobrenome'];

0


Everything indicates this passing POSTS that do not exist, after all your inputs have no name and your select also not. In your field

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

Make them have Names that will be respective to your POST, as I will 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 Index so that the variables are equivalent to POSTS in a way that there is no conflict in the data passage and can enter 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 adjusted the Names and POSTS so that you can include in your project, if you persist another error post here in the comment

Browser other questions tagged

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