0
Good night.
I’ve created a registration system that’s working. What happens is when I register a user, it stays in the bank but when I reload or return a page is added another ghost user (because nothing appears) in the bank, is it because of some error in my programming?
<?php
header("Content-type:text/html; charset=utf8");
?>
<!DOCTYPE html>
<html>
<head>
<title>Olaaaaaaas</title>
<link rel="shortocut icon" type="image/png" href="img/logo.png">
<link rel="stylesheet" type="text/css" href="style.css">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>
<body>
<?php
if (isset($_POST['nome']) && isset($_POST['email']) && isset($_POST['turno']) && isset($_POST['horas']) && isset($_POST['entrada']));
{
$nome = $_POST['nome'];
$email = $_POST['email'];
$turno = $_POST['turno'];
$horas = $_POST['horas'];
$entrada = $_POST['entrada'];
try {
$username = 'root';
$password = '';
$pdo = new PDO('mysql:host=localhost;dbname=tabela', $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $pdo->prepare('INSERT INTO cronograma VALUES(:codigo,:nome,:email,:turno,:horas,:entrada)');
$stmt->execute(array(
':codigo' => null,
':nome' => $nome,
':email' => $email,
':turno' => $turno,
':horas' => $horas,
':entrada' => $entrada,
));
if ($stmt->rowCount() > 0) //verifica que o insert foi executado
{
echo "<script>alert('funcionario cadastrado');</script>";
}
} catch(PDOException $e) {
echo 'Error: ' . $e->getMessage();
}
}
?>
<h1>Cadastrar</h1>
<form method="POST" action="">
<label>Nome</label><br>
<input class="oi" type="text" name="nome" placeholder="Nome do trabalhador"><br>
<label>Email</label><br>
<input type="text" name="email" placeholder="Email do trabalhador"><br>
<label>Turno</label><br>
<input type="text" name="turno" placeholder="Turno do trabalhador"><br>
<label>Horas</label><br>
<input type="num" name="horas" placeholder="horas de contribuição"><br>
<label>Entrada</label><br>
<input type="num" name="entrada" placeholder="Inicio na empresa"><br>
<br><input type="submit">
</form>
<table class="table table-dark">
<thead>
<tr>
<th scope="col">codigo</th>
<th scope="col">Nome</th>
<th scope="col">Entrada</th>
<th scope="col">E-mail</th>
<th scope="col">turno</th>
<th scope="col">Horas de contribuição</th>
</tr>
</thead>
<tbody>
<?php
try {
$stmt = $pdo->prepare('SELECT * FROM cronograma');
$stmt->execute();
//carrega os dados do banco
$result = $stmt->fetchAll();
//imprime os dados do banco
foreach ($result as $registro => $dados) {
echo '<tr>';
echo '<td>' . $dados['codigo'] . '</td>';
echo '<td>' . $dados['nome'] . '</td>';
echo '<td>' . $dados['email'] . '</td>';
echo '<td>' . $dados['turno'] . '</td>';
echo '<td>' . $dados['horas'] . '</td>';
echo '<td>' . $dados['entrada'] . '</td>';
echo '</tr>';
}
echo 'Foram encontrado(s) '.$stmt->rowCount().' registro(s)';
} catch(PDOException $e) {
echo 'Error: ' . $e->getMessage();
}
?>
</tbody>
</table>
</body>
</html>
and my database is "table"
Table:
cronograma{
codigo int auto_increment,
nome varchar 100,
turno varchar 100,
horas varchar 100,
entrada varchar 100,
email varchar 100,
}
You are placing the insert command on the same page. I suggest creating another page only with the insert PHP code. At the end you can return to the form page using
header("Location: http://pagina.com.br/formulario.php");
– Rodrigo Tognin
Or clear variables after giving the Insert in the bank:
$_POST['nome'] = "";
This way it wouldn’t run when reloading the page. In this case you need to not only check that the variables are created with theìf (isset...
, but if they are filled as well.– Rodrigo Tognin