1
resposta

informação nao salva no banco de dados

Olá, estou praticando o CRUD com PHP e mysql, criei um form para preencher informações mas quando clico em submit ela nao vai para o banco de dados, alguém podria ajudar?

segue código obrigado!

Formulário

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <!--Bootswatch-->
  <link rel="stylesheet" href="https://bootswatch.com/4/sandstone/bootstrap.min.css">
    <!--Font Awesome-->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
    <title>Cadastro alunos GBK</title>
</head>

<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
  <div class="container-fluid">
    <a class="navbar-brand" href="#">Navbar</a>
    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarColor02" aria-controls="navbarColor02" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>


</nav>
<br>
<body>
        <?php require_once 'cadastro.php'; ?>
        <div class="container" class="col-md-4" class="col-md-offset-4">
            <form action="cadastro.php" method="POST">
            <div class="row">
                <div class="col-6 col-md-4">
                    <div class="form-group" class="col-md-4">
                        <label for="matricula">Matricula</label>
                        <input type="number" name='matricula' class="form-control" id="matricula" aria-describedby="matricula" placeholder="Número da matrícula">
                    </div>
                </div>
                <div class="col-12 col-md-8">   
                    <div class="form-group">
                        <label for="nome">Nome</label>
                        <input type="text" class="form-control" name='nome' id="nome" placeholder="Nome Completo">
                    </div>
                </div>
            </div>
            <br>

                <div class="row">
                    <div class="col-12 col-md-8">
                        <div class="form-group">
                            <label for="faixa">Faixa atual</label>
                            <select class="form-control" aria-label="faixa" name='faixa'>
                                <option selected>Selecione a faixa</option>
                                <option value="Branca">Branca</option>
                                <option value="Amarela">Amarela</option>
                                <option value="Vermelha">Vermelha</option>
                                <option value="Laranja">Laranja</option>
                                <option value="Verde">Verde</option>
                                <option value="Roxa">Roxa</option>
                                <option value="Marrom">Marrom</option>
                                <option value="Preta">Preta</option>
                            </select>
                        </div>
                    </div>
                    <div class="col-6 col-md-4">
                        <div class="form-group">
                        <label for="dataExame">Data do último exame de faixa</label>
                        <input type="date" class="form-control" id="dataExame" name='dataexame'>
                        </div>
                    </div>
                </div>
        <br>
            <div class="row justify-content-center">
                <button type="button" class="btn btn-success" name='cadastro'>Cadastrar</button>
            </div>
            </form>
    </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.12.3/umd/popper.min.js" integrity="sha384-vFJXuSJphROIrBnz7yo7oB41mKfc8JzQZiCq4NCceLEaO4IHwicKwpJf9c9IpFgh" crossorigin="anonymous"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js" integrity="sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ" crossorigin="anonymous"></script>    
</body>
</html>

CREATE

<?php

$mysqli = new mysqli('localhost', 'root', '', 'cadastro') or die(mysqli_error($mysqli));

if (isset($_POST['cadastro'])){
    $matricula = $_POST['matricula'];
    $nome = $_POST['nome'];
    $faixa = $_POST['faixa'];
    $exame = $_POST['dataexame'];

    $mysqli->query("INSERT INTO cadastro (matricula, nome, faixa, dataexame) VALUES ('$matricula','$nome','$faixa','$exame')") or
            die($mysqli->error);
}

?>
1 resposta

Olá Ricardo,

Na sua tag <button> troque o type de 'button' para 'submit': <button type="submit" class="btn btn-success" name='cadastro'>Cadastrar</button>