I am creating a simple registration page and created a Form to store what was typed in the Database. However, even with Form validation in PHP to see if it has any empty fields, when I click Register, it stores blank data in the Database. Could you lend me a hand in this case?
This is the html form and field validations in PHP.
<form method="post" name="formCadastro" action="inserir_dados.php" >
<input type="text" id="nome" name="nome" placeholder="Seu nome completo"
<?php
if(!empty($_SESSION['value_nome'])){
echo "value='".$_SESSION['value_nome']."'";
unset($_SESSION['value_nome']);
}
?>>
<?php
if(!empty($_SESSION['vazio_nome'])){
echo "<p style='color: #b63e3e;position: relative; top:43px'>".$_SESSION['vazio_nome']."</p>";
unset($_SESSION['vazio_nome']);
}
?>
<br><br><br>
<input type="text" id="email" name="email" placeholder="Email comercial"
<?php
if(!empty($_SESSION['value_email'])){
echo "value='".$_SESSION['value_email']."'";
unset($_SESSION['value_email']);
}
?>>
<?php
if(!empty($_SESSION['vazio_email'])){
echo "<p style='color: #b63e3e;position: relative; top:43px'>".$_SESSION['vazio_email']."</p>";
unset($_SESSION['vazio_email']);
}
?>
<br><br><br>
<input type="text" id ="fone" name="fone" placeholder="Telefone com DDD"
<?php
if(!empty($_SESSION['value_fone'])){
echo "value='".$_SESSION['value_fone']."'";
unset($_SESSION['value_fone']);
}
?>>
<?php
if(!empty($_SESSION['vazio_fone'])){
echo "<p style='font-family: Helvetica Neue; color: #b63e3e;position: relative; top:43px'>".$_SESSION['vazio_fone']."</p>";
unset($_SESSION['vazio_fone']);
}
?>
<br><br><br>
<input type="submit" value="Cadastrar">
</form>
Here is the code to store in the DB.
<?php
session_start ();
include_once 'conexao.php';
$nome = null;
$email = null;
$fone = null;
if(empty($_POST['nome'])){
$_SESSION['vazio_nome'] = "Campo nome é obrigatório";
$url = 'index.php';
echo "
<META HTTP-EQUIV=REFRESH CONTENT = '0;URL=$url'>
";
}else{
$_SESSION['value_nome'] = $_POST['nome'];
}
if(empty($_POST['email'])){
$_SESSION['vazio_email'] = "Campo e-mail é obrigatório";
$url = 'index.php';
echo "
<META HTTP-EQUIV=REFRESH CONTENT = '0;URL=$url'>
";
}else{
$_SESSION['value_email'] = $_POST['email'];
}
if(empty($_POST['fone'])){
$_SESSION['vazio_fone'] = "Campo e-mail é obrigatório";
$url = 'index.php';
echo "
<META HTTP-EQUIV=REFRESH CONTENT = '0;URL=$url'>
";
}else{
$_SESSION['value_fone'] = $_POST['fone'];
}
$nome = mysqli_real_escape_string($conexao, $_POST['nome']);
$email = mysqli_real_escape_string($conexao, $_POST['email']);
$fone = mysqli_real_escape_string($conexao, $_POST['fone']);
$result_sql = "INSERT INTO cliente (nome,email,fone) VALUES ('$nome','$email','$fone')";
$resultado_sql= mysqli_query($conexao, $result_sql);
if($conexao->query($result_sql) == TRUE){
echo "Usuário OK";
}else{
echo "Erro".$result_sql."<br>".$conexao->error;
}
$conexao->close();
? >