I am making a page that adds information to a table in the database but everytime I refresh the page, it adds it again. I have tried header('Location: index.php'); exit();
but it errors out saying "localhost tried to redirect you too many times."
This is my code
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<form action="index.php" method="post">
<input type="text" name="nume" placeholder="nume" required>
<input type="text" name="prenume" placeholder="prenume" required>
<input type="submit" name="sub" value="Adauga in baza de date">
<br>
</form>
<table>
<tr>
<th>ID</th>
<th>Nume</th>
<th>Prenume</th>
</tr>
<?php
if(isset($_POST['sub']))
{
$pdo = new PDO("mysql:host=localhost;dbname=liamed", "root", "");
$nume = $_POST['nume'];
$prenume = $_POST['prenume'];
$q = "insert into users(nume,prenume)values(:nume,:prenume)";
$r = $pdo->prepare($q);
$r->execute(array(":nume"=>$nume, ":prenume"=>$prenume));
$_POST = array();
}
$pdo = new PDO("mysql:host=localhost;dbname=liamed", "root", "");
$query = "select * from liamed.users";
$d = $pdo->query($query);
foreach ($d as $data){
?>
<tr>
<td><?php echo $data['id'];?></td>
<td><?php echo $data['nume'];?></td>
<td><?php echo $data['prenume'];?></td>
</tr>
</table>
</body>
<?php }
unset($_POST['sub']);
?>
</html>
Thanks!