não estou conseguindo salvar os dados do formulário no banco de dados que criei:
HTML:
<!DOCTYPE html>
<html lang="pt_br">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CADASTRO</title>
<link rel="stylesheet" href="cadastro.css">
</head>
<body>
<div class="cadastro">
<h1>CADASTRAR USUÁRIO</h1>
<form method="POST" action="envia.php">
<label for="nome"> NOME:</label>
<input type="text" id="nome" size="54" name="nome" maxlength="100"><br>
<label for="idade"> IDADE:</label>
<input type="number" id="idade" name="idade">
<label for="cpf"> CPF:</label>
<input type="text" id="cpf" name="cpf" maxlength="14" pattern="\d{3}\.\d{3}\.\d{3}\-\d{2}" required title="Digite o CPF no formato xxx.xxx.xxx-xx"><br>
<button type="submit"> ENVIAR DADOS</button>
</form>
</div>
</body>
</html>
CSS:
*{
margin: 0;
padding: 0;
box-sizing: border-box;
font-size: arial;
}
input {
font-size: 20px;
padding: 10px;
margin-top: 5px;
margin-left: 5px;
}
label{
padding: 10px;
margin-right: 5px;
}
button{
margin-top: 45px;
font-size: 20px;
background-color: rgb(231, 236, 236);
margin-left: center;
cursor: pointer;
}
.cadastro{
top: 20%;
padding: 10%;
margin-left: 10%;
height: 300px;
width: 800px;
background-color: rgb(187, 177, 199);
text-align: center;
}
PHP:
<!DOCTYPE html>
<html lang="pt_br">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>RECEBER_DADOS</title>
<link rel="stylesheet" href="cadastro.css">
</head>
<body>
<?php
$conexao = mysqli_connect("localhost", "root", " ", "teste");
if(!$conexao){
echo "não conectado";
} else {
echo "conectado ao banco de dados";
}
$cpf = $_POST['cpf'];
$cpf = mysqli_real_escape_string($conexao, $cpf);
$sql = "SELECT cpf FROM teste.dados WHERE cpf='$cpf'";
$retorno = mysqli_query($conexao, $sql);
if(mysqli_num_rows($retorno) > 0) {
echo "CPF JÁ CADASTRADO! <br>";
echo "<a href='cadastro.html'> VOLTAR</a>";
} else {
$cpf = $_POST['cpf'];
$idade = $_POST['idade'];
$nome = $_POST['nome'];
$sql = "INSERT INTO teste.dados(cpf, idade, nome) VALUES ('$cpf', '$idade', '$nome')";
$resultado = mysqli_query($conexao, $sql);
echo "** USUARIO CADASTRADO COM SUCESSO!<br>";
echo "<a href='cadastro.html'> VOLTAR</a>";
}
?>
</body>
</html>
banco de dados: teste
Tabela: dados